Rotary Encoder (KY-040)
An endless knob that reports which way it turned, plus a push.
How it works
Unlike a potentiometer, a rotary encoder spins forever with no end stops. As you turn it, two pins (CLK and DT) output pulses slightly out of phase; comparing them tells you the direction. Most modules also have a push button (SW).
Read it by watching CLK for a change, then checking DT at that moment: if they differ, it's turning one way; if they match, the other. For snappy response use an interrupt on CLK.
Pins
- CLK
- Clock pulse — to a digital pin.
- DT
- Data pulse — to a digital pin.
- SW
- Push button — to a digital pin (INPUT_PULLUP).
- + / GND
- Power.
Ratings
- Output
- 2-bit quadrature (CLK + DT)
- Button
- Active-LOW
Tips
- Detect direction on a CLK edge by comparing CLK and DT.
- Encoders are noisy — debounce, or use hardware interrupts.