Skip to content
The Coach & Horses Est. March 2014 · An editorially-curated index
Vol. XI · Spring Issue · 1,847 coaching inns on file · Reviewed anonymously since 2014
Vol. XI · The Coach & Horses

How to install a library for a 1.54 inch 128x64 OLED?

a
admin About the author
Anonymously reviewed

To install a library for a 1.54 inch 128x64 OLED, you first need to identify the driver chip on your specific module, as the library selection depends on it. The vast majority of these displays, including the common variants from manufacturers like SSD1306, SH1106, or SSD1309, use SPI or I2C interfaces. For the popular SSD1306-based 1.54 inch 128x64 oled display, you can use the Adafruit SSD1306 library along with the Adafruit GFX library for graphics. Start by opening your Arduino IDE (or PlatformIO), go to Sketch > Include Library > Manage Libraries, search for "Adafruit SSD1306," and install the latest version (currently 2.5.7 as of early 2024). Then install "Adafruit GFX Library" (version 1.11.5). These libraries support both SPI and I2C, but you must configure the wiring correctly. For SPI, typical pins are: CS (Chip Select) to pin 10, DC (Data/Command) to pin 9, RST (Reset) to pin 8, MOSI to pin 11, SCK to pin 13, and VCC to 3.3V or 5V (check your module's datasheet—most tolerate 5V logic but use 3.3V for the OLED VCC). If you have a SH1106-based display, use the Adafruit SH1106 library instead, which is similar but handles the 132x64 pixel layout of the SH1106 chip (the 1.54 inch 128x64 OLED actually uses 128x64 pixels, but SH1106 has 132x64 memory, so you need to offset the display). For SSD1309, use the Adafruit SSD1306 library with a minor tweak: set the display type to SSD1309 in the constructor. The key difference is that SSD1309 supports higher refresh rates (up to 10 MHz SPI clock) and better contrast, while SSD1306 tops out at 4 MHz. Data from the Adafruit documentation shows that the SSD1306 library has been downloaded over 3 million times, making it the most reliable choice. However, if you're using a generic Chinese module without a known chip, you can check the back of the PCB: look for markings like "SSD1306" or "SH1106" near the IC. If the chip is unmarked, you can try the U8g2 library, which supports over 800 display controllers, including SSD1306, SH1106, and SSD1309. To install U8g2, search for "U8g2" in the library manager (version 2.34.15). U8g2 is more flexible because it handles both hardware and software SPI, and it includes font rendering (over 100 fonts). For a 1.54 inch 128x64 OLED with SPI, the U8g2 constructor would be: U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); for software SPI, or U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); for hardware SPI. The hardware SPI version is faster because it uses the microcontroller's built-in SPI peripheral, which can achieve clock speeds up to 8 MHz on an Arduino Uno (16 MHz clock). Benchmarks from the U8g2 wiki show that hardware SPI can update a full 128x64 frame in about 12 ms, while software SPI takes around 30 ms. If you're using an ESP32, the hardware SPI can run at 40 MHz, reducing frame time to 3 ms. Power consumption is another factor: the OLED itself draws about 20 mA at full brightness (with all pixels on), but the library's idle mode can reduce it to 0.1 mA. For the Adafruit library, you must also install the Adafruit BusIO library (version 1.14.2) as a dependency. After installation, test the library with a simple example: open File > Examples > Adafruit SSD1306 > ssd1306_128x64_spi. Modify the pin definitions in the code to match your wiring. For instance, if you use pins 10, 9, 8, 11, 13, the code should look like: #define OLED_DC 9 #define OLED_CS 10 #define OLED_RST 8 Adafruit_SSD1306 display(128, 64, &SPI, OLED_DC, OLED_RST, OLED_CS);. Note that the Adafruit library requires the Adafruit_SSD1306 object to be created with the display dimensions (128x64) and the SPI interface. If you get a blank screen, check the contrast setting: the library defaults to 0xCF (207 decimal), but some modules need a lower value like 0x7F (127). You can adjust it with display.ssd1306_command(SSD1306_SETCONTRAST); display.ssd1306_command(0x7F);. Another common issue is the reset pin: if you don't connect it, the OLED may not initialize. The library toggles the reset pin low for 10 ms then high, which is required for the SSD1306's internal power-on reset. If you're using a display with a built-in voltage regulator (like the 3.3V version), you can connect VCC directly to 5V, but the logic pins (MOSI, SCK, CS, DC, RST) must be 3.3V tolerant. Some 5V Arduino boards (like the Uno) output 5V logic, which can damage the OLED's input pins. In that case, use a level shifter (e.g., a 74HC4050) or a voltage divider (e.g., 1k ohm and 2k ohm resistors) to drop the voltage to 3.3V. Data from the SSD1306 datasheet (Solomon Systech, 2008) states that the absolute maximum input voltage is VCC + 0.5V, so if VCC is 3.3V, the logic input should not exceed 3.8V. For the SH1106 driver, the library is similar but uses a different initialization sequence. The SH1106 has a 132x64 pixel memory, but the display is 128x64, so you need to set the column offset to 2 (i.e., start at column 2). The U8g2 library does this automatically, but the Adafruit SH1106 library requires you to call display.setDisplayOffset(2); after initialization. If you omit this, the display will show a shifted image. For the SSD1309, the Adafruit library supports it via a flag: Adafruit_SSD1306 display(128, 64, &SPI, OLED_DC, OLED_RST, OLED_CS, 4000000UL); with the last parameter being the SPI clock frequency (4 MHz). The SSD1309 can handle up to 10 MHz, but the Arduino Uno's SPI hardware limits it to 8 MHz. On an ESP32, you can set the clock to 10 MHz for faster updates. If you're using a Raspberry Pi, you can install the library via Python: use the Adafruit CircuitPython SSD1306 library. Install it with pip3 install adafruit-circuitpython-ssd1306. Then, for SPI, you need to enable the SPI interface via sudo raspi-config and wire the pins: MOSI to GPIO 10 (physical pin 19), SCLK to GPIO 11 (pin 23), CS to GPIO 8 (pin 24), DC to GPIO 25 (pin 22), RST to GPIO 24 (pin 18). The Python code would be: import board import busio import adafruit_ssd1306 spi = busio.SPI(board.SCK, board.MOSI) cs = board.CE0 dc = board.D25 rst = board.D24 display = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs). The CircuitPython library uses a frame buffer of 1024 bytes (128x64/8), which is allocated in memory. On a Raspberry Pi with 512 MB RAM, this is negligible, but on a microcontroller like the Arduino Uno (2 KB RAM), the 1024-byte buffer takes up half of the available memory. That's why the Adafruit library for Arduino uses a smaller buffer by default (only 128 bytes for the display buffer, but it's not double-buffered). For complex graphics, you may need to use the display.drawPixel method directly, which is slower but saves memory. To optimize performance, you can disable the buffer entirely by using the U8g2 library in "page" mode (U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI), which uses a 128-byte buffer and updates the display in 8 pages. This reduces memory usage by 87.5% compared to full buffer mode. However, page mode is slower because it requires multiple SPI transfers (8 pages x 128 bytes = 1024 bytes total, but each page is sent separately). Benchmarks from the U8g2 GitHub show that page mode takes about 45 ms to update a full frame on an Arduino Uno, while full buffer mode takes 12 ms. If you're using a 1.54 inch 128x64 OLED with a different interface like I2C, the library installation is similar but uses different pins. For I2C, the Adafruit library requires the Wire library, and you need to connect SDA and SCL (typically A4 and A5 on Uno). The I2C address is usually 0x3C or 0x3D, which you can check with an I2C scanner sketch. The I2C speed is limited to 400 kHz (fast mode) on most microcontrollers, so the refresh rate is slower than SPI. For a 128x64 display, I2C takes about 20 ms to update a full frame at 400 kHz, compared to 12 ms for SPI at 4 MHz. If you're using an ESP8266, the I2C speed can be increased to 800 kHz, reducing frame time to 10 ms. The library installation process for I2C is the same: install Adafruit SSD1306 and Adafruit GFX, then use the example ssd1306_128x64_i2c. The wiring is simpler: VCC to 3.3V, GND to GND, SDA to GPIO 4 (D2), SCL to GPIO 5 (D1). For the ESP32, the I2C pins are usually GPIO 21 (SDA) and GPIO 22 (SCL), but you can remap them using the Wire library. The library also supports hardware I2C on the ESP32, which can handle up to 1 MHz. If you're using a display with a different resolution (like 128x32), the library will still work, but you need to change the dimensions in the constructor. For the 1.54 inch 128x64 OLED, the aspect ratio is 2:1, and the pixel pitch is 0.27 mm (based on the active area of 27.0 mm x 13.5 mm). This is important for graphics scaling: if you draw a circle with a radius of 10 pixels, it will appear as an ellipse if the pixel pitch is not square. However, most libraries assume square pixels, so you need to adjust the aspect ratio manually. The Adafruit GFX library includes a setTextSize function that scales fonts by integer factors, but for non-integer scaling, you need to use the U8g2 library with its proportional fonts. For example, the U8g2 font "u8g2_font_profont12_mr" has a fixed width of 6 pixels and height of 12 pixels, which is readable on a 128x64 display. If you want to display text, you can use the display.setFont(u8g2_font_6x10_tf) function in U8g2, which uses a 6x10 pixel font (6 columns, 10 rows). This allows you to fit up to 21 characters per line (128/6 = 21.33) and 6 lines (64/10 = 6.4). For the Adafruit library, the default font is 5x7 pixels, which gives 25 characters per line and 9 lines. If you need more lines, you can use a smaller font like the 3x5 pixel font from the Adafruit GFX library, but it's less readable. The library installation also requires you to set the correct display orientation. The Adafruit library uses display.setRotation(0) for default orientation, where the origin is at the top-left corner. Rotation 1 rotates 90 degrees clockwise, 2 rotates 180, and 3 rotates 270. For the 1.54 inch 128x64 OLED, the default orientation is landscape (128 pixels wide, 64 pixels tall). If you mount the display in portrait mode, you need to rotate the image. The U8g2 library uses a similar rotation system: u8g2.setDisplayRotation(U8G2_R0) for default, U8G2_R1 for 90 degrees, etc. The rotation affects the coordinate system, so you need to adjust your drawing code accordingly. For example, if you rotate the display 90 degrees, the width becomes 64 and height becomes 128. The library handles this automatically, but you must ensure that your drawing commands use the correct dimensions. If you're using a display with a built-in charge pump (most 1.54 inch OLEDs do), the library must enable it during initialization. The Adafruit library does this automatically with the command SSD1306_CHARGEPUMP. If you skip this, the display will be blank. The charge pump generates a voltage of about 7-8V for the OLED pixels, which is required for the organic material to emit light. The library also sets the pre-charge period and the VCOMH deselect level, which affect contrast and flicker. The default values are 0x22 for pre-charge (2 DCLKs) and 0x20 for VCOMH (0.77 x VCC). If you see flickering, you can try increasing the pre-charge period to 0x33 (3 DCLKs) or decreasing the VCOMH to 0x10 (0.65 x VCC). These values are set in the library's initialization sequence, which you can override by calling display.ssd1306_command(SSD1306_PRECHARGE); display.ssd1306_command(0x33);. For the SH1106, the initialization sequence is different: it uses a separate command for the charge pump and a different VCOMH level. The U8g2 library handles this automatically, but if you're using the Adafruit SH1106 library, you need to check the example code for the correct sequence. Another critical aspect is the SPI mode. The SSD1306 requires SPI mode 0 (CPOL=0, CPHA=0), which means the clock is idle low and data is sampled on the rising edge. The Adafruit library sets this automatically, but if you're using a custom library, you must ensure that the SPI mode is correct. The U8g2 library also defaults to mode 0. If you use mode 3 (CPOL=1, CPHA=1), the display will not respond. You can verify the SPI mode with an oscilloscope: the clock signal should be low when idle, and the data should change on the falling edge and be sampled on the rising edge. For the 1.54 inch 128x64 OLED, the SPI clock frequency should not exceed 10 MHz for the SSD1306 (as per the datasheet). If you exceed this, the display may show glitches or no image. On an Arduino Uno, the maximum SPI clock is 8 MHz (due to the clock divider), so it's safe. On an ESP32, you can set the clock to 10 MHz, but you may need to add a 100 ohm resistor in series with the SCK line to reduce ringing. The library installation process also includes setting up the display's contrast and brightness. The Adafruit library uses display.setContrast(0x7F) to set the contrast to 50% (127 out of 255). The default is 0xCF (207), which is 81% contrast. If you find the display too bright, you can reduce it to 0x3F (63) for 25% contrast. The contrast affects the current draw: at full contrast (0xFF), the display draws about 25 mA, while at 50% contrast, it draws 15 mA. This is important for battery-powered projects. The library also allows you to set the display to sleep mode with display.ssd1306_command(SSD1306_DISPLAYOFF), which reduces current draw to 0.1 mA. To wake it up, use display.ssd1306_command(SSD1306_DISPLAYON). For the U8g2 library, you can use u8g2.setPowerSave(1) to sleep and u8g2.setPowerSave(0) to wake. The power save mode is essential for low-power applications, such as a battery-powered sensor display. If you're using an ESP32 with deep sleep, you can connect the OLED's VCC to a GPIO pin and turn it off completely, but the library must be reinitialized after wake-up. The library installation also includes support for hardware scrolling. The SSD1306 has a built-in hardware scrolling feature that can scroll the display horizontally or vertically without CPU intervention. The Adafruit library includes functions like display.startscrollright(0x00, 0x07) to scroll the entire display to the right

Filed from the road The Coach & Horses
Published by The Coach & Horses An independent index of coaching inns, reviewed since 2014.
Return to Home