The KY-040 is a rotary encoder module designed for precise control of position and rotation. Unlike potentiometers, which provide an absolute position, the KY-040 generates incremental signals that allow users to track relative movement. It features a knob that can be rotated in either direction and a push-button switch for additional functionality. The module is widely used in applications requiring user input, such as volume control, menu navigation, and motor control.
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 Pulses per Revolution | 20 |
Push-Button Switch | Integrated |
Dimensions | 32mm x 19mm x 30mm |
The KY-040 module has 5 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 switch output (active LOW) |
4 | DT | Data signal (used to determine rotation direction when combined with CLK) |
5 | CLK | Clock signal (used to generate pulses during rotation) |
The KY-040 rotary encoder is easy to integrate into circuits and works seamlessly with microcontrollers like the Arduino UNO. Below are the steps to use the module effectively:
The following code demonstrates how to read the rotation direction and button press from the KY-040 module:
// Define pins for the KY-040 rotary encoder
#define CLK 2 // Clock pin
#define DT 3 // Data pin
#define SW 4 // Switch 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); // 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 rotation direction
if (stateDT != 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 the CLK pin
lastStateCLK = currentStateCLK;
// Check if the button is pressed
if (digitalRead(SW) == LOW) {
if (!buttonPressed) {
Serial.println("Button Pressed!");
buttonPressed = true;
}
} else {
buttonPressed = false;
}
}
INPUT_PULLUP
.No Response from the Encoder
Incorrect Rotation Direction
Button Not Working
Noisy or Erratic Readings
Can the KY-040 be used with 3.3V systems?
What is the resolution of the KY-040?
Can I use the KY-040 for absolute position tracking?
Is the KY-040 suitable for high-speed applications?