Lesson 13: Display Devices

Put your robot arm's status on a real screen — no laptop required

📺 128×64 pixels of pure status screen — angles, mode, and live joystick data

🎯 Learning Objectives

By the end of this lesson, you will:

  • Wire a GME12864-13 OLED (SSD1306) using only 4 wires
  • Print text at pixel positions with Adafruit_SSD1306
  • Show live servo angles, mode, and stick deflection on screen
  • Refresh a display using the draw-to-buffer / push-frame pattern

Key Concepts:

  • OLED vs LCD — pixels emit their own light
  • I2C bus — two shared wires, an address instead of pins
  • Framebuffer drawing — display() pushes the frame
  • Timed screen updates with millis()

📦 Section 1: Parts Inventory

New this lesson

  • 1 × GME12864-13 — 0.96″ OLED, 128×64 px, SSD1306 driver, I2C (yellow top band, blue bottom)
  • 4 × female-to-male jumper wires

Carried over from Lesson 12

  • Arduino Uno + USB cable
  • Joystick module + 2 SG90 servos
  • Breadboard and jumper wires
  • The whole working joystick control sketch

💡 OLED vs LCD

A character LCD has a backlight and stamps letters into fixed boxes. This OLED has no backlight at all — each of its 8,192 pixels is its own tiny light. That means two upgrades: you can draw anywhere on the screen (including graphics!), and black pixels are truly off.

📌 Section 2: Pin Map & Wiring

The GME12864-13 has just four pins: GND, VCC, SCL (clock), and SDA (data). On the Uno, the I2C bus lives on the last two analog pins — the same bus any I2C device can share:

Complete robot-arm pin map

  • OLED SDA: A4 (I2C data)
  • OLED SCL: A5 (I2C clock)
  • OLED VCC / GND: power rail / ground rail (works on 3.3–5V)
  • Joystick VRx / VRy / SW: A0 / A1 / D2 (unchanged)
  • Button: A2, Buzzer (optional): pin 8
  • Base servo: pin 9, Shoulder servo: pin 10

⚠️ Check the silkscreen!

Not all OLED boards print the pins in the same order — most go GND, VCC, SCL, SDA, but some swap the first two or last two. Read the labels on YOUR board before wiring, not the cable colors.

👋 Section 3: Hello, Display

Install two libraries: Sketch → Include Library → Manage Libraries, then install Adafruit SSD1306 and Adafruit GFX Library. Now the classic first test:

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C   // most SSD1306 OLEDs; try 0x3D if blank

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
    Serial.begin(9600);
    if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
        Serial.println("OLED not found - check wiring and address!");
        while (true);   // stop here - nothing else will work
    }

    display.clearDisplay();            // wipe the drawing buffer
    display.setTextColor(SSD1306_WHITE);
    display.setTextSize(2);            // 2x = ~12x16 px per char
    display.setCursor(0, 0);           // PIXEL position, not column/row!
    display.println("Hello,");
    display.println("arm!");
    display.setTextSize(1);            // ~6x8 px per char
    display.println();
    display.println("Base: ? Mode: ?");
    display.display();                 // push the buffer to the screen
}

void loop() {
    // nothing yet - the frame stays until the next display()
}

⚠️ Black screen checklist

  • Serial says "not found"? Wrong address — change 0x3C to 0x3D.
  • Still nothing? Check pin order on the board's silkscreen — SCL/SDA are the most commonly swapped pair.
  • No contrast to adjust! Unlike LCDs there's no pot — if pixels light at all, it works.

🔄 Screen mounted upside down?

If the OLED is physically mounted flipped (pins at the bottom), you don't need to rewire anything — flip it in software. Add one line in setup() right after display.begin() succeeds:

display.setRotation(2);   // rotate everything 180 degrees

From then on every setCursor, print, and shape is drawn as if the screen were mounted right-side up — all the code in this lesson works unchanged. (setRotation(0) is the default; 1 and 3 rotate 90° sideways, which also swaps the width and height to 64×128.)

