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.
In this blog post, we will explore the functionality, applications, and code implementations of rotary encoders with the ESP32 microcontroller.
They are crucial for applications requiring precise control and feedback, making them essential components in various electronic devices and systems.
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.
Moreover, the resolution of a rotary encoder can significantly impact its performance. A higher resolution can yield finer control, which is necessary for applications like robotics where precision is crucial.
🧠 Key Features of Rotary Encoder
Understanding the key features of rotary encoders helps in selecting the right one for your projects. Here are the essential characteristics:
| 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 |
Understanding how rotary encoders work is essential for implementing them effectively in your projects. By converting rotational motion into electrical signals, they provide valuable data for microcontrollers like the ESP32 to process.
📦 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
Applications of rotary encoders extend beyond basic controls; they are integral in automation and robotics, where precise motor control and position feedback are necessary. For instance, in CNC machines, rotary encoders track the position of the spindle to ensure accurate machining.
- 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
Additionally, rotary encoders have become increasingly relevant in IoT applications, where they facilitate user interactions with various smart devices, enhancing user experience through intuitive controls.
Let’s delve deeper into the components required for setting up a basic rotary encoder project with the ESP32, ensuring we have everything ready for a successful implementation.
🧾 ESP32 Code With Rotary Encoder
🧾 ESP32 Code With Rotary Encoder
Now that we have the components ready, let’s explore the code required to operate the rotary encoder. The following code snippet demonstrates a basic implementation, providing the foundation for further enhancements.
❗ 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.

Properly wiring your components is crucial for optimal performance. Ensure your connections are secure and that your power supply meets the voltage requirements of both the ESP32 and the rotary encoder.
This code illustrates how the ESP32 responds to rotary movements and button presses, enabling it to function efficiently in various applications.
In conclusion, rotary encoders are versatile components that can significantly enhance your projects, especially when combined with powerful microcontrollers like the ESP32. By understanding their functionality, applications, and how to implement them in your projects, you can unlock a new level of control and interaction in your electronics.

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