Projects / Rotary encoder menu on an OLED

Rotary encoder menu on an OLED

Intermediate1 hGuided buildReviewed

Build it step by step

0 of 4 done
  1. NANO

    Wire the encoder

    CLK to D2, DT to D3, SW to D4, + to 5V, GND.

  2. NANO16.1 mASSD1306

    Wire the OLED

    SDA A4, SCL A5, 5V, GND.

  3. NANO16.1 mA

    Add the outputs

    Blue LED with 220 Ω on D9, buzzer on D8.

  4. NANO

    Upload

    Turn the knob to move the >, press to edit (* appears), turn to change, press again to leave.

In short

How do you build a rotary encoder menu on an OLED?

Turn a KY-040 rotary encoder to move through a menu on an OLED and press it to change settings like LED brightness and beep on or off. You need Arduino Nano, Rotary encoder module (KY-040), SSD1306 OLED Display, LED 5 mm blue, Resistor 220 Ω, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 4 steps and about 60 minutes.

Level
Intermediate
Time
About 60 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Encoder CLK / DT / SWNano D2 / D3 / D4Rotation (D2 has an interrupt) and push button
Encoder + / GNDNano 5V / GNDPull-ups on the module
OLED SDA / SCL / VCC / GNDNano A4 / A5 / 5V / GNDMenu display
Nano D9220 Ω, blue LED, GNDBrightness demo (PWM pin)
Nano D8Buzzer + (− to GND)Key beep

Code

Arduino IDE, board "Arduino Nano". Install "Adafruit SSD1306" and "Adafruit GFX".

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int CLK = 2, DT = 3, SW = 4, LED = 9, BUZZ = 8;
volatile int turns = 0;
const char* ITEMS[] = {"Brightness", "Beep", "About"};
int cursor = 0, brightness = 128;
bool editing = false, beep = true;

void onTurn() { turns += digitalRead(DT) != digitalRead(CLK) ? 1 : -1; }

void draw() {
  oled.clearDisplay(); oled.setTextSize(1);
  for (int i = 0; i < 3; i++) {
    oled.setCursor(0, i * 14);
    oled.print(i == cursor ? (editing ? "* " : "> ") : "  ");
    oled.print(ITEMS[i]);
    oled.setCursor(90, i * 14);
    if (i == 0) { oled.print(brightness * 100 / 255); oled.print("%"); }
    if (i == 1) oled.print(beep ? "On" : "Off");
  }
  if (cursor == 2 && editing) { oled.setCursor(0, 50); oled.print("ElectroLark menu v1"); }
  oled.display();
}

void setup() {
  pinMode(CLK, INPUT); pinMode(DT, INPUT); pinMode(SW, INPUT_PULLUP);
  pinMode(LED, OUTPUT); pinMode(BUZZ, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(CLK), onTurn, FALLING);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); oled.setTextColor(SSD1306_WHITE);
  analogWrite(LED, brightness); draw();
}

void loop() {
  noInterrupts(); int t = turns; turns = 0; interrupts();
  if (t) {
    if (!editing) cursor = constrain(cursor + t, 0, 2);
    else if (cursor == 0) { brightness = constrain(brightness + t * 16, 0, 255); analogWrite(LED, brightness); }
    else if (cursor == 1) beep = !beep;
    draw();
  }
  if (digitalRead(SW) == LOW) {
    delay(30); while (digitalRead(SW) == LOW) {}
    editing = !editing;
    if (beep) tone(BUZZ, 2000, 30);
    draw();
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Turning one clickThe cursor moves one line
Editing BrightnessThe LED gets brighter or dimmer in 16 steps
Editing BeepToggles On and Off, and presses beep only when On

If it does not work

Cursor moves the wrong way

Swap CLK and DT, or change +1 and -1 in onTurn.

Jumps two lines per click

Your encoder gives two edges per detent. Divide t by 2.

Erratic jumps

Add 100 nF capacitors from CLK and DT to GND to smooth contact bounce.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the rotary encoder menu on an OLED?

Arduino Nano, Rotary encoder module (KY-040), SSD1306 OLED Display, LED 5 mm blue, Resistor 220 Ω, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the rotary encoder menu on an OLED take to build?

About 60 minutes for a intermediate maker, in 4 steps.

How do I know the rotary encoder menu on an OLED works?

Turning one click: The cursor moves one line. Editing Brightness: The LED gets brighter or dimmer in 16 steps. Editing Beep: Toggles On and Off, and presses beep only when On.

Cursor moves the wrong way: what should I check?

Swap CLK and DT, or change +1 and -1 in onTurn.

Jumps two lines per click: what should I check?

Your encoder gives two edges per detent. Divide t by 2.

Erratic jumps: what should I check?

Add 100 nF capacitors from CLK and DT to GND to smooth contact bounce.