The KY-040 is a rotary encoder that detects rotational position and direction. Unlike a potentiometer, which provides an absolute position, the KY-040 outputs quadrature pulses that indicate relative movement. This makes it ideal for applications requiring precise control or incremental adjustments. Additionally, the KY-040 includes a built-in push button, expanding its functionality for user interface controls.
The KY-040 rotary encoder has the following key specifications:
Parameter | Value |
---|---|
Operating Voltage | 3.3V to 5V |
Output Type | Digital (Quadrature Pulses) |
Number of Pulses per Revolution | 20 |
Push Button Type | Momentary (Normally Open) |
Operating Temperature | -40°C to +85°C |
Dimensions | 19mm x 15mm x 29mm |
The KY-040 has 5 pins, as described in the table below:
Pin | Name | Description |
---|---|---|
1 | GND | Ground connection |
2 | + | Power supply (3.3V to 5V) |
3 | SW | Push button output (active LOW) |
4 | DT | Data signal (used for detecting rotation direction, works with CLK) |
5 | CLK | Clock signal (used for detecting rotation steps, works with DT) |
+
pin to a 3.3V or 5V power source and the GND
pin to ground.CLK
and DT
pins to digital input pins on your microcontroller.SW
pin to a digital input pin on your microcontroller.CLK
and DT
signals to detect rotation.SW
signal to detect button presses.SW
, CLK
, and DT
pins if your microcontroller does not have internal pull-ups.Below is an example of how to use the KY-040 with an Arduino UNO to detect rotation and button presses:
// Define pins for the KY-040 encoder
#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 lastCLKState; // To store the previous state of the CLK pin
int currentCLKState; // To store the current state of the CLK pin
int counter = 0; // Counter to track rotation steps
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
Serial.begin(9600); // Initialize serial communication
lastCLKState = digitalRead(CLK); // Read the initial state of the CLK pin
}
void loop() {
// Read the current state of the CLK pin
currentCLKState = digitalRead(CLK);
// Check if the CLK state has changed
if (currentCLKState != lastCLKState) {
// Determine rotation direction based on the DT pin state
if (digitalRead(DT) != currentCLKState) {
counter++; // Clockwise rotation
} else {
counter--; // Counterclockwise rotation
}
// Print the counter value to the serial monitor
Serial.print("Counter: ");
Serial.println(counter);
}
// Update the last CLK state
lastCLKState = currentCLKState;
// Check if the push button is pressed
if (digitalRead(SW) == LOW) {
Serial.println("Button Pressed!");
delay(200); // Debounce delay
}
}
CLK
and DT
pins are used to detect rotation direction and steps.SW
pin is used to detect button presses.No Response from the Encoder:
+
and GND
pins.CLK
and DT
pins are connected to the correct digital input pins on the microcontroller.Incorrect Rotation Direction:
CLK
and DT
pins to correct the direction.Button Not Detected:
SW
pin is connected properly.Noisy or Erratic Output:
CLK
and GND
pins and between the DT
and GND
pins.Q: Can the KY-040 be used with 3.3V microcontrollers like the ESP32?
A: Yes, the KY-040 operates at 3.3V to 5V, making it compatible with 3.3V microcontrollers.
Q: How many steps does the KY-040 encoder have per revolution?
A: The KY-040 has 20 steps per full revolution.
Q: Do I need external pull-up resistors for the KY-040?
A: If your microcontroller does not have internal pull-ups, you will need to add external pull-up resistors for the SW
, CLK
, and DT
pins.
Q: Can I use the KY-040 for absolute position sensing?
A: No, the KY-040 is a relative encoder and does not provide absolute position information.