"Rotary encoder to bar-graph LED" A bar that just happens to be curved?
Ask chat gpt for an idea, I did.
A rotary encoder to LED bar graph project is a common microcontroller application. As you rotate the encoder, more (or fewer) LEDs in a bar graph illuminate to indicate the current position.
>>Components
Arduino Uno, Nano, or compatible
Rotary encoder (e.g. KY-040)
10-segment LED bar graph
10 × 220 Ω resistors (if using a bare LED bar)
Breadboard and jumper wires
Example wiring
Rotary Encoder
CLK → D2
DT → D3
SW (optional) → D4
→ 5V
GND → GND
LED Bar Graph
Connect each LED through a 220 Ω resistor:
LED Arduino Pin
1 D5
2 D6
3 D7
4 D8
5 D9
6 D10
7 D11
8 D12
9 D13
10 A0
Arduino example
const int clkPin = 2;
const int dtPin = 3;
const int leds[10] = {5,6,7,8,9,10,11,12,13,A0};
int position = 0;
int lastCLK;
void setup() {
pinMode(clkPin, INPUT_PULLUP);
pinMode(dtPin, INPUT_PULLUP);
for(int i=0;i<10;i++) {
pinMode(leds, OUTPUT);
}
lastCLK = digitalRead(clkPin);
}
void loop() {
int currentCLK = digitalRead(clkPin);
if (currentCLK != lastCLK && currentCLK == LOW) {
if (digitalRead(dtPin) != currentCLK)
position++;
else
position--;
position = constrain(position, 0, 10);
for(int i=0;i<10;i++) {
digitalWrite(leds, i < position ? HIGH : LOW);
}
}
lastCLK = currentCLK;
}
How it works
Turning the encoder clockwise increments position.
Turning it counterclockwise decrements position.
position is limited between 0 and 10.
The first position LEDs are turned on, creating a growing/shrinking bar graph.
If you're using a 74HC595, MAX7219, TM1638, or an addressable LED strip (WS2812/NeoPixel) instead of directly wiring 10 LEDs, I can provide a version that uses those components and requires far fewer Arduino pins.
rotary encoder to led bar graph