The KY-040 is a rotary encoder module designed for precise control of position and rotation. Unlike potentiometers, rotary encoders can rotate infinitely in either direction, making them ideal for applications requiring continuous or incremental adjustments. The module also includes a built-in push button, adding an extra layer of functionality for user input.
The KY-040 rotary encoder module has the following key specifications:
Parameter | Value |
---|---|
Operating Voltage | 3.3V to 5V |
Output Type | Digital (Incremental Encoder) |
Number of Pins | 5 |
Push Button | Integrated (Momentary Switch) |
Rotational Steps | 20 steps per full rotation |
Dimensions | 32mm x 19mm x 30mm (approximate) |
The KY-040 module has 5 pins, as described in the table below:
Pin | Label | Description |
---|---|---|
1 | GND | Ground connection for the module. |
2 | + | Power supply pin (3.3V to 5V). |
3 | SW | Push button output. Active LOW when the button is pressed. |
4 | DT | Data pin for the rotary encoder. Outputs pulses based on rotation direction. |
5 | CLK | Clock pin for the rotary encoder. Outputs pulses based on rotation movement. |
Connect the Pins:
GND
pin to the ground of your circuit.+
pin to a 3.3V or 5V power source.CLK
and DT
pins to digital input pins on your microcontroller.SW
pin to another digital input pin to use the push button.Read the Encoder Output:
CLK
and DT
pins generate pulses as the encoder is rotated. The sequence of these pulses determines the direction of rotation (clockwise or counterclockwise).SW
pin outputs a LOW signal when the button is pressed.Debounce Signals:
SW
, CLK
, and DT
pins if your microcontroller does not have internal pull-ups enabled.Below is an example Arduino sketch to read the KY-040 rotary encoder and push button:
// Define pin connections for the KY-040 module
#define CLK 2 // Clock pin connected to digital pin 2
#define DT 3 // Data pin connected to digital pin 3
#define SW 4 // Switch pin connected to digital pin 4
int counter = 0; // Variable to store the encoder position
int lastStateCLK; // Previous state of the CLK pin
int currentStateCLK; // Current state of the CLK pin
bool buttonPressed = false; // Flag for button press
void setup() {
pinMode(CLK, INPUT); // Set CLK pin as input
pinMode(DT, INPUT); // Set DT pin as input
pinMode(SW, INPUT_PULLUP); // Set SW pin as input with pull-up resistor
// Read the initial state of the CLK pin
lastStateCLK = digitalRead(CLK);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the current state of the CLK pin
currentStateCLK = digitalRead(CLK);
// Check if the state of CLK has changed
if (currentStateCLK != lastStateCLK) {
// Read the state of the DT pin
int stateDT = digitalRead(DT);
// Determine the rotation direction
if (stateDT != currentStateCLK) {
counter++; // Clockwise rotation
} else {
counter--; // Counterclockwise rotation
}
// Print the current counter value
Serial.print("Position: ");
Serial.println(counter);
}
// Update the last state of the CLK pin
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 triggers
}
} else {
buttonPressed = false; // Reset the flag when the button is released
}
}
No Output from the Encoder:
Unstable or Erratic Readings:
CLK
/DT
pins and ground to reduce noise.Push Button Not Responding:
SW
pin is connected to a digital input pin.Incorrect Direction Detection:
CLK
and DT
pins to correct the direction.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 matches the microcontroller's voltage.
Q: How many steps does the encoder have per rotation?
A: The KY-040 typically has 20 steps per full rotation, but this may vary slightly depending on the module.
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 feedback. You must track the position in software.