Sub topic 7
Getting started with an SSD1306 OLED
This chapter assumes you can upload a sketch, wire a breadboard, and install a library from the Library Manager. It also assumes you know what I2C is, from the earlier chapters on I2C basics and the address scanner. By the end you will have an SSD1306 OLED showing text, and you will know why the sketch is written the way it is.
What the SSD1306 is
The SSD1306 is a controller chip that drives small monochrome OLED panels. Each pixel makes its own light, so there is no backlight and no contrast dial. Pixels are either on or off. The most common modules are 128x64 pixels (about 0.96 inch) and 128x32 pixels (about 0.91 inch). Many are white, blue, or yellow-and-blue (the top rows have a different color).
The chip supports several bus types. The cheap four-pin modules use I2C, which is what this chapter covers. Some modules with seven pins use SPI instead. SPI needs more wires and a different constructor, so check the pin labels before you wire anything.
Compared with the character LCD from earlier chapters, the OLED has two differences that shape the code:
- It is graphical. You can place text at any pixel position, in several sizes, and draw lines and shapes.
- The library keeps a copy of the whole screen in the Arduino's RAM. You draw into that copy, then send it to the display in one step.
Wiring
A four-pin I2C module has pins labeled GND, VCC (sometimes VDD), SCL, and SDA. The order varies between vendors, so read the silkscreen on your board and do not trust a photo.
| Module pin | Uno / Nano | Mega 2560 | Leonardo | ESP32 (default) |
|---|---|---|---|---|
| GND | GND | GND | GND | GND |
| VCC | 5V or 3.3V | 5V or 3.3V | 5V or 3.3V | 3.3V |
| SDA | A4 | 20 | 2 | GPIO 21 |
| SCL | A5 | 21 | 3 | GPIO 22 |
Most modules include a small voltage regulator and accept 3.3 V to 5 V on VCC. A few bare panels accept only 3.3 V. If your board is not clearly marked, power it from 3.3 V. That works on both 5 V and 3.3 V boards. On a 5 V Arduino, the I2C lines are pulled up on the module, so a direct connection is normal for these modules.
Installing the libraries
Open the Library Manager and install Adafruit SSD1306. When asked, also install its dependencies. The important one is Adafruit GFX Library, which supplies the drawing and text functions. The SSD1306 library handles only the chip itself: initialization and sending the buffer.
Find the address first
SSD1306 modules respond at 0x3C or 0x3D. The difference comes from a solder jumper or resistor on the back of the board. Some vendors print 0x78 or 0x7A on the module. That is the same address shifted left by one bit, in the 8-bit notation that includes the read/write bit. Arduino's Wire library uses 7-bit addresses, so 0x78 means 0x3C.
If you are not sure, run the scanner sketch from the previous chapter. It will print the address that acknowledges. Use that value in display.begin().
The first sketch
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {} // display not found: stop here
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Hello, OLED"));
display.display();
}
void loop() {}Upload it. "Hello, OLED" should appear in the top-left corner. If the screen stays dark, go to the failure table near the end of this chapter.
The constructor
Adafruit_SSD1306 display(128, 64, &Wire, -1);| Argument | Meaning |
|---|---|
| `128` | Width in pixels. |
| `64` | Height in pixels. Use `32` for a 128x32 module. |
| `&Wire` | The I2C bus to use. Boards with a second bus can pass `&Wire1`. |
| `-1` | Reset pin number. `-1` means no reset pin is connected, which is the usual case for four-pin modules. |
The width and height are not detected. If you declare 64 rows on a 32-row panel, the picture is wrong: the text may appear squeezed, doubled, or cut. If you declare 32 on a 64-row panel, you get only half the screen.
The library also accepts two more optional arguments, the I2C clock speed while drawing and afterward. The defaults (400 kHz while transferring, 100 kHz afterward) are fine for most projects. They matter if other devices on the same bus cannot handle 400 kHz.
begin()
display.begin(SSD1306_SWITCHCAPVCC, 0x3C)begin() does three things. It allocates the frame buffer, it starts the I2C bus, and it sends the initialization commands to the chip. It returns false if allocation fails or the chip does not respond.
The first argument chooses how the panel gets its high voltage for the pixels. The SSD1306 contains a charge pump that boosts the supply voltage. SSD1306_SWITCHCAPVCC turns that charge pump on, and that is what virtually every module with a single supply pin needs. SSD1306_EXTERNALVCC is for a panel with a separate external supply, which hobby modules rarely have.
The second argument is the 7-bit I2C address. If you leave it at a wrong value, begin() can still succeed on some boards, because it only checks that something answers during setup, so a dark screen with a successful begin() still points to the address.
The for (;;) {} line halts the sketch when begin() fails. A halted board is hard to debug, so while developing, print a message first:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation or init failed"));
for (;;) {}
}
Serial.println(F("SSD1306 ready"));
}
void loop() {}The F() macro around string literals keeps the text in flash memory instead of copying it into RAM. This matters on an Uno, as the next section shows.
How drawing works: buffer, then display()
Calls such as println(), setCursor(), and clearDisplay() do not talk to the screen. They change bytes in the buffer in RAM. Nothing becomes visible until you call display(), which sends the whole buffer to the chip over I2C.
This explains the most common beginner problem: the sketch prints text, and the screen stays blank because display() was never called. It also explains two other behaviors:
- Drawing is fast, since it only edits RAM. The slow part is
display(), which pushes 1024 bytes for a 128x64 panel. At 400 kHz that takes roughly 25 to 30 ms, and at 100 kHz about four times longer. - You can draw many things, then call
display()once, and the screen updates in one go with no flicker.
The RAM cost
The buffer needs one bit per pixel. A 128x64 panel has 8192 pixels, so the buffer is 1024 bytes. A 128x32 panel needs 512 bytes. An Arduino Uno has 2048 bytes of RAM, so a 128x64 display takes half of it before your own variables, the serial buffers, and the I2C library take their share. begin() returns false if there is not enough memory left, which is one more reason to check its result. A later chapter covers the memory budget in detail.
When the compiler reports "Low memory available, stability problems may occur" after you add a display, the usual fixes are to wrap every string literal in F(), avoid the String class, and shrink large arrays.
Text basics
Before drawing anything, you need to know four calls.
setTextSize(n)scales the built-in font. Size 1 is 6x8 pixels per character cell. Size 2 is 12x16, and so on.setTextColor(SSD1306_WHITE)selects the "on" color. On a monochrome panel, the lit pixel color is whatever your module emits (white, blue, or yellow).setCursor(x, y)sets the top-left corner of the next character. The origin (0, 0) is the top-left corner of the screen, x grows to the right, and y grows downward.print()andprintln()write text at the cursor, then advance it. They accept the same value types asSerial.print(), includingint,float, and an optional number base.
The text size decides how much fits on a 128x64 screen:
| Text size | Cell size (pixels) | Characters per line | Lines on 128x64 |
|---|---|---|---|
| 1 | 6x8 | 21 | 8 |
| 2 | 12x16 | 10 | 4 |
| 3 | 18x24 | 7 | 2 |
By default, text wraps to the next line when it reaches the right edge. The cursor does not clear anything. If you print new text over old text, the old pixels stay lit, and the result looks garbled. Clearing and updating values cleanly is the topic of a later chapter. For now, call clearDisplay() before drawing a new frame.
The yellow-and-blue panels
Some 128x64 modules have a yellow strip in the top 16 rows and blue below. This is a property of the panel, not something you can change in software. Put the title in rows 0 to 15, and keep the main content below.
A first live example
This sketch reads a potentiometer on A0 and shows its value. It redraws the full frame on every pass, which sidesteps leftover pixels.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {}
}
display.setTextColor(SSD1306_WHITE);
}
void loop() {
int value = analogRead(A0);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.print(F("A0: "));
display.print(value);
display.display();
delay(100);
}Turn the potentiometer and the number follows it. Because clearDisplay() runs first on each pass, a shrinking number such as 1000 changing to 98 leaves no stray digit. The 100 ms delay keeps the update rate near ten frames per second, which is easy to read and spares the I2C bus.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Screen stays dark, `begin()` returns `false` | Wrong address, loose SDA or SCL wire, or no power | Run the scanner, recheck wiring, and confirm VCC and GND. |
| Screen dark, sketch continues | Missing `display()` call, or no `setTextColor` set | Call `display()` after drawing, and set a color. |
| Text appears, but only in the top half or looks stretched | Height declared as 32 vs 64 does not match the panel | Change the constructor to the true size. |
| Picture is shifted or has noise at the edges | Panel uses a different controller, such as SH1106 | Use a library for that controller. |
| Sketch hangs or behaves oddly after adding the display | Low RAM from the 1024-byte buffer | Use `F()` on strings and reduce other memory use. |
| Everything is drawn upside down | Panel is mounted rotated | Call `display.setRotation(2)` after `begin()`. |
The SH1106 deserves a note. Many modules sold as "1.3 inch OLED" use the SH1106 chip, which looks nearly the same as the SSD1306 but has a slightly wider internal memory (132 columns). With the SSD1306 library, such a panel shows a shifted picture with a noisy two-pixel column at one edge. That symptom points to the wrong controller, not to a wiring fault.
Exercise
Your sketch calls clearDisplay(), setCursor(0, 0), and println(F("Ready")) in setup(), and begin() succeeds, but the screen stays blank. What is missing, and why does the screen stay blank without it?
Show answer
The sketch never calls display.display(). The drawing calls only change the frame buffer in the Arduino's RAM. display() is the call that sends the buffer to the SSD1306 over I2C, so nothing appears until it runs. Add display.display() after the println() call.
Exercise
You have a 128x64 SSD1306 on an Arduino Uno. How many bytes does the frame buffer use, what fraction of the Uno's 2048 bytes of RAM is that, and how many characters fit on one line at text size 2?
Show answer
The panel has 128 x 64 = 8192 pixels at one bit each, so the buffer is 8192 / 8 = 1024 bytes. That is half of the Uno's 2048 bytes. At text size 2 each character cell is 12 pixels wide, so 128 / 12 gives 10 whole characters per line.