Adafruit SSD1306
by Adafruit
This is the driver for SSD1306-based OLED displays, the small 0.96-inch 128x64 and 128x32 screens found in thousands of projects. It handles the I2C or SPI communication and the display's internal commands. All the actual drawing (text, lines, shapes, bitmaps) comes from Adafruit's separate GFX library, which this library depends on, so calls like setCursor(), setTextSize(), print(), drawLine() and fillRect() are GFX functions. When you install it from the Library Manager, choose "Install all" so Adafruit GFX and Adafruit BusIO come with it.
The most important thing to understand is the frame buffer. Every drawing call only edits a copy of the screen held in your microcontroller's RAM, and nothing appears until you call display.display(), which sends the whole buffer to the screen in one go. Forgetting that call is the most common reason for a blank OLED. The same buffer-then-flush pattern is used by most Adafruit graphics drivers, so what you learn here carries over to other displays.
That buffer also explains the best-known error, SSD1306 allocation failed. A 128x64 display needs 1024 bytes of RAM for its buffer (a 128x32 display needs 512), and an Arduino Uno has only 2048 bytes in total. If your sketch also holds many strings or large arrays, there is not enough memory left and begin() fails. Wrap text in the F() macro to keep it in flash, reduce other RAM use, or move to an ESP32, which has far more memory.
If the screen stays blank, work through these in order. Check the I2C address: most modules answer at 0x3C, some at 0x3D, and a module printed with 0x78 is the same 0x3C address written in 8-bit form, so run the I2C scanner example below if unsure. Check that the width and height in the constructor match your panel. Use SSD1306_SWITCHCAPVCC unless you know your module supplies its own display voltage. And remember that a wrong address does not always make begin() return false, so a successful begin() does not prove the screen is found.
One more trap: many 1.3-inch OLEDs are sold as SSD1306 but actually use the SH1106 controller. On those, this library produces a picture that is shifted by a couple of pixels with garbage at the edge. If that is what you see, use the Adafruit SH110X library or U8g2 instead.
Wiring is simple. On an Uno, SDA is A4 and SCL is A5. On most ESP32 boards the default is SDA GPIO21 and SCL GPIO22, and you can pick other pins by calling Wire.begin(sda, scl) before display.begin(). Power the module from 3.3 V on ESP32 boards. For a more memory-friendly alternative on small boards, look at U8g2, which has a page-buffer mode that needs only a fraction of the RAM.
Setup
Installation
Arduino IDE — Library Manager
Sketch → Include Library → Manage Libraries → search Adafruit SSD1306
PlatformIO
lib_deps = adafruit/Adafruit SSD1306Compatibility
Supported boards
Code
Examples
Initialize with error check and print text
The baseline pattern for any SSD1306 project. It stops and reports if the frame buffer cannot be allocated, and shows the required display() call at the end.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64 // use 32 for 128x32 panels
#define OLED_RESET -1 // -1 = share the board reset pin
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // stop here
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Hello, SolderHub"));
display.display(); // nothing shows until this is called
}
void loop() {}Find your display's I2C address
Run this once if the screen stays blank. It prints the address of every I2C device it finds. Use the result as OLED_ADDR above.
#include <Wire.h>
void setup() {
Serial.begin(115200);
Wire.begin(); // on ESP32 with custom pins: Wire.begin(SDA_PIN, SCL_PIN);
Serial.println(F("Scanning..."));
for (uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
Serial.print(F("Found device at 0x"));
Serial.println(addr, HEX);
}
}
Serial.println(F("Done"));
}
void loop() {}Show a sensor value in large text
A small label on top and a big reading below. Text size 3 gives 18x24 pixel characters, so four characters fit across a 128-pixel screen.
float tempC = 27.4; // replace with your sensor reading
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print(F("Temperature"));
display.setTextSize(3);
display.setCursor(0, 20);
display.print(tempC, 1); // one decimal place
display.setTextSize(1);
display.setCursor(100, 20);
display.print(F("C"));
display.display();Partial redraw without flicker
Clearing only the strip you are about to rewrite avoids the flash of a full clearDisplay() on every update.
unsigned long seconds = millis() / 1000;
display.fillRect(0, 20, 128, 10, SSD1306_BLACK); // clear just this strip
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.print(F("Uptime: "));
display.print(seconds);
display.display();ESP32 with custom I2C pins
Call Wire.begin() with your own SDA and SCL pins before display.begin(). Useful when the default GPIO21 and GPIO22 are taken.
#define SDA_PIN 21
#define SCL_PIN 22
void setup() {
Wire.begin(SDA_PIN, SCL_PIN); // must come before display.begin()
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 not found"));
for (;;);
}
display.clearDisplay();
display.display();
}