Arduino and the SSD1306 OLED (128x64, I2C): wiring, code and live simulator
The small 0.96 inch OLED display with an SSD1306 driver connects to an Arduino with four wires: power, ground, and the two I2C wires SDA and SCL (A4 and A5 on an Uno). The Arduino draws into a 1 KB picture in its own memory, one bit per pixel, and sends the whole picture to the display's address 0x3C. The sketch below does that without any library: it writes "VoltBench", a frame counter and a moving progress bar. It runs in your browser first.
Run it in your browser (free simulator)
Press ▶ Simulate in the box above. Nothing to install and no account needed. Open it in the workbench to change it (free account).
Press Simulate
The Uno sends the start-up commands to the display and then one new picture after another: "VoltBench" in large letters, a line of small text, a frame counter and a progress bar that fills up.
Look at the bus
Each picture is 1024 bytes sent over I2C at 400 kHz in pieces of 16 bytes. The simulated display decodes exactly those bytes, so what you see is what the sketch sends.
Change the text
Open the circuit in the workbench and change the strings in loop(), or the size factor of text(): 1 is 6 pixels per character, 2 is twice as big. Run it again to see your change.
What you need
- An Arduino Uno (or Nano) and its USB cable
- A 0.96 inch 128x64 OLED module with an SSD1306 driver and four pins (GND, VCC, SCL, SDA)
- 4 female-to-male jumper wires
- Arduino IDE (the sketch needs only the built-in Wire library)
Careful: Check the order of the pins printed on your module: some boards have VCC and GND swapped (VCC, GND, SCL, SDA instead of GND, VCC, SCL, SDA). Power on the wrong pin can destroy the display.
Wiring: Arduino Uno and SSD1306 OLED
I2C uses fixed pins on the Uno: SDA is A4 and SCL is A5 (they are also brought out above AREF on the R3 board). The module has its own regulator and pull-up resistors, so it needs no other parts.
| OLED pin | Arduino pin | What it does |
|---|---|---|
| GND | GND | Ground |
| VCC | 5V | Power (most modules take 3.3 V to 5 V) |
| SCL | A5 | I2C clock |
| SDA | A4 | I2C data |
On an Arduino Mega SDA is D20 and SCL is D21; on an ESP32 the usual pins are GPIO21 (SDA) and GPIO22 (SCL). The display's address is 0x3C on almost every module; a few use 0x3D (a jumper or resistor on the back chooses it).
How the code works
The display's memory is 128 columns by 8 pages; each page is 8 pixel rows tall, and one byte is one column of 8 pixels in a page. The sketch keeps the same layout in a 1024-byte array fb. px(x, y) sets one pixel by setting bit y % 8 of byte (y / 8) × 128 + x.
setup() starts Wire at 400 kHz and sends the SSD1306 start-up sequence: display off, clock, 64 rows, the charge pump on (the module makes its own panel voltage from VCC), horizontal addressing, orientation, contrast, and display on. Every command is sent after a 0x00 control byte.
loop() clears the array, draws text with a 5x7 font kept in program memory (PROGMEM), draws the progress bar, and sends the array: first the column and page range, then the data after a 0x40 control byte, 16 bytes per I2C transmission because the Wire buffer holds only 32.
With the Adafruit SSD1306 and GFX libraries you get fonts, lines and bitmaps ready-made; they keep the same 1 KB buffer, which is half of the Uno's RAM, so leave room for the rest of your program.
Put it on the real Arduino
Wire the four pins
GND to GND, VCC to 5V, SCL to A5 and SDA to A4, checking the pin order printed on your module.
Upload the sketch
Paste the code below into a new sketch, choose your board and port, and press Upload.
Watch the display
The text and the moving bar appear within a second. If the display stays dark, run an I2C scanner sketch to see whether the module answers and at which address.
Change the address if needed
If the scanner finds the display at 0x3D, change #define ADDR 0x3C at the top of the sketch to 0x3D.
The code (Arduino C++)
This is the Arduino sketch for the real Uno, using only the built-in Wire library. The simulator runs the same code.
Show the code (176 lines)
#include <Wire.h>
#include <avr/pgmspace.h>
// SSD1306 128x64 OLED on I2C (A4 = SDA, A5 = SCL).
// Draws text and a little animation into a 1 KB frame buffer and sends the whole buffer every 100 ms.
#define ADDR 0x3C
byte fb[1024]; // 8 pages x 128 columns, bit 0 = top pixel of a page
void cmd(byte c) {
Wire.beginTransmission(ADDR);
Wire.write(0x00);
Wire.write(c);
Wire.endTransmission();
}
const byte initSeq[] PROGMEM = {
0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0x8D, 0x14, 0x20, 0x00, // off, clock, 64 rows, charge pump, horizontal mode
0xA1, 0xC8, 0xDA, 0x12, 0x81, 0xCF, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6, 0xAF // upright, contrast, normal display, on
};
void flushScreen() {
cmd(0x21); cmd(0); cmd(127); // columns 0..127
cmd(0x22); cmd(0); cmd(7); // pages 0..7
for (int i = 0; i < 1024; i += 16) { // data in pieces that fit the Wire buffer
Wire.beginTransmission(ADDR);
Wire.write(0x40);
Wire.write(fb + i, 16);
Wire.endTransmission();
}
}
void px(int x, int y) {
if (x < 0 || x > 127 || y < 0 || y > 63) return;
fb[(y >> 3) * 128 + x] |= 1 << (y & 7);
}
const uint8_t font[95][5] PROGMEM = {
{0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x5F, 0x00, 0x00},
{0x00, 0x07, 0x00, 0x07, 0x00},
{0x14, 0x7F, 0x14, 0x7F, 0x14},
{0x24, 0x2A, 0x7F, 0x2A, 0x12},
{0x23, 0x13, 0x08, 0x64, 0x62},
{0x36, 0x49, 0x55, 0x22, 0x50},
{0x00, 0x05, 0x03, 0x00, 0x00},
{0x00, 0x1C, 0x22, 0x41, 0x00},
{0x00, 0x41, 0x22, 0x1C, 0x00},
{0x08, 0x2A, 0x1C, 0x2A, 0x08},
{0x08, 0x08, 0x3E, 0x08, 0x08},
{0x00, 0x50, 0x30, 0x00, 0x00},
{0x08, 0x08, 0x08, 0x08, 0x08},
{0x00, 0x60, 0x60, 0x00, 0x00},
{0x20, 0x10, 0x08, 0x04, 0x02},
{0x3E, 0x51, 0x49, 0x45, 0x3E},
{0x00, 0x42, 0x7F, 0x40, 0x00},
{0x42, 0x61, 0x51, 0x49, 0x46},
{0x21, 0x41, 0x45, 0x4B, 0x31},
{0x18, 0x14, 0x12, 0x7F, 0x10},
{0x27, 0x45, 0x45, 0x45, 0x39},
{0x3C, 0x4A, 0x49, 0x49, 0x30},
{0x01, 0x71, 0x09, 0x05, 0x03},
{0x36, 0x49, 0x49, 0x49, 0x36},
{0x06, 0x49, 0x49, 0x29, 0x1E},
{0x00, 0x36, 0x36, 0x00, 0x00},
{0x00, 0x56, 0x36, 0x00, 0x00},
{0x08, 0x14, 0x22, 0x41, 0x00},
{0x14, 0x14, 0x14, 0x14, 0x14},
{0x00, 0x41, 0x22, 0x14, 0x08},
{0x02, 0x01, 0x51, 0x09, 0x06},
{0x32, 0x49, 0x79, 0x41, 0x3E},
{0x7E, 0x11, 0x11, 0x11, 0x7E},
{0x7F, 0x49, 0x49, 0x49, 0x36},
{0x3E, 0x41, 0x41, 0x41, 0x22},
{0x7F, 0x41, 0x41, 0x22, 0x1C},
{0x7F, 0x49, 0x49, 0x49, 0x41},
{0x7F, 0x09, 0x09, 0x09, 0x01},
{0x3E, 0x41, 0x49, 0x49, 0x7A},
{0x7F, 0x08, 0x08, 0x08, 0x7F},
{0x00, 0x41, 0x7F, 0x41, 0x00},
{0x20, 0x40, 0x41, 0x3F, 0x01},
{0x7F, 0x08, 0x14, 0x22, 0x41},
{0x7F, 0x40, 0x40, 0x40, 0x40},
{0x7F, 0x02, 0x0C, 0x02, 0x7F},
{0x7F, 0x04, 0x08, 0x10, 0x7F},
{0x3E, 0x41, 0x41, 0x41, 0x3E},
{0x7F, 0x09, 0x09, 0x09, 0x06},
{0x3E, 0x41, 0x51, 0x21, 0x5E},
{0x7F, 0x09, 0x19, 0x29, 0x46},
{0x46, 0x49, 0x49, 0x49, 0x31},
{0x01, 0x01, 0x7F, 0x01, 0x01},
{0x3F, 0x40, 0x40, 0x40, 0x3F},
{0x1F, 0x20, 0x40, 0x20, 0x1F},
{0x3F, 0x40, 0x38, 0x40, 0x3F},
{0x63, 0x14, 0x08, 0x14, 0x63},
{0x07, 0x08, 0x70, 0x08, 0x07},
{0x61, 0x51, 0x49, 0x45, 0x43},
{0x00, 0x7F, 0x41, 0x41, 0x00},
{0x02, 0x04, 0x08, 0x10, 0x20},
{0x00, 0x41, 0x41, 0x7F, 0x00},
{0x04, 0x02, 0x01, 0x02, 0x04},
{0x40, 0x40, 0x40, 0x40, 0x40},
{0x00, 0x01, 0x02, 0x04, 0x00},
{0x20, 0x54, 0x54, 0x54, 0x78},
{0x7F, 0x48, 0x44, 0x44, 0x38},
{0x38, 0x44, 0x44, 0x44, 0x20},
{0x38, 0x44, 0x44, 0x48, 0x7F},
{0x38, 0x54, 0x54, 0x54, 0x18},
{0x08, 0x7E, 0x09, 0x01, 0x02},
{0x0C, 0x52, 0x52, 0x52, 0x3E},
{0x7F, 0x08, 0x04, 0x04, 0x78},
{0x00, 0x44, 0x7D, 0x40, 0x00},
{0x20, 0x40, 0x44, 0x3D, 0x00},
{0x7F, 0x10, 0x28, 0x44, 0x00},
{0x00, 0x41, 0x7F, 0x40, 0x00},
{0x7C, 0x04, 0x18, 0x04, 0x78},
{0x7C, 0x08, 0x04, 0x04, 0x78},
{0x38, 0x44, 0x44, 0x44, 0x38},
{0x7C, 0x14, 0x14, 0x14, 0x08},
{0x08, 0x14, 0x14, 0x18, 0x7C},
{0x7C, 0x08, 0x04, 0x04, 0x08},
{0x48, 0x54, 0x54, 0x54, 0x20},
{0x04, 0x3F, 0x44, 0x40, 0x20},
{0x3C, 0x40, 0x40, 0x20, 0x7C},
{0x1C, 0x20, 0x40, 0x20, 0x1C},
{0x3C, 0x40, 0x30, 0x40, 0x3C},
{0x44, 0x28, 0x10, 0x28, 0x44},
{0x0C, 0x50, 0x50, 0x50, 0x3C},
{0x44, 0x64, 0x54, 0x4C, 0x44},
{0x00, 0x08, 0x36, 0x41, 0x00},
{0x00, 0x00, 0x7F, 0x00, 0x00},
{0x00, 0x41, 0x36, 0x08, 0x00},
{0x08, 0x08, 0x2A, 0x1C, 0x08}
};
void text(int x, int y, const char *s, byte k) {
for (; *s; s++, x += 6 * k) {
byte ch = *s;
if (ch < 32 || ch > 126) continue;
for (byte c = 0; c < 5; c++) {
byte col = pgm_read_byte(&font[ch - 32][c]);
for (byte r = 0; r < 7; r++)
if (col & (1 << r))
for (byte a = 0; a < k; a++) for (byte b = 0; b < k; b++) px(x + c * k + a, y + r * k + b);
}
}
}
void rect(int x, int y, int w, int h) {
for (int i = 0; i < w; i++) { px(x + i, y); px(x + i, y + h - 1); }
for (int j = 0; j < h; j++) { px(x, y + j); px(x + w - 1, y + j); }
}
unsigned int t = 0;
void setup() {
Wire.begin();
Wire.setClock(400000);
delay(50);
for (byte i = 0; i < sizeof(initSeq); i++) cmd(pgm_read_byte(&initSeq[i]));
}
void loop() {
memset(fb, 0, sizeof(fb));
text(0, 0, "VoltBench", 2);
text(0, 24, "SSD1306 I2C OLED", 1);
char line[16];
snprintf(line, sizeof(line), "Frame %u", t);
text(0, 36, line, 1);
rect(0, 50, 128, 12); // progress bar
for (int x = 2; x < 2 + (t % 124); x++) for (byte y = 52; y < 60; y++) px(x, y);
flushScreen();
t++;
delay(100);
}
Troubleshooting
- The display stays dark
- Check VCC and GND first (and that they are not swapped), then SDA on A4 and SCL on A5. An I2C scanner sketch tells you whether anything answers on the bus. If it answers at 0x3D, change ADDR.
- The picture is shifted, or only the top half shows
- The module is a 128x32 display or uses an SH1106 driver (common on 1.3 inch modules), which has 132 columns. For 128x32 change the row count in the start-up sequence; for SH1106 use a library for that chip.
- Random dots or snow on the screen
- The display memory is not cleared at power-up. The sketch sends a full picture every frame, so this disappears once it runs; if it stays, the bus is unreliable: shorten the wires.
- The sketch hangs or the board resets
- The frame buffer takes 1 KB of the Uno's 2 KB of RAM. Keep long strings in flash with F() and avoid big arrays elsewhere.
Frequently asked questions
What is the I2C address of the SSD1306 OLED?
Usually 0x3C. Some modules use 0x3D. The address printed on the back of the board (0x78 or 0x7A) is the same address shifted left by one bit, as some data sheets write it.
Which pins are SDA and SCL on the Arduino Uno?
A4 is SDA and A5 is SCL. The Uno R3 also has SDA and SCL pins next to AREF, which are the same two signals.
Can the OLED run on 3.3 V?
Most 0.96 inch modules have a regulator and work from 3.3 V to 5 V. Check yours: a module without the regulator needs 3.3 V.
Do I need the Adafruit library?
No. This sketch uses only Wire and draws its own text, which shows how the display works. The Adafruit SSD1306 and GFX libraries, or U8g2, save work in bigger projects.
Can I connect two OLED displays?
Yes, if they have different addresses (0x3C and 0x3D), on the same two wires. Two displays with the same address need an I2C multiplexer such as the TCA9548A.
Can I try this without a display?
Yes. The simulator on this page runs the same sketch on a virtual Uno with a virtual SSD1306 that decodes the real I2C traffic, for free in your browser.
Embed this circuit on your own page
Copy this code into your blog, tutorial or course page. Visitors can run the circuit right there.
<iframe src="https://voltbench.click/embed/gallery/arduino-oled-ssd1306-i2c" width="100%" height="560" style="border:1px solid #2a3140;border-radius:8px" loading="lazy" allowfullscreen title="Arduino and the SSD1306 OLED (128x64, I2C): wiring, code and live simulator"></iframe>
Build your own version
Open this circuit in the VoltBench workbench, change the wiring or the code, and run it. When it works, flash a real board. Free to use.