Rotary Encoder with Esp32
- 23-07-2025
📌 What is a Rotary Encoder?
A rotary encoder is an electro-mechanical sensor that converts the angular position or motion of a rotating shaft into digital signals. Unlike a potentiometer, it can rotate infinitely in either direction and is commonly used in digital volume knobs, step selection, or menu navigation.
There are two main types:
- Incremental (most common)
- Absolute
The most widely used type with ESP32 is the incremental rotary encoder with a push-button switch.
🧠 Key Features of Rotary Encoder
| Feature | Details |
|---|---|
| Rotation | Continuous 360° (no end stops) |
| Signal Output | Digital (Quadrature: A and B) |
| Additional Button | Built-in push button (optional) |
| Resolution | Typically 20 pulses per rotation |
| Operating Voltage | 3.3V to 5V |
| Durability | Long life (up to 100,000 rotations) |
⚙️ Pinout of Rotary Encoder
| Pin | Name | Description |
|---|---|---|
| CLK | Output A | Channel A (primary output) |
| DT | Output B | Channel B (direction control) |
| SW | Switch | Push-button (active LOW) |
| VCC | Power | 3.3V or 5V input |
| GND | Ground | Common ground |
📦 How It Works
A rotary encoder generates two square wave signals (CLK and DT) slightly out of phase. By comparing the timing between these signals, the ESP32 can determine the direction of rotation:
- Clockwise (CW): CLK leads DT
- Counterclockwise (CCW): DT leads CLK
The SW pin is a built-in push button often used to select or confirm actions.
🧪 Applications of Rotary Encoder
- Digital volume knobs
- Menu navigation in embedded systems
- Motor position tracking
- CNC machines and robotics
- Media players and audio mixers
- IoT interfaces and control panels
🔗 Basic Example with ESP32
✅ Components Needed
- ESP32 Dev Board
- Rotary Encoder Module (KY-040 or similar)
- Jumper Wires
- Breadboard
📍 Where to Use a Rotary Encoder
- 🔊 Digital volume control for audio systems
- 📻 Tuning knobs in radios and receivers
- 📋 Menu navigation in embedded systems (LCD/OLED)
- 🔁 Infinite scrolling inputs (brightness, time, settings)
- 🧭 Direction and position control in robotics
🧾 ESP32 Code With Rotary Encoder
HOW TO OPERATE
#define CLK 19
#define DT 18
#define SW 21
int counter = 0;
int lastStateCLK;
bool currentStateCLK;
bool currentStateSW;
bool lastStateSW = HIGH;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
Serial.begin(115200);
lastStateCLK = digitalRead(CLK);
}
void loop() {
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
counter++;
} else {
counter--;
}
Serial.print("Counter: ");
Serial.println(counter);
}
lastStateCLK = currentStateCLK;
currentStateSW = digitalRead(SW);
if (currentStateSW == LOW && lastStateSW == HIGH) {
Serial.println("Button Pressed");
delay(50);
}
lastStateSW = currentStateSW;
delay(1);
}❗ Important Notes
- Debouncing: Mechanical rotary encoders may need software debouncing or filtering for smooth reading.
- Interrupts: For better performance and smoother rotation, use hardware interrupts instead of polling.
- Pull-ups: Some modules have built-in pull-up resistors; if not, use
INPUT_PULLUP. - Power Compatibility: Works on both 3.3V and 5V logic—perfect for ESP32.
- Wiring Tip: Keep wires short to reduce noise.


Leave a Reply
You must be logged in to post a comment.