ESP32 KY-040 Rotary Encoder Module

KY-040 Rotary Encoder Module image

KY-040

Protocol: Digital

Overview

The KY-040 is a rotary encoder module that provides digital signals corresponding to the rotational position and direction. It features continuous 360-degree rotation and includes a built-in push-button switch, making it suitable for various control applications.

About KY-040 Rotary Encoder Module

The KY-040 Rotary Encoder Module is a rotary input device that provides information about the amount and direction of rotation. Unlike potentiometers, which have limited rotation angles, the KY-040 can rotate continuously, making it ideal for applications requiring precise control, such as volume adjustments, menu navigation, and motor speed control. The module also features a built-in push-button switch, adding an extra layer of functionality.

Where to Buy

KY-040 Rotary Encoder Module

Starting from

$2 per unit

Amazon com
Amazon.com Ships worldwide
View Price
Amazon de
Amazon.de Ships to EU
View Price
Aliexpress
AliExpress Best value
View Price

Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.

Technical Specifications

Operating Voltage 3.3V to 5V
Pulses per Revolution 20
Output 2-bit Gray Code
Mechanical Angle 360° Continuous
Built-in Switch Yes (Push-to-Operate)
Dimensions 30mm x 18mm x 30mm

Pinout Configuration

The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32.

  • CLK (Clock): Outputs pulses corresponding to the rotation; connect to a digital input pin on the microcontroller.
  • DT (Data): Outputs pulses used in conjunction with CLK to determine rotation direction; connect to a digital input pin on the microcontroller.
  • SW (Switch): Connected to the built-in push-button; connect to a digital input pin on the microcontroller.
  • VCC: Connects to the power supply, typically 3.3V or 5V.
  • GND: Connects to the ground of the circuit.

Wiring with ESP32

  • VCC: Connect to ESP32 3.3V.
  • GND: Connect to ESP32 GND.
  • CLK: Connect to ESP32 digital input pin (e.g., GPIO18).
  • DT: Connect to ESP32 digital input pin (e.g., GPIO19).
  • SW: Connect to ESP32 digital input pin (e.g., GPIO21).

Troubleshooting Guide

Common Issues

❌ Unexpected Behavior or No Response

⚠️ Incorrect Direction Detection

Debugging Tips

🔍 Serial Monitor

⚡ Voltage Checks

Code Examples

Arduino Example

C++
🔧

ESP-IDF Example

C++
🏠

ESPHome Example

YAML
🛠️

PlatformIO Example

C++
🐍

MicroPython Example

Python

Conclusion

The ESP32 KY-040 Rotary Encoder Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.

For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.

Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.