
Intermediate4/7/2026- Author: IoTSpark Maker
ESP32 + Rotary Encoder KY-040: Counting Rotation Direction (Quick Test Sample Code)
The KY-040 is a mechanical (incremental) rotary encoder, completely different from a potentiometer — it has no rotation limit (spins infinitely both ways) and outputs two 90°-out-of-phase square waves (a quadrature signal, on the CLK and DT pins).
ky-040rotary-encoderngat
4 steps2 components
The KY-040 is a mechanical rotary encoder (incremental encoder), completely different from a potentiometer — it has no rotation-angle limit (it spins infinitely in both directions), and outputs 2 square-wave signals 90° out of phase (called a quadrature signal, on the CLK and DT pins) to determine both how many steps it turned and which direction (clockwise/counter-clockwise). It usually also includes a push button (SW) triggered by pressing down on the shaft.