ESPBoards Learn – Early Access!

KY-040 Rotary Encoder Module

KY-040 Rotary Encoder Module image

View on Amazon

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

Get Your KY-040

KY-040 Rotary Encoder Module

Starting from

$2 per unit

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

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

Technical Specs

KY-040 Specifications

Complete technical specification details for KY-040 Rotary Encoder Module

📊 Technical Parameters

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
Pin Configuration

KY-040 Pinout

The **KY-040** is a 5-pin rotary encoder module with integrated push button:

Visual Pinout Diagram

Pinout Diagram Primary
KY-040 Rotary Encoder Module pinout
5
Total Pins

Pin Types

Power
2
Communication
2
Control
1

Quick Tips

🔌

**Interface**: Digital quadrature encoder (2-phase),🔄 **Rotation**: Continuous 360° rotation (no limits)

🎯

**Direction**: CLK and DT phase relationship determines rotation direction,🔘 **Button**: Built-in push-button switch (active low)

**Power**: 3.3V or 5V operation,💡 **Debouncing**: Software debouncing recommended for stable readings

Pin Descriptions

Pin NameTypeDescriptionNotes
1 CLK
Communication

Clock signal output

Pulses on rotation

2 DT
Communication

Data signal output

Phase-shifted pulses for direction detection

3 SW
Control

Switch/button signal

Active low when pressed

4 VCC
Power

Power supply

3.3V or 5V

5 GND
Power

Ground connection

Connection Guide

Wiring KY-040 to ESP32

To interface the **KY-040** with an **ESP32** for rotary input:

Pin Connections

KY-040 PinConnectionESP32 PinDescription
1 VCC Required
3.3V

Power supply

2 GND Required
GND

Ground

3 CLK Required
GPIO18

Clock input (any GPIO)

4 DT Required
GPIO19

Data input (any GPIO)

5 SW Optional
GPIO21

Button input (any GPIO)

💡

**Interrupt Pins**: Use interrupt-capable GPIO pins for best response

🔌

**GPIO Selection**: Any digital GPIO pins work, shown pins are examples

**Voltage**: Use 3.3V to avoid level shifting

🎯

**Software**: Implement debouncing and state machine for direction detection

🔘

**Button Optional**: SW pin not required if button functionality not needed

Help & Support

KY-040 Troubleshooting

Common issues and solutions to help you get your sensor working

Common Issues

Debugging Tips

Code Examples

KY-040 Programming Examples

Ready-to-use code examples for different platforms and frameworks

Summary

Wrapping Up KY-040

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.

Best Practices

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

Safety First

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

Ready to Start Building?

Now that you have all the information you need, it's time to integrate the KY-040 into your ESP32 project and bring your ideas to life!