Proje Defteri

OLED Menu Generator for Arduino

Build the OLED menu for your Arduino or ESP32 project by clicking: submenus, adjustable numbers, on/off options. Browse it with the keys under the screen, then copy ready-made code for buttons or a rotary encoder. Everything runs in your browser.

You can also click the screen and use the arrow keys and Enter. On a number item, Select starts editing and Select again stops.

Action runs your function when selected, Number steps between min and max, Toggle flips on every select, Submenu opens its own items (up to 3 levels).

An Arduino OLED menu with the code already written

Sooner or later a project needs a settings screen, and it always lands in the same place: three buttons or a rotary encoder, a 0.96 inch OLED and dozens of hand-written if statements. Which row is selected, where the cursor goes when you leave a submenu, how the list scrolls once it no longer fits: each of these gets coded by hand. This tool takes that part off your plate. You decide what the menu looks like; the code that navigates, draws and reads the keys comes ready.

How to use it

  1. Pick the library. Adafruit SSD1306 is the most common one; U8g2 also drives the 1.3 inch SH1106 modules and needs less RAM.
  2. Pick the input: 3 buttons for up, down and select, or a rotary encoder with a push button (such as the KY-040). Enter the pin numbers for your board.
  3. Add items, name them, choose a type, reorder them with the arrows. Submenus take their own items.
  4. Browse the menu with the keys under the preview. The messages panel warns when a row does not fit or a submenu is empty.
  5. Copy the code or download it as an .ino. Fill in the action functions and upload.

Why the preview can be trusted

The preview is not a picture of the menu. The page runs the exact code it hands you in the Arduino interpreter behind the OLED simulator, and every press of ▲, ▼ or Select becomes a call to that code's onUp(), onDown() and selectItem(). The navigation you see is the navigation you upload. Adafruit SSD1306 output is identical pixel for pixel. With U8g2 positions and boxes match and text is approximated.

How the sketch is organised

PartWhat it does
menuTitle, menuStart, menuSize, menuParentEach menu's title, index of its first item, item count and parent menu. The tree is flattened into plain tables, no structs or pointers needed.
itemLabel, itemType, itemRefEach item's label, type and link: the submenu number, the value number or the action number.
values[]Current values of the number and toggle items. Read them straight from your own code, for example values[0] is the brightness.
drawMenu()Draws the screen. It is only called after a key press, so an idle loop() does not keep the I2C bus busy.
action_...()One empty function per action item. Switching a relay or starting a calibration goes here.
onValueChange()Called the moment a number or toggle changes. The right place to set a PWM output or save to EEPROM.

Buttons or a rotary encoder?

Three buttons are the simplest wiring: one leg of each button to its pin, the other leg to GND. The code turns on the internal pull-ups and applies a 30 ms debounce. A rotary encoder is nicer for long lists and wide number ranges because a quick turn covers ground fast, and its push button does the selecting. The encoder code polls a state table instead of using interrupts, so any two digital pins work. Long delay() calls inside loop() will make it miss clicks; use millis() if you need to wait.

Common problems

The screen stays dark. The code starts the display at 0x3C, the address of most modules. Some use 0x3D; change the address in the begin() line. Wiring is covered step by step in the Arduino OLED SSD1306 guide.

A column of noise along one edge. Most 1.3 inch modules use the SH1106 controller, which looks like this when driven by the SSD1306 library. Choose U8g2 as the library and SH1106 as the controller.

The menu scrolls by itself. A button pin is floating. Make sure the other leg of the button goes to GND; the code uses INPUT_PULLUP, so no external resistor is needed.

To add icons, draw them with the OLED bitmap generator and add a drawBitmap() line inside drawMenu(). For a different font, browse the U8g2 font list.

Frequently Asked Questions

How do I make a menu on an OLED display with Arduino?

Build the menu item by item on this page, pick the library (Adafruit SSD1306 or U8g2) and the input (3 buttons or a rotary encoder), then paste the code into the Arduino IDE. The sketch keeps the menu in tables and only redraws the screen when a key is pressed. Every action item gets an empty function where your own code goes.

Does the preview match the real screen?

With Adafruit SSD1306, yes, pixel for pixel. The preview is not a separate mock-up: the page runs the exact code it gives you in an in-browser Arduino interpreter and passes your key presses to that code's functions. With U8g2 the shapes and positions match, but text is drawn approximately with a 5x7 font; the real u8g2_font_6x10_tf is slightly wider.

How many items and levels can the menu have?

Up to 60 items and 3 levels (the main menu plus two levels of submenus). A 128x64 screen with a title bar shows 5 rows at a time; longer lists scroll and a scrollbar appears on the right. Labels are stored in RAM, so a very long menu can squeeze an Arduino Uno; move the strings to PROGMEM if that happens.

Why are accented letters replaced?

The built-in Adafruit GFX font and u8g2_font_6x10_tf only cover ASCII reliably. Arduino stores letters like é or ü as two UTF-8 bytes, which show up as junk symbols. The tool folds labels to ASCII where it can and flags the change in the messages. For accented text, create a custom font with the GFX font converter.

My rotary encoder runs backwards or skips two rows per click. What do I change?

If it runs backwards, swap the ENC_A and ENC_B pin numbers. If one click moves two rows, your encoder changes state twice per detent; lower ENC_STEPS in the code from 4 to 2. If it does nothing at all, check that the common pin (C) is wired to GND.

Are the settings lost when power is cut?

Yes, the values live in the values[] array in RAM. To keep them, save with EEPROM.put() inside onValueChange() and read them back with EEPROM.get() at the start of setup(). On an ESP32 the Preferences library does the same job.