KY-040 Rotary Encoder Module – 360° Incremental Encoder with Push Button for Arduino & ESP32
The KY-040 Rotary Encoder Module is a compact electromechanical input device designed to detect rotational movement, direction, and push-button presses in electronic projects.
Featuring a 360° continuous-rotation incremental rotary encoder with an integrated push button, the module generates two quadrature signals that allow a microcontroller to determine whether the shaft is rotating clockwise or counterclockwise.
Unlike a conventional potentiometer, the KY-040 does not have fixed minimum and maximum rotational positions. Instead, it generates digital pulses as the shaft rotates, making it ideal for navigating menus, adjusting parameters, selecting options, and controlling electronic devices.
The KY-040 is suitable for Arduino, ESP32, Raspberry Pi, robotics, LCD menus, motor control interfaces, audio equipment, and DIY electronics projects.
Key Features
-
Model: KY-040
-
Encoder Type: Mechanical Incremental Rotary Encoder
-
Rotation: 360° Continuous Rotation
-
Direction Detection: Clockwise and Counterclockwise
-
Output Signals: CLK and DT Quadrature Outputs
-
Integrated Push Button: SW
-
Typical Resolution: 20 Detents per Revolution (Variant Dependent)
-
Operating Logic: Compatible with 3.3V and 5V Microcontroller Interfaces
-
5-Pin Interface: CLK, DT, SW, +, GND
- Tactile Rotation Feedback
- Compact PCB Design
- Suitable for Arduino, ESP32, and Raspberry Pi Projects
Technical Specifications
| Parameter |
Specification |
| Model |
KY-040 |
| Product Type |
Rotary Encoder Module |
| Encoder Technology |
Mechanical Contact |
| Encoder Type |
Incremental Quadrature |
| Rotation Angle |
360° Continuous |
| Rotation Direction |
Bidirectional |
| Typical Detents |
20 per Revolution |
| Electrical Pulses |
Depends on Encoder Variant |
| Output Channels |
CLK (A), DT (B) |
| Push Button Output |
SW |
| Operating Interface |
Digital GPIO |
| Logic Voltage |
3.3V / 5V, Depending on Wiring |
| Number of Pins |
5 |
| Pin Configuration |
CLK / DT / SW / + / GND |
| Shaft Type |
Rotary Shaft with Push Action |
| Shaft Diameter |
Typically 6mm |
| Board Dimensions |
Approximately 26 × 19mm |
| Rotation Limit |
No Fixed End Stops |
| Absolute Position Measurement |
No |
| Communication Protocol |
Quadrature Digital Signals |
Pin Configuration
| Pin |
Function |
| CLK |
Encoder Channel A / Clock Signal |
| DT |
Encoder Channel B / Direction Signal |
| SW |
Integrated Push Button Output |
| + |
Positive Supply / Pull-Up Connection |
| GND |
Ground |
Note: The KY-040 is a mechanical contact encoder. Some versions include onboard pull-up resistors, while others rely on the microcontroller's internal pull-ups. Verify the supplied PCB before wiring.
How It Works
The KY-040 uses two mechanical encoder contacts to generate digital signals as the shaft rotates.
These signals are commonly labeled CLK and DT.
The signals change in a quadrature sequence, meaning one channel changes state before the other.
By monitoring the order of these transitions, a microcontroller can determine the direction of rotation.
Clockwise Rotation
One quadrature signal leads the other as the encoder rotates in one direction.
Counterclockwise Rotation
The signal sequence reverses when the shaft rotates in the opposite direction.
Push Button Function
The encoder shaft can also be pressed like a push button.
The SW pin changes state when the button is pressed, allowing the same control to be used for both rotation and selection.
Arduino Connection Example
| KY-040 |
Arduino Uno |
| CLK |
D2 |
| DT |
D3 |
| SW |
D4 |
| + |
5V |
| GND |
GND |
This connection allows the Arduino to detect rotational movement and push-button presses.
Arduino Example Code
const int clkPin = 2;
const int dtPin = 3;
const int swPin = 4;
int counter = 0;
int lastCLK;
void setup() {
pinMode(clkPin, INPUT_PULLUP);
pinMode(dtPin, INPUT_PULLUP);
pinMode(swPin, INPUT_PULLUP);
Serial.begin(9600);
lastCLK = digitalRead(clkPin);
}
void loop() {
int currentCLK = digitalRead(clkPin);
if (currentCLK != lastCLK && currentCLK == LOW) {
if (digitalRead(dtPin) != currentCLK) {
counter++;
Serial.print("Clockwise: ");
} else {
counter--;
Serial.print("Counterclockwise: ");
}
Serial.println(counter);
}
lastCLK = currentCLK;
if (digitalRead(swPin) == LOW) {
Serial.println("Button Pressed");
delay(200);
}
}
Expected Behavior
- Rotating the encoder clockwise increases the counter.
- Rotating it counterclockwise decreases the counter.
- Pressing the shaft displays a button-press message.
- The counter can be used to control menu selections, brightness, speed settings, or other parameters.
Important: The reported direction may be reversed depending on the module wiring and encoder construction. Mechanical contact bounce can also cause missed or extra counts. For reliable operation, use a rotary encoder library or a suitable debouncing algorithm.
Applications
- Arduino and ESP32 Projects
- Raspberry Pi User Interfaces
- LCD and OLED Menu Navigation
- Volume and Audio Controls
- Motor Speed Adjustment Interfaces
- Robotics Control Panels
- CNC and 3D Printer Control Interfaces
- Digital Parameter Adjustment
- Frequency Selection Interfaces
- Interactive Electronics
- DIY Instrumentation
- Educational STEM Projects
- Electronic Prototyping
Important Notes
-
The KY-040 is an incremental rotary encoder, not an absolute position encoder.
- It detects relative rotational movement rather than the shaft's absolute angle.
- It can rotate continuously without fixed end stops.
- The typical 20-detent resolution may vary between manufacturers.
- The number of electrical transitions per detent depends on the encoder model and decoding method.
- Mechanical contact bounce can produce additional transitions.
- Software debouncing or a dedicated encoder library is recommended for reliable counting.
- The integrated push button is a mechanical switch and may also require debouncing.
-
For ESP32 and Raspberry Pi, use a 3.3V-compatible wiring configuration and never apply 5V signals directly to their GPIO pins.
- The encoder does not directly control motor speed or position; a microcontroller must interpret its signals.
- The KY-040 is not designed for precision shaft position feedback in high-speed industrial motor control applications.
- For motor position feedback, consider a suitable optical, magnetic, or industrial encoder.
Package Includes
1 × KY-040 Rotary Encoder Module