Rotary encoder settings box
A rotary encoder with a push button scrolls through a menu on an OLED, the basis of settings screens on many gadgets.
Build it step by step
0 of 8 doneInstall the libraries
Adafruit SSD1306 and Adafruit GFX Library.
Wire the encoder
CLK to D2, DT to D3, SW to D4, plus and GND.
Wire the OLED and the LED
OLED on A4 and A5, LED on D5 through 220 ohm.
Upload the sketch
The menu shows with a cursor on Brightness.
Turn the knob
The cursor moves between the three items.
Press on Brightness and turn
The LED brightness changes with the value.
Press again to save
The star turns back to a cursor.
Unplug and replug USB
Your settings are still there.
How do you build a rotary encoder settings box?
A rotary encoder with a push button scrolls through a menu on an OLED, the basis of settings screens on many gadgets. You need Arduino Nano, Rotary encoder module (KY-040), SSD1306 OLED Display, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- DisplaysBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| KY-040 CLK | Nano D2 | Interrupt pin, falling edge |
| KY-040 DT | Nano D3 | Direction |
| KY-040 SW | Nano D4 | Push button, pressed reads LOW |
| KY-040 plus and GND | Nano 5V and GND | The module has its own pull-up resistors |
| OLED SDA and SCL | Nano A4 and A5 | I2C, address 0x3C |
| Red LED plus through 220 ohm | Nano D5 | Shows the brightness setting |
| Red LED minus | Nano GND | Ground |
Code
A rotary encoder has two switches inside that open and close slightly out of step with each other as the shaft turns. When the CLK line falls, the level of the DT line tells you which way it turned: high means clockwise, low means anticlockwise. The sketch counts turns in an interrupt service routine so none are missed while the OLED redraws, and it ignores edges closer than two milliseconds to hide contact bounce. The menu has three levels in a simple state machine: scrolling the list, editing a value, and a reset action. Pressing the button on Brightness or Blink enters edit mode, turning changes the value, and pressing again saves it to the Nano EEPROM, which keeps the data when power is removed. A marker byte at address 0 tells the sketch the saved data is valid on first power-up. The LED shows the brightness and blink choices live. Install Adafruit SSD1306 and Adafruit GFX from the Library Manager.
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 <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t PIN_CLK = 2, PIN_DT = 3, PIN_SW = 4, PIN_LED = 5;
volatile int8_t steps = 0; // turns counted by the interrupt
volatile unsigned long lastEdge = 0;
void onTurn() {
unsigned long now = micros();
if (now - lastEdge < 2000) return; // ignore contact bounce under 2 ms
lastEdge = now;
steps += (digitalRead(PIN_DT) == HIGH) ? 1 : -1;
}
// Settings saved in EEPROM: brightness 0..10, blink mode 0..2.
uint8_t brightness = 5;
uint8_t blinkMode = 0; // 0 steady, 1 slow, 2 fast
const char* BLINK_NAME[3] = {"Steady", "Slow", "Fast"};
const char* ITEMS[3] = {"Brightness", "Blink", "Reset defaults"};
uint8_t cursorItem = 0;
bool editing = false;
void loadSettings() {
if (EEPROM.read(0) == 0x5A) { // marker says the data is valid
brightness = min(EEPROM.read(1), (uint8_t)10);
blinkMode = min(EEPROM.read(2), (uint8_t)2);
}
}
void saveSettings() {
EEPROM.update(0, 0x5A);
EEPROM.update(1, brightness);
EEPROM.update(2, blinkMode);
}
void draw() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print(editing ? "Edit value" : "Settings");
for (uint8_t i = 0; i < 3; i++) {
oled.setCursor(0, 16 + i * 14);
oled.print(i == cursorItem ? (editing ? '*' : '>') : ' ');
oled.print(' ');
oled.print(ITEMS[i]);
if (i == 0) { oled.print(": "); oled.print(brightness); }
if (i == 1) { oled.print(": "); oled.print(BLINK_NAME[blinkMode]); }
}
oled.display();
}
void updateLed() {
unsigned long period = (blinkMode == 1) ? 1000 : 300;
bool on = (blinkMode == 0) || ((millis() % period) < period / 2);
analogWrite(PIN_LED, on ? brightness * 25 : 0);
}
void setup() {
pinMode(PIN_CLK, INPUT_PULLUP);
pinMode(PIN_DT, INPUT_PULLUP);
pinMode(PIN_SW, INPUT_PULLUP);
pinMode(PIN_LED, OUTPUT);
attachInterrupt(digitalPinToInterrupt(PIN_CLK), onTurn, FALLING);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
loadSettings();
draw();
}
void loop() {
noInterrupts(); int8_t d = steps; steps = 0; interrupts();
bool changed = false;
if (d != 0) {
if (!editing) {
cursorItem = (cursorItem + 3 + (d > 0 ? 1 : -1)) % 3;
} else if (cursorItem == 0) {
brightness = constrain((int)brightness + d, 0, 10);
} else if (cursorItem == 1) {
blinkMode = (blinkMode + 3 + (d > 0 ? 1 : -1)) % 3;
}
changed = true;
}
static bool lastSw = HIGH;
bool sw = digitalRead(PIN_SW);
if (lastSw == HIGH && sw == LOW) { // button pressed
if (cursorItem == 2) { brightness = 5; blinkMode = 0; saveSettings(); }
else { if (editing) saveSettings(); editing = !editing; }
changed = true;
delay(30);
}
lastSw = sw;
if (changed) draw();
updateLed();
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Turn clockwise | Cursor moves down |
| Turn anticlockwise | Cursor moves up |
| Edit brightness | LED dims and brightens with the value |
| Set Blink to Fast | LED blinks about three times a second |
| Press Reset defaults | Brightness 5 and Steady, saved |
If it does not work
Menu moves the wrong way
Swap the CLK and DT wires, or change the sign in onTurn.
Steps skip or double
Increase the 2 ms bounce limit, or add a 10 nanofarad capacitor from CLK to GND.
Display blank
Try address 0x3D, and check the display voltage.
Settings do not survive power-off
Check saveSettings is called when you press the knob to finish editing.
LED never lights
Check the LED direction and that brightness is above 0.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the rotary encoder settings box?
Arduino Nano, Rotary encoder module (KY-040), SSD1306 OLED Display, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the rotary encoder settings box take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the rotary encoder settings box works?
Turn clockwise: Cursor moves down. Turn anticlockwise: Cursor moves up. Edit brightness: LED dims and brightens with the value.
Menu moves the wrong way: what should I check?
Swap the CLK and DT wires, or change the sign in onTurn.
Steps skip or double: what should I check?
Increase the 2 ms bounce limit, or add a 10 nanofarad capacitor from CLK to GND.
Display blank: what should I check?
Try address 0x3D, and check the display voltage.
Settings do not survive power-off: what should I check?
Check saveSettings is called when you press the knob to finish editing.
