PinLab › Components › Rotary encoder KY-040
CLKDTSW+GND turn = pulses, press = button

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

PinWhat it isWhere it connects / why
CLK (A)Pulses, channel A→ GPIO (e.g. 18).
DT (B)Pulses, channel B→ GPIO (e.g. 19).
SWButton (to GND when pressed)→ GPIO (e.g. 23) with INPUT_PULLUP.
+Supply→ 3V3 (NOT 5 V — the module pull-ups go to this pin).
GNDGround→ 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

Used in projects