Rotary encoder KY-040
endless knob + push button
A knob that turns endlessly in both directions, with a "click" at every step, and can be pressed. Perfect for menus, volume and setting values.
Open the course →How it works
Inside there are two contacts (CLK and DT) that open and close with a small time shift while you turn. If CLK falls while DT is HIGH, you are turning one way; if DT is LOW, the other way. This is called a quadrature signal. It is best read with an interrupt: the ESP32 reacts to the change immediately, and no click is lost.
Pins and wiring
| Pin | What it is | Where it connects / why |
|---|---|---|
| CLK (A) | Pulses, channel A | → GPIO (e.g. 18). |
| DT (B) | Pulses, channel B | → GPIO (e.g. 19). |
| SW | Button (to GND when pressed) | → GPIO (e.g. 23) with INPUT_PULLUP. |
| + | Supply | → 3V3 (NOT 5 V — the module pull-ups go to this pin). |
| GND | Ground | → GND. |
Specifications
Steps20 clicks per turn
Supply3.3 – 5 V
OutputsCLK, DT, SW
Pull-up10 kΩ on CLK and DT (on the module)
Tips and typical mistakes
- Jumps by 2 or back and forth? That is bounce — ignore changes faster than ~2 ms.
- Do not power it from 5 V: CLK/DT would then put 5 V into the ESP32.