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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Parts list
2 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Mega 2560 R3 pin SCL; OLED display, 1.12 inch SH1107 pin SCL
- Arduino Mega 2560 R3 pin SDA; OLED display, 1.12 inch SH1107 pin SDA
- Ground: Arduino Mega 2560 R3 pin GND; OLED display, 1.12 inch SH1107 pin GND
- Arduino Mega 2560 R3 pin 5V; OLED display, 1.12 inch SH1107 pin VCC
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.