🖼️ Section 4: The Framebuffer Pattern

Here's the mind-bending part that makes OLEDs pleasant to code: nothing you draw appears until you call display.display(). Every print, fillRect, and drawCircle writes into a hidden 1 KB buffer in the Arduino's RAM. Then one call ships the whole finished frame down the I2C wire.

every refresh:
    clearDisplay()   ->  wipe the RAM buffer (screen unchanged!)
    draw everything  ->  text, numbers, shapes - still hidden
    display()        ->  whole frame appears at once, no flicker

Compare with the character LCD world: there, clear() wiped the visible screen and caused flicker, so you had to overwrite fixed-width fields. On the OLED, "clearing" is free — it only erases RAM. The screen just jumps from old frame to new frame. And since pixels are addressed in pixel coordinates, setCursor(64, 16) means halfway across, 16 pixels down — not column 64, row 16.

unsigned long lastDisplayTime = 0;
const int displayInterval = 250;   // refresh 4x per second

// Print an angle right-aligned in a 3-char field
void printAngle(int n) {
    if (n < 100) display.print(" ");
    display.print(n);
}

void updateDisplay() {
    display.clearDisplay();                    // clear buffer only
    display.setTextColor(SSD1306_WHITE);

    // Title bar (lands in the yellow band)
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.print("ARM STATUS   ");
    display.print(modeText);                   // "MANUAL"

    // Big angle readouts
    display.setTextSize(2);
    display.setCursor(0, 16);
    display.print("B:");
    printAngle(baseAngle);
    display.setCursor(64, 16);
    display.print("S:");
    printAngle(shoulderAngle);

    // Live stick deflection - debugging info
    display.setTextSize(1);
    display.setCursor(0, 48);
    display.print("dX:");
    display.print(lastDX);
    display.print("  dY:");
    display.print(lastDY);

    display.display();                         // push the frame
}

🧩 Section 5: The Complete Sketch

Lesson 12's joystick control, now reporting on the OLED. Count the timers again — motion every 15 ms, screen every 250 ms, button checked every pass. One chip, three jobs, zero delay():

// ============================================================
// Lesson 13: Display Devices (GME12864-13 OLED)
// Joystick-driven arm with live status on a 128x64 SSD1306.
//   X -> base (pin 9)   Y -> shoulder (pin 10)
//   SW click -> home   OLED -> angles + mode + deflection
// ============================================================

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Servo baseServo;
Servo shoulderServo;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// ---- Pins ----
const int joyX  = A0;
const int joyY  = A1;
const int joySW = 2;

// ---- Servo state ----
int baseAngle     = 90;
int shoulderAngle = 90;
const int MIN_ANGLE = 20;
const int MAX_ANGLE = 160;
const char modeText[] = "MANUAL";   // future: AUTO, etc.

// ---- Joystick calibration ----
int centerX = 512;
int centerY = 512;
const int deadZone = 25;
int lastDX = 0;                     // shown on the display
int lastDY = 0;

// ---- Independent timers ----
unsigned long lastStepTime    = 0;
unsigned long lastDisplayTime = 0;
const int stepInterval    = 15;    // servo update every 15 ms
const int displayInterval = 250;   // screen update every 250 ms
const int maxStep = 4;

int lastSWState = HIGH;

int signOf(int v) {
    return (v > 0) - (v < 0);
}

int stepFor(int deflection) {
    int d = abs(deflection);
    if (d < deadZone) return 0;
    return map(d, deadZone, 512, 1, maxStep);
}

void calibrateJoystick() {
    long sumX = 0, sumY = 0;
    for (int i = 0; i < 50; i++) {
        sumX += analogRead(joyX);
        sumY += analogRead(joyY);
        delay(10);
    }
    centerX = sumX / 50;
    centerY = sumY / 50;
}

void printAngle(int n) {           // right-align in 3 chars
    if (n < 100) display.print(" ");
    display.print(n);
}

