Wiring guide

How to wire an SSD1306 OLED to an Arduino Mega

To wire an SSD1306 OLED to an Arduino Mega, connect VCC to 5V, GND to GND, SDA to pin 20 and SCL to pin 21. The Arduino Mega is a 5 V board and the module has its own regulator, so it runs from 5 V and the bus stays at the board’s level. The sketch below uses the Adafruit SSD1306 and Adafruit GFX (Adafruit_SSD1306.h) library. It draws the board name, a box and a counter of the seconds since power-up.

  • 5 V logic
  • Library: Adafruit_SSD1306.h
  • 4 wires
Arduino Mega · SSD1306 OLEDlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

SSD1306 OLED to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

SSD1306 OLED pinArduino Mega pinWire colorWhy
VCC5VredThe module has its own regulator and runs from 3.3 or 5 V.
GNDGNDblueCommon ground.
SDApin 20violetArduino Mega’s I2C data line, where Wire.begin() puts it.
SCLpin 21cyanArduino Mega’s I2C clock line.
Voltage

Power and logic levels for SSD1306 OLED and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The SSD1306 OLED takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

On the Mega the I2C bus is pin 20 (SDA) and pin 21 (SCL), not A4 and A5 as on an Uno. The SDA and SCL pins by AREF are the same two nets. The display answers at address 0x3C, the address almost every 0.96 inch module ships with.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: Adafruit SSD1306 and Adafruit GFX (Adafruit_SSD1306.h). This is the sketch the circuit above runs.

SSD1306 on Arduino Mega I2C
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// 128 x 64 pixels, on the I2C bus: SDA pin 20, SCL pin 21
Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("No SSD1306 at 0x3C");
    for (;;) {}
  }
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.println("HELLO");
  display.setTextSize(1);
  display.println("SSD1306 OLED");
  display.drawRect(0, 40, 128, 24, SSD1306_WHITE);
  display.display();
  Serial.println("OLED ready");
}

void loop() {
  // A counter in the box, redrawn twice a second.
  display.fillRect(2, 42, 124, 20, SSD1306_BLACK);
  display.setCursor(8, 48);
  display.print("Up for ");
  display.print(millis() / 1000);
  display.print(" s");
  display.display();
  delay(500);
}
Output

What you should see

The panel shows HELLO in large letters, SSD1306 OLED below it, and a box with Up for followed by a number of seconds that counts up. The serial monitor, at 9600 baud, prints OLED ready once.

If the monitor prints No SSD1306 at 0x3C instead, nothing answered on the bus: check SDA and SCL first. The font in the simulator has capitals only, so lower case prints as capitals.

The serial monitor prints this, recorded from the circuit above running:

OLED ready
Pitfalls

Common mistakes wiring an SSD1306 OLED to an Arduino Mega

Forgetting display()

Every call before display() only changes the copy in RAM. A sketch that draws and never calls display() leaves the screen black, and that is the most common "my OLED does not work" there is.

Wiring SDA and SCL to A4 and A5

That is the Uno’s I2C bus. On a Mega, A4 and A5 are ordinary analog pins and the display never answers. Use 20 (SDA) and 21 (SCL), or the SDA and SCL pins by AREF.

Mixing up the 128x32 module

The 128 by 32 OLED uses the same chip and the same address, but the constructor needs 128, 32. Built for 64 rows, everything is drawn half off the panel.

Build your own SSD1306 OLED project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.