

The KY-040 is a rotary encoder module that allows for precise control of position and rotation. Unlike a potentiometer, which provides an absolute position, the KY-040 outputs incremental signals, making it ideal for applications requiring relative position tracking. It features a built-in push button and provides two output signals (A and B) that can be used to determine the direction and amount of rotation.








The KY-040 rotary encoder module has the following key specifications:
| Parameter | Value |
|---|---|
| Operating Voltage | 3.3V to 5V |
| Output Type | Digital (Incremental) |
| Number of Pins | 5 |
| Push Button | Built-in |
| Rotational Steps | 20 steps per full rotation |
| Dimensions | 32mm x 19mm x 30mm |
The KY-040 module has five pins, as described in the table below:
| Pin | Label | Description |
|---|---|---|
| 1 | GND | Ground connection |
| 2 | + | Power supply (3.3V to 5V) |
| 3 | SW | Push button output (active LOW) |
| 4 | DT | Data signal (Channel B) |
| 5 | CLK | Clock signal (Channel A) |
+ pin to a 3.3V or 5V power source and the GND pin to ground.CLK (Channel A) and DT (Channel B) pins to digital input pins on your microcontroller.SW pin to a digital input pin if you want to use the push button.CLK and DT signals to determine the direction and amount of rotation.SW pin can be used to detect button presses.SW pin is not functioning as expected, ensure that a pull-up resistor is enabled in your microcontroller or added externally.Below is an example Arduino sketch to read the KY-040 rotary encoder and detect rotation direction and button presses:
// Define KY-040 pins
#define CLK 2 // Clock pin (Channel A)
#define DT 3 // Data pin (Channel B)
#define SW 4 // Push button pin
int lastStateCLK; // To store the previous state of the CLK pin
int currentStateCLK; // To store the current state of the CLK pin
int counter = 0; // Counter to track rotation
bool buttonPressed = false; // Flag for button press
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP); // Enable internal pull-up resistor for SW pin
// Read the initial state of the CLK pin
lastStateCLK = digitalRead(CLK);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the current state of the CLK pin
currentStateCLK = digitalRead(CLK);
// If the state of CLK has changed, a rotation has occurred
if (currentStateCLK != lastStateCLK) {
// Check the direction of rotation
if (digitalRead(DT) != currentStateCLK) {
counter++; // Clockwise rotation
} else {
counter--; // Counterclockwise rotation
}
// Print the counter value to the Serial Monitor
Serial.print("Counter: ");
Serial.println(counter);
}
// Update the last state of CLK
lastStateCLK = currentStateCLK;
// Check if the button is pressed
if (digitalRead(SW) == LOW) {
if (!buttonPressed) {
Serial.println("Button Pressed!");
buttonPressed = true; // Set the flag to avoid multiple prints
}
} else {
buttonPressed = false; // Reset the flag when the button is released
}
}
No Response from the Encoder:
CLK and DT pins are connected to the correct microcontroller pins.Incorrect Rotation Readings:
Push Button Not Working:
SW pin is connected to a digital input pin with a pull-up resistor enabled.Erratic or Unstable Readings:
CLK and GND pins, and between the DT and GND pins, to reduce noise.Q: Can the KY-040 be used with 3.3V microcontrollers like the ESP32?
A: Yes, the KY-040 is compatible with 3.3V systems. Ensure the power supply and signal levels match the microcontroller's requirements.
Q: How do I increase the resolution of the encoder?
A: The KY-040 has a fixed resolution of 20 steps per rotation. For higher resolution, consider using a different encoder with more steps.
Q: Can I use the KY-040 for absolute position tracking?
A: No, the KY-040 is an incremental encoder and does not provide absolute position information. It is best suited for relative position tracking.