The KY-040 rotary encoder is a mechanical device used to measure angular position or rotation. It is commonly used in volume controls, menu navigation, and position tracking in robotics. This tutorial will guide you through connecting and using the KY-040 rotary encoder with Arduino.
What You Will Need
- KY-040 Rotary Encoder Module
- Arduino Board (e.g., Uno, Mega, Nano)
- Breadboard and Jumper Wires
- A computer with the Arduino IDE installed
Step 1: Understanding the KY-040 Rotary Encoder
The KY-040 encoder consists of a rotary dial that generates pulses when rotated. It also includes a built-in push button.
KY-040 Pinout
Pin | Function |
---|---|
GND | Ground |
+ | Power Supply (5V) |
CLK | Clock Pulse Signal |
DT | Data Pulse Signal |
SW | Push Button Signal |
Step 2: Wiring the KY-040 to Arduino
Here’s how to connect the KY-040 to an Arduino Uno:
KY-040 Pin | Arduino Pin |
---|---|
GND | GND |
+ | 5V |
CLK | Pin 2 |
DT | Pin 3 |
SW | Pin 4 |
Step 3: Upload the Code
The following example code reads the rotary encoder’s rotation and detects button presses:
#define CLK 2 // Clock pin
#define DT 3 // Data pin
#define SW 4 // Switch pin
int counter = 0; // Counter to track rotation
int currentStateCLK;
int lastStateCLK;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP); // Internal pull-up for the button
Serial.begin(9600);
// Read the initial state of the CLK pin
lastStateCLK = digitalRead(CLK);
Serial.println("KY-040 Rotary Encoder Test");
}
void loop() {
// Read the current state of the CLK pin
currentStateCLK = digitalRead(CLK);
// If the state has changed, the encoder has rotated
if (currentStateCLK != lastStateCLK) {
// Check the DT pin to determine the rotation direction
if (digitalRead(DT) != currentStateCLK) {
counter++;
} else {
counter--;
}
Serial.print("Counter: ");
Serial.println(counter);
}
// Save the last state of CLK
lastStateCLK = currentStateCLK;
// Check if the button is pressed
if (digitalRead(SW) == LOW) {
Serial.println("Button pressed!");
delay(200); // Debounce delay
}
}
Step 4: Test the Setup
- Connect the Arduino to your computer via USB.
- Open the Arduino IDE and select the correct Board and Port under the Tools menu.
- Upload the code by clicking Upload.
- Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to
9600
. - Rotate the encoder and press the button. Observe the counter and button press messages in the Serial Monitor.
Applications of the KY-040 Rotary Encoder
- Volume or brightness control
- Menu navigation
- Motor position tracking
- Robotics and automation systems
Troubleshooting
- No rotation detection: Check the CLK and DT wiring and ensure the pins match the code.
- Unstable readings: Add a small debounce delay to stabilize the encoder signals.
-
Button not detected: Verify the SW pin is properly connected and uses
INPUT_PULLUP
mode.
Conclusion
You’ve successfully interfaced the KY-040 rotary encoder with Arduino. This versatile module enables you to add rotation tracking and button functionality to your projects. Experiment with it in different applications, like building custom controls or navigating menus in your systems!