Most OLED tutorials stop at "Hello World." Your real projects need a menu: pick a mode, change a setting, go back, open a submenu. Here's the pattern I use on classroom robots and small instruments: three buttons, one 128×64 SSD1306, no fancy touchscreen. It's simple.
You'll get wiring, debounce that doesn't lie, a highlight bar, nested screens and a settings page where Up/Down change a number. It's all plain Adafruit GFX. You don't need images, because the screen is just text and rectangles.
What you'll build
- A main menu with Start, Settings and About
- A settings submenu with a brightness proxy (contrast text), delay ms and back
- A highlight that moves with Up/Down, while Select enters or confirms
- Non-blocking button reads, so you can run animations later on the same board
The parts you need and how you wire them
- An Arduino Uno or Nano
- An I2C SSD1306 128×64
- Three momentary pushbuttons, wired to GND, that use the internal pull-ups
| Part | Arduino Uno |
|---|---|
| OLED VCC / GND | 5V / GND |
| OLED SDA / SCL | A4 / A5 |
| BTN_UP | D2 → GND when pressed |
| BTN_DOWN | D3 → GND when pressed |
| BTN_SEL | D4 → GND when pressed |
Use INPUT_PULLUP, so pressed reads LOW. You'll save the external resistors, and that's how most maker
kits are wired already.
Why menus break (and how you avoid it)
- No debounce. One physical press becomes five menu jumps. Fix: ignore repeats for ~180 ms after an edge. You'll feel the difference at once.
- Redraw every loop forever. You get flicker and wasted CPU. Fix: redraw only when the selection or screen changes, which isn't often.
- Blocking
delay()everywhere. Your buttons feel dead during animations. Fix: usemillis()timing. - Huge string tables in SRAM. On an Uno, put your labels in
F()/ PROGMEM flash strings.
The screen model I use
Keep a small state machine. Don't try to encode your whole UI as one giant if tree without
names. You'll hate yourself next week.
enum Screen : uint8_t {
SCR_MAIN = 0,
SCR_SETTINGS,
SCR_ABOUT,
SCR_RUN
};
Screen screen = SCR_MAIN;
int8_t cursor = 0; // highlighted row
bool dirty = true; // needs redraw
// Settings values
int delayMs = 80;
int contrastLevel = 2; // 0..3 mapped to text only in this demo
Your button helper (edge and debounce)
const uint8_t PIN_UP = 2;
const uint8_t PIN_DN = 3;
const uint8_t PIN_OK = 4;
struct Btn {
uint8_t pin;
bool lastStable;
bool lastRaw;
unsigned long lastChange;
};
Btn bUp{PIN_UP, true, true, 0};
Btn bDn{PIN_DN, true, true, 0};
Btn bOk{PIN_OK, true, true, 0};
// returns true once per press (active low)
bool pressed(Btn &b) {
bool raw = digitalRead(b.pin); // HIGH = not pressed
unsigned long now = millis();
if (raw != b.lastRaw) {
b.lastChange = now;
b.lastRaw = raw;
}
if ((now - b.lastChange) > 25) {
if (raw != b.lastStable) {
b.lastStable = raw;
if (raw == LOW) return true; // just pressed
}
}
return false;
}
25 ms is enough for cheap tactile switches. If you still get doubles, raise it to 40. Don't put
delay(200) after every press. That's how menus feel laggy.
How you draw the main menu
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const char* mainItems[] = { "Start", "Settings", "About" };
const uint8_t MAIN_COUNT = 3;
void drawMain() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("MAIN MENU"));
display.drawFastHLine(0, 10, 128, SSD1306_WHITE);
for (uint8_t i = 0; i < MAIN_COUNT; i++) {
int y = 16 + i * 14;
if (i == cursor) {
display.fillRect(0, y - 2, 128, 12, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(6, y);
display.print(mainItems[i]);
}
display.display();
}
An inverted row means selected, and students don't need it explained. You can swap the fill for a >
prefix if you'd like less flashing.
Settings screen, where you edit a value
On Settings, the cursor picks a row. When the row is a value, Up/Down change the number instead of moving the cursor, or you enter "edit mode" with Select. Here's the simpler classroom version: the cursor moves between rows, and on a value row you don't need Left. Up/Down adjust while you hold Select, or you use a dedicated edit flag.
const char* setItems[] = { "Delay ms", "Level", "Back" };
const uint8_t SET_COUNT = 3;
bool editing = false;
void drawSettings() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(editing ? F("SETTINGS *") : F("SETTINGS"));
display.drawFastHLine(0, 10, 128, SSD1306_WHITE);
for (uint8_t i = 0; i < SET_COUNT; i++) {
int y = 16 + i * 14;
bool hi = (i == cursor);
if (hi) {
display.fillRect(0, y - 2, 128, 12, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(4, y);
display.print(setItems[i]);
if (i == 0) {
display.setCursor(80, y);
display.print(delayMs);
} else if (i == 1) {
display.setCursor(80, y);
display.print(contrastLevel);
}
}
display.display();
}
Full sketch you can copy and paste
Here's the complete program. Upload it. You only need the Serial Monitor if you want debug output, since the UI lives on the OLED.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
enum Screen : uint8_t { SCR_MAIN, SCR_SETTINGS, SCR_ABOUT, SCR_RUN };
Screen screen = SCR_MAIN;
int8_t cursor = 0;
bool dirty = true;
bool editing = false;
int delayMs = 80;
int contrastLevel = 2;
const uint8_t PIN_UP = 2, PIN_DN = 3, PIN_OK = 4;
struct Btn {
uint8_t pin;
bool lastStable;
bool lastRaw;
unsigned long lastChange;
};
Btn bUp{PIN_UP, true, true, 0};
Btn bDn{PIN_DN, true, true, 0};
Btn bOk{PIN_OK, true, true, 0};
bool pressed(Btn &b) {
bool raw = digitalRead(b.pin);
unsigned long now = millis();
if (raw != b.lastRaw) { b.lastChange = now; b.lastRaw = raw; }
if ((now - b.lastChange) > 30) {
if (raw != b.lastStable) {
b.lastStable = raw;
if (raw == LOW) return true;
}
}
return false;
}
void drawMain() {
const char* items[] = {"Start", "Settings", "About"};
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("MAIN MENU"));
display.drawFastHLine(0, 10, 128, SSD1306_WHITE);
for (uint8_t i = 0; i < 3; i++) {
int y = 18 + i * 14;
if (i == cursor) {
display.fillRect(0, y - 2, 128, 12, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK);
} else display.setTextColor(SSD1306_WHITE);
display.setCursor(8, y);
display.print(items[i]);
}
display.display();
}
void drawSettings() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(editing ? F("SETTINGS [edit]") : F("SETTINGS"));
display.drawFastHLine(0, 10, 128, SSD1306_WHITE);
const char* items[] = {"Delay ms", "Level", "Back"};
for (uint8_t i = 0; i < 3; i++) {
int y = 18 + i * 14;
if (i == cursor) {
display.fillRect(0, y - 2, 128, 12, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK);
} else display.setTextColor(SSD1306_WHITE);
display.setCursor(4, y);
display.print(items[i]);
if (i == 0) { display.setCursor(78, y); display.print(delayMs); }
if (i == 1) { display.setCursor(78, y); display.print(contrastLevel); }
}
display.display();
}
void drawAbout() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("ABOUT"));
display.drawFastHLine(0, 10, 128, SSD1306_WHITE);
display.setCursor(0, 20);
display.println(F("OLED menu demo"));
display.println(F("3 buttons + SSD1306"));
display.println(F("Select = back"));
display.display();
}
void drawRun() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 10);
display.println(F("RUNNING"));
display.setTextSize(1);
display.setCursor(10, 40);
display.print(F("delay="));
display.print(delayMs);
display.println(F(" Sel=stop"));
display.display();
}
void setup() {
pinMode(PIN_UP, INPUT_PULLUP);
pinMode(PIN_DN, INPUT_PULLUP);
pinMode(PIN_OK, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) for (;;);
display.clearDisplay();
display.display();
}
void loop() {
bool up = pressed(bUp);
bool dn = pressed(bDn);
bool ok = pressed(bOk);
if (screen == SCR_MAIN) {
if (up) { cursor = (cursor + 2) % 3; dirty = true; }
if (dn) { cursor = (cursor + 1) % 3; dirty = true; }
if (ok) {
if (cursor == 0) { screen = SCR_RUN; }
else if (cursor == 1) { screen = SCR_SETTINGS; cursor = 0; editing = false; }
else { screen = SCR_ABOUT; }
dirty = true;
}
if (dirty) { drawMain(); dirty = false; }
}
else if (screen == SCR_SETTINGS) {
if (!editing) {
if (up) { cursor = (cursor + 2) % 3; dirty = true; }
if (dn) { cursor = (cursor + 1) % 3; dirty = true; }
if (ok) {
if (cursor == 2) { screen = SCR_MAIN; cursor = 1; }
else { editing = true; }
dirty = true;
}
} else {
if (up) {
if (cursor == 0) delayMs = constrain(delayMs + 10, 20, 500);
if (cursor == 1) contrastLevel = constrain(contrastLevel + 1, 0, 3);
dirty = true;
}
if (dn) {
if (cursor == 0) delayMs = constrain(delayMs - 10, 20, 500);
if (cursor == 1) contrastLevel = constrain(contrastLevel - 1, 0, 3);
dirty = true;
}
if (ok) { editing = false; dirty = true; }
}
if (dirty) { drawSettings(); dirty = false; }
}
else if (screen == SCR_ABOUT) {
if (ok) { screen = SCR_MAIN; cursor = 2; dirty = true; }
if (dirty) { drawAbout(); dirty = false; }
}
else if (screen == SCR_RUN) {
if (ok) { screen = SCR_MAIN; cursor = 0; dirty = true; }
if (dirty) { drawRun(); dirty = false; }
// place animation / sensor work here using delayMs
}
}
How you can extend this for a product
- Save settings to EEPROM when you leave Settings, so power cycles keep delayMs.
- Long-press Select for back. It helps when you only have two buttons.
- Rotary encoder instead of Up/Down. You'll keep the cursor math and only change the input driver.
- Icons as 8×8 PROGMEM bitmaps beside labels. Keep them tiny on an Uno.
- Timeout that returns you to main if nobody presses a button for 30 s. It's a simple sleep UI.
An SRAM note for your Uno
One 128×64 Adafruit buffer is 1 KB, and menu strings in RAM add up. I'd rather use
display.println(F("text")), and I'd skip String concatenation. If you add a second
full-screen buffer for animation frames, you'll crash. See
Uno memory limits.
Debugging tips from my bench
- If the highlight jumps two steps, your debounce is too short, or you're polling twice per press.
- If Select doesn't respond, check that the pin's pulled up and that you wired it to GND, not 5V.
- If the OLED blanks only after you open Settings, you may have crashed SRAM. Simplify your draw code.
- Test your buttons with Serial prints before you attach menu logic.
FAQ
Can I do this with one button?
Yes. A short press picks the next item and a long press selects. It's harder for beginners, and I've found three buttons clearer when I teach.
Will this work on ESP32?
Yes. Change SDA and SCL if you need to (often 21 and 22) and keep the same menu logic. An ESP32 has more RAM, so you can build a richer UI if you'd like.
How do I add icons next to menu items?
Call drawBitmap(0, y, icon, 8, 8, color), then print your text at x=12. You can generate the icons in the OLED animation maker as static frames, and it's quick.
The highlight skips two rows when I hold Up. Why?
The edge detector already returns one event per press, so it isn't the culprit. If you add a second "repeat while held" timer that also moves the cursor, with no gap after the first edge, you'll get two moves in the first tenth of a second. Start repeats only after about 400 ms of a steady LOW, then every 120 ms.
Select works on my main list and does nothing on the confirm screen
The confirm screen has its own cursor count. If you reuse the main-menu index without clamping, you leave
cursor at 2 when the dialog only has Yes and No. Set cursor = 0 on the way in, and you're done.
My sketch feels fine on Uno and jittery on ESP32. Why?
An ESP32 loop() can run much faster. Your 25 ms debounce window still works, but a button
wired to a pin that's also a strap or an LED will bounce in software forever. Keep your buttons on ordinary
GPIOs, OLED SDA on GPIO21, SCL on GPIO22, address 0x3C.
Give your menu a scrolling list and a confirm screen
Three rows fit under the title line of a 128×64 panel. A settings product outgrows that on your first
afternoon: brightness, timeout, units, invert, about and a destructive "erase log." Stuffing every
label into y = 16 + i * 14 pushes the last rows off the glass. Keep your draw loop, and add
a window. Here's how.
Store top as the index of the first visible row. Then you only draw what fits. Draw only three items,
items[top + row], and let cursor be the absolute index in the full list. When
Up would move the cursor above top, decrement both. When Down would move past
top + 2, increment top and stop at count - 3. The highlight
rectangle stays on screen, and the labels slide under it. Reset top and cursor
whenever you enter a screen. A Settings list of 6 and a main list of 3 can't share a leftover
index, and you'll see odd jumps if they do.
const uint8_t VISIBLE = 3;
void nudge(int8_t dir, uint8_t count) {
int next = cursor + dir;
if (next < 0 || next >= count) return; // clamp, do not wrap, on long lists
cursor = next;
if (cursor < top) top = cursor;
if (cursor > top + VISIBLE - 1) top = cursor - (VISIBLE - 1);
dirty = true;
}
Wrapping with % is friendly for a three-item demo and hostile for eight items. One extra
Down click teleports you from "Erase" back to "Brightness" with no visual warning, and you won't know why. Clamp at the
ends, and paint a tiny arrow in the right margin when top > 0 or when more rows exist
below the window. That arrow is four pixels, and it heads off the "the menu ate my item" bug reports.
Second screen: make users confirm before erase
Destructive rows shouldn't fire on the same Select that merely enters edit mode, or you'll wipe data by accident. Add
SCR_CONFIRM. From the erase row, Select sets a function pointer or a small enum
(PENDING_ERASE), forces cursor = 0, and draws two choices, "Cancel" and
"Erase," plus one line that names the action. Select on Cancel returns to Settings, and you'll find the cursor
restored to the erase row. Select on Erase performs the work, then returns. Backing out has to be the
default highlight. People mash Select when the UI feels laggy, so the safe row has to be the one under
their thumb.
Hold-to-repeat belongs on Up and Down only, in my view. Select stays a single edge, so a long press can't confirm a dialog the user hasn't read. Here's a practical set of constants, measured against cheap tactile switches rather than a datasheet:
| Timer | Starting point | What goes wrong nearby |
|---|---|---|
| Debounce stable window | 30 ms | Under 15 ms double-steps; over 60 ms feels mushy |
| Delay before key repeat | 400 ms held LOW | Shorter than ~250 ms repeats on the initial click |
| Repeat period | 120 ms | 60 ms flies past an 8-item list; 200 ms feels stuck |
| Long-press Back, if you add it | 600 ms on Select | Must be ignored while a confirm dialog is open |
You can implement repeat without delay(). Remember the timestamp of the last accepted move. If the
pin is still LOW and now - lastMove exceeds the repeat period, synthesize another Up or
Down and set dirty. Your OLED still redraws once per accepted move. A full-width inverted
bar is 128×12 white pixels. That's fine, but it's also a lot of lit area if you later care about
battery. A > marker in column 0 keeps the same navigation code and leaves the row dark, which I like.
Bench failures that look like logic bugs to you
-
The cursor runs without a finger on the button. You've left the pin floating.
INPUT_PULLUPinsetup()is mandatory on Uno D2, D3 and D4. A wire to 5V instead of GND inverts the sense, so the menu scrolls on its own and your press stops it. That's a wiring slip, not a bug. -
Opening confirm blanks the panel. You drew the dialog and returned without
display(), or the erase handler built aStringlog line and blew the 2 KB SRAM that already stores the 1024-byte framebuffer. Print withF(). -
Values edit one step past your click. Both the edge event and the repeat timer
accepted the same millisecond. Ignore repeats until
now - lastChangeis past the 400 ms arming delay. -
Your ESP32 build compiles and the highlight never moves. Buttons on GPIO0 or GPIO15 fight
boot mode, so move them. Keep your OLED on GPIO21 and GPIO22 at
0x3C.
If this list is the front end of a logger, the Run screen from the first sketch becomes page one, and
the scrolling settings become page two. Share the button helper. Don't share cursor
across screens without an explicit reset, or you'll chase ghosts. That single assignment stops the confirm dialog from
highlighting a row that doesn't exist.
Try a menu without typing code: the OLED menu template in the free OLED Block Coder uses three buttons (up, down, select). You can change the items with blocks, test the buttons on a live preview and copy the Arduino sketch.
Related guides you might like
Need icons for your menu rows?
Draw 8×8 or 16×16 glyphs in the free tool, then paste the PROGMEM arrays into your sketch. You're set.
Open oledanimationmaker.com →Free tools: OLED Animation Maker for your SSD1306 and SH1106 animations, Assemble Studio for your name and logo animations, the Shadow Box Lamp Generator for your 3D-printed shadow lamps, Text to STL for your 3D-printed names and keychains, and 3MF to Web to show your 3D prints on any website. New: the Image to Byte Array converter, the OLED Robot Eyes generator and the Lithophane Maker. See all tools.