Built-in project · Arduino Mega 2560 R3

Grove OLED on an Arduino Mega

The 1.12 inch 128x128 SH1107 OLED with Adafruit’s SH110X library on a Mega: a ball bounces around the square, sent as sixteen pages of RAM. The whole build, an Arduino Mega and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

Build it as the OLED Graphics Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $42, free on Mokxi.

The circuit runs here once the page has loaded.

What you'll learn

  • Talking to a display over I2C
  • Drawing into a frame buffer, then sending it
  • Using the Adafruit GFX and SH110X libraries
  • Animating by erasing and redrawing a shape
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: Arduino Mega 2560 R3
  • Parts: 2

How it works

The Mega talks to a 128 by 128 OLED over I2C, using the SDA and SCL pins, with 5 V and ground for power. The sketch uses Adafruit's SH110X library, with a display at address 0x3C.

The library draws into a picture held in RAM, one bit per pixel. Nothing shows until display.display() sends the picture to the screen.

The title and a frame are drawn once. Then every 40 ms a ball is erased, moved and drawn again. It bounces off the edges of the frame and the new picture is sent each time.

Parts list

2 parts, plus the jumper wires. They retail for about $56; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
OLED display, 1.12 inch SH1107 pin SCLArduino Mega 2560 R3 pin SCL
OLED display, 1.12 inch SH1107 pin SDAArduino Mega 2560 R3 pin SDA
OLED display, 1.12 inch SH1107 pin GNDArduino Mega 2560 R3 pin GND
OLED display, 1.12 inch SH1107 pin VCCArduino Mega 2560 R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Mega 2560 R3
// Grove OLED: the 1.12 inch 128x128 OLED on an SH1107, drawn with
// Adafruit's SH110X library on a Mega.
//
//   OLED   SDA -> 20 (SDA)   SCL -> 21 (SCL)   VCC -> 5 V   GND -> GND
//
// The library keeps the whole picture in RAM, one bit a pixel: 128 x 128 / 8
// is 2048 bytes, which is all of an Uno's RAM and a quarter of a Mega's, so
// this one runs on the Mega. display() sends it as sixteen pages of 128
// bytes, each with its page address and column 0 first.
//
// A ball bounces round the square, and the page count of each frame is
// printed: only the pages that changed are sent.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

Adafruit_SH1107 display(128, 128, &Wire);

int x = 20, y = 30, dx = 3, dy = 2;

void setup() {
  Serial.begin(9600);
  if (!display.begin(0x3C, true)) {
    Serial.println("SH1107 not found");
    while (1) {
    }
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(16, 4);
  display.print("Grove OLED 1.12");
  display.drawRect(0, 16, 128, 112, SH110X_WHITE);
  display.display();
  Serial.println("Grove OLED ready");
}

void loop() {
  display.fillCircle(x, y, 5, SH110X_BLACK);
  x += dx;
  y += dy;
  if (x < 7 || x > 120) dx = -dx;
  if (y < 23 || y > 120) dy = -dy;
  display.fillCircle(x, y, 5, SH110X_WHITE);
  display.display();
  delay(40);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It starts the serial port, then calls display.begin with address 0x3C. If the screen does not answer, it prints a message and stops forever. Otherwise it clears the buffer, prints the title, draws the frame with drawRect, and sends it with display.display().

void setup() {
  Serial.begin(9600);
  if (!display.begin(0x3C, true)) {
    Serial.println("SH1107 not found");
    while (1) {
    }
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(16, 4);
  display.print("Grove OLED 1.12");
  display.drawRect(0, 16, 128, 112, SH110X_WHITE);
  display.display();
  Serial.println("Grove OLED ready");
}

loop sketch.ino

Each pass erases the ball by drawing it in black, then adds dx and dy to its position. If it reaches an edge of the frame, the matching speed flips sign, so it bounces. It draws the ball in white, sends the picture, and waits 40 ms.

void loop() {
  display.fillCircle(x, y, 5, SH110X_BLACK);
  x += dx;
  y += dy;
  if (x < 7 || x > 120) dx = -dx;
  if (y < 23 || y > 120) dy = -dy;
  display.fillCircle(x, y, 5, SH110X_WHITE);
  display.display();
  delay(40);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The screen stays blank and the serial monitor says "SH1107 not found". This project
    Check SDA and SCL go to the Mega's SDA and SCL pins, and that VCC is on 5V and GND is on ground.
  • The screen is blank or shows garbage. OLED display
    Check SDA, SCL, power, and the I2C address (0x3C). Use a library for the SH1107 and the right size, 128x128. An SSD1306 driver will not work properly.
  • The Uno runs out of memory. OLED display
    The frame buffer is 2 KB, a big part of the Uno's RAM. Use a page-based mode, or a board with more memory.
  • The upload fails or nothing runs. Arduino Mega 2560 R3
    Under Tools, choose Arduino Mega or Mega 2560 as the board, then pick the right port. An Uno selection uploads the wrong build.
  • I2C, SPI, or serial pins are in the Uno spots and do not work. Arduino Mega 2560 R3
    On the Mega, SDA is pin 20 and SCL is 21. SPI uses 50 to 53, not 10 to 13. Serial1 to Serial3 are on pins 19 to 14.

Change it and keep it

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