KY-040 rotary encoder: pinout and wiring
The KY-040 rotary encoder turns endlessly and reports direction through the phase shift of two signals, CLK and DT. See the pinout, how to wire it to an Arduino, the code to count clicks and test the quadrature in the simulator.
Specifications
- Type
- Incremental quadrature encoder with a push button on the shaft
- Supply
- 3.3 V to 5 V
- Outputs
- CLK, DT and SW, with 10 kΩ pull-ups on the board
- Clicks per turn
- ≈ 20 (varies by model)
- Button
- Contact that pulls SW LOW when the shaft is pressed
- Absolute position
- No: it only counts changes
Pinout
The silkscreen reads CLK, DT, SW, + and GND. The pin order varies between makers: check your board before wiring.
KY-040 (5 pins)
| 1 | CLK | Quadrature output A |
| 2 | DT | Quadrature output B |
| 3 | SW | Shaft button (LOW when pressed) |
| 4 | + | Supply, 3.3 V or 5 V |
| 5 | GND | Ground |
Encoder simulator
Turn the shaft in quarter-click steps and watch the CLK and DT signals, the button and the count.
Last direction:
Each click goes through four CLK/DT states. Clockwise, CLK changes before DT; counter-clockwise, DT changes first. SW stays LOW while the shaft is pressed, because the board has a pull-up.
How to wire it
Connect + to 5 V and GND to ground. Connect CLK and DT to digital pins, preferably one with an interrupt (D2 or D3 on the Arduino Uno), and SW to another digital pin.
The board already has 10 kΩ pull-ups, so the pins sit HIGH at rest and go LOW when the contact closes. No external resistor is needed.
On each click the encoder goes through four CLK/DT states (00, 10, 11, 01 one way and the reverse the other). The direction comes from comparing the two signals: when CLK changes, if DT differs from CLK the turn is clockwise.
The shaft button is independent. Use INPUT_PULLUP or the board pull-up and handle bounce with a short delay.
Mechanical contacts bounce. If the count skips or doubles, filter with a 10 nF capacitor from each output to GND or add a minimum time between reads in code.
Example code (Arduino)
Sketch that counts clicks with an interrupt on CLK and reads the button (CLK = 2, DT = 3, SW = 4).
#define PIN_CLK 2
#define PIN_DT 3
#define PIN_SW 4
volatile int count = 0;
void readEncoder() {
if (digitalRead(PIN_CLK) == digitalRead(PIN_DT)) count--;
else count++;
}
void setup() {
Serial.begin(9600);
pinMode(PIN_CLK, INPUT);
pinMode(PIN_DT, INPUT);
pinMode(PIN_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_CLK), readEncoder, CHANGE);
}
void loop() {
static int last = 0;
if (count != last) {
last = count;
Serial.println(count);
}
if (digitalRead(PIN_SW) == LOW) {
Serial.println("Button");
delay(250);
}
}
Common mistakes
- Using digitalRead in a slow loop: with fast turning the states pass unread. Use an interrupt or read more often.
- Ignoring contact bounce: the count moves by two or flips direction. Filter in hardware or software.
- Expecting absolute position: the encoder only reports changes, and position is lost at power-off.
- Reading the button without considering the level: SW stays LOW for as long as the shaft is pressed, not just at the instant of the click.
- Mixing up the pin order: the silkscreen changes between modules. Check before powering.
Always check the manufacturer's datasheet for your exact model: pin order can vary between modules.
Frequently asked questions
How does the encoder show the rotation direction?
CLK and DT are two signals a quarter cycle apart. Which one changes first shows whether the turn is clockwise or counter-clockwise.
What is the difference between an encoder and a potentiometer?
A potentiometer has limited travel and gives a voltage proportional to position. An encoder turns endlessly and only sends change pulses, with no absolute position.
How many clicks per turn does the KY-040 have?
Usually 20, but it depends on the model. Each click is one full cycle of four CLK/DT states.
Does the KY-040 work with ESP32?
Yes. Power it from 3.3 V, connect CLK, DT and SW to input GPIOs and use interrupts or the ESP32Encoder library.