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.
| 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) |
| 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 |
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.
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
ESP32 Dev Board
Rotary Encoder Module (KY-040 or similar)
Jumper Wires
Breadboard
π 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
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.