void updateDisplay() {
    display.clearDisplay();
    display.setTextColor(SSD1306_WHITE);

    display.setTextSize(1);
    display.setCursor(0, 0);
    display.print("ARM STATUS   ");
    display.print(modeText);

    display.setTextSize(2);
    display.setCursor(0, 16);
    display.print("B:");
    printAngle(baseAngle);
    display.setCursor(64, 16);
    display.print("S:");
    printAngle(shoulderAngle);

    display.setTextSize(1);
    display.setCursor(0, 48);
    display.print("dX:");
    display.print(lastDX);
    display.print("  dY:");
    display.print(lastDY);

    display.display();
}

void setup() {
    Serial.begin(9600);
    baseServo.attach(9);
    shoulderServo.attach(10);
    pinMode(joySW, INPUT_PULLUP);
    baseServo.write(baseAngle);
    shoulderServo.write(shoulderAngle);

    if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
        Serial.println("OLED not found - check wiring/address");
        while (true);
    }
    display.clearDisplay();
    display.setTextColor(SSD1306_WHITE);
    display.setTextSize(1);
    display.setCursor(0, 20);
    display.println("Calibrating...");
    display.println("Hands off stick!");
    display.display();

    calibrateJoystick();
    updateDisplay();
}

void loop() {
    // Job 1: timed servo motion (every 15 ms)
    if (millis() - lastStepTime >= stepInterval) {
        lastStepTime = millis();
        lastDX = analogRead(joyX) - centerX;
        lastDY = analogRead(joyY) - centerY;
        baseAngle     = constrain(baseAngle     + signOf(lastDX) * stepFor(lastDX), MIN_ANGLE, MAX_ANGLE);
        shoulderAngle = constrain(shoulderAngle + signOf(lastDY) * stepFor(lastDY), MIN_ANGLE, MAX_ANGLE);
        baseServo.write(baseAngle);
        shoulderServo.write(shoulderAngle);
    }

    // Job 2: timed screen refresh (every 250 ms)
    if (millis() - lastDisplayTime >= displayInterval) {
        lastDisplayTime = millis();
        updateDisplay();
    }

    // Job 3: button check (every pass - no timer needed)
    int swState = digitalRead(joySW);
    if (swState == LOW && lastSWState == HIGH) {
        baseAngle = 90;
        shoulderAngle = 90;
    }
    lastSWState = swState;
}

🔍 Notice the layout math

The top ~16 pixels are the yellow band — that's why the title sits at y=0. Size-2 text is ~16 px tall, so angles sit at y=16. The debug line sits at y=48, comfortably in the blue zone. Pixel displays mean you design with a ruler, not rows.

🔧 Section 6: Tuning and Next Steps

Text runs off the edge

At size 2 each character is ~12 px wide — only ~10 fit per line. Sketch the layout on paper first: x = columns of pixels, not character cells.

Screen lags behind the arm

By design — 4 updates/sec. You can lower displayInterval to 100 safely; a full I2C frame just isn't free, so don't go every-loop.

Display garbled when servos move

Servo current spikes corrupt I2C. Separate 5V supply for servos (shared GND!), keep SDA/SCL wires short.

Sketch won't fit / crashes

The 1 KB framebuffer eats half the Uno's RAM. Avoid big String concatenation — use print() pieces like we do.

🛠️ Put It Together — Challenges

  • Joystick meter: draw a bar with fillRect() whose length follows abs(lastDX) — a live deflection gauge.
  • Draw the arm: use drawLine() to sketch a simple two-segment arm matching the real angles. Trig required — that's the fun part.
  • Add the elbow: 64 px tall fits four size-1 rows or two size-2 rows plus a footer — you don't need paging, just a new layout.
  • Mode blink: make modeText flash every 500 ms when a "HOMING" mode is active — one more state variable, one more timer.
Take Lesson 13 Quiz →
← Previous: Lesson 12
Course Overview
Next: Lesson 14 →