30 Days Lost in Space → Help Center

Libraries

U8g2: OLED Display Library

The go-to library for controlling OLED and LCD displays. Draw text, shapes, and graphics on SSD1306 and other display controllers.

What is U8g2?

U8g2 is a graphics library for monochrome displays (OLED, LCD). It supports a huge range of display controllers and provides functions for drawing text, shapes, and bitmaps.

Installation

Search for "U8g2" in the Arduino Library Manager and install it.

Constructor

For the 128×64 SSD1306 OLED in your kit:

#include <U8g2lib.h>
#include <Wire.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);

Basic Pattern

void setup() {
  u8g2.begin();
}

void loop() {
  u8g2.clearBuffer();       // Clear screen buffer
  // ... draw stuff ...
  u8g2.sendBuffer();        // Push to display
}

Drawing Functions

Text

u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(x, y, "Hello!");
u8g2.setCursor(x, y);
u8g2.print(42);  // Print numbers

Shapes

u8g2.drawLine(x1, y1, x2, y2);
u8g2.drawFrame(x, y, w, h);       // Rectangle outline
u8g2.drawBox(x, y, w, h);         // Filled rectangle
u8g2.drawCircle(x, y, r);         // Circle outline
u8g2.drawDisc(x, y, r);           // Filled circle
u8g2.drawTriangle(x0,y0, x1,y1, x2,y2);

Pixels

u8g2.drawPixel(x, y);

Popular Fonts

| Font | Size | Use | |------|------|-----| | u8g2_font_6x10_tr | Tiny | Status text | | u8g2_font_ncenB08_tr | Small | General text | | u8g2_font_ncenB14_tr | Medium | Headings | | u8g2_font_ncenB24_tr | Large | Big numbers | | u8g2_font_unifont_tr | Mono | Code-like text |

Rotation

// In constructor: U8G2_R0, U8G2_R1, U8G2_R2, U8G2_R3
// R0 = 0°, R1 = 90°, R2 = 180°, R3 = 270°