KY-040 Rotary Encoder Module
Components

KY-040 Rotary Encoder Module

0.0(0)
The KY-040 Rotary Encoder Module is a digital knob that turns endlessly, counts one step per click and works as a push button when its shaft is pressed. Unlike a potentiometer, it reports how far and in which direction it was turned instead of an absolute position; one turn usually has 20 clicks. As the shaft turns, the CLK and DT pins produce two square waves shifted 90 degrees from each other, and the direction comes from which one changes first. The module has 5 pins (CLK, DT, SW, + and GND), runs on 3.3–5 V and has pull-up resistors on CLK and DT. In a typical setup, + goes to 5 V (3.3 V on ESP32 and Pico), CLK and DT to two interrupt-capable pins (D2 and D3 on the Arduino Uno) and SW to a pin set as INPUT_PULLUP. Because the contacts bounce, debouncing or interrupts prevent missed clicks. It is used for LCD and OLED menus, volume and brightness control, counters and stepper motor position control.
KY-040 Rotary Encoder Module: Arduino Wiring, Code & Pinout