How-to · Beginner · 3 min read
Rotary Encoders
KY-040 Wiring, Code and Debouncing
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
A rotary encoder is a knob that turns endlessly and outputs two square-wave signals, A and B, a quarter step apart. Which one changes first tells the direction, and counting the changes gives how far it turned. The common KY-040 module has about 20 clicks per turn plus a push button. Unlike a potentiometer, it has no end stops, which makes it ideal for menus and volume controls.
Key specs at a glance
- Type
- Incremental, mechanical, quadrature (A and B)
- Steps
- Usually 20 detents and 20 pulses per turn (some 15 or 30)
- Pins (KY-040)
- CLK (A), DT (B), SW (button), + and GND
- Supply
- 3.3 to 5 V; module has 10 kΩ pull-ups
- Push switch
- Built-in, active LOW
- High resolution
- Optical encoders, hundreds to thousands of pulses per turn
How quadrature works
Inside are two contacts that open and close as the shaft turns, offset by a quarter of a step. Turning clockwise, A changes before B; anticlockwise, B changes before A. Reading B at the moment A falls tells the direction: if B is HIGH, it turned one way; if LOW, the other. Each click (detent) usually corresponds to one full cycle on the KY-040.
Wiring and code
Connect CLK and DT to two interrupt-capable pins (D2 and D3 on an Uno), SW to another pin with INPUT_PULLUP, + to 5 V or 3.3 V and GND to GND.
The easiest reliable option is the Encoder library by Paul Stoffregen: Encoder knob(2,3); long pos = knob.read()/4; (divide by 4 because it counts every edge). On ESP32, the ESP32Encoder library uses the hardware pulse counter, so no counts are missed even at high speed.
Bounce and missed steps
Mechanical contacts bounce, which shows up as counts jumping back and forth. Fixes, from simplest: 1. Use a library with a state-table decoder, which ignores invalid transitions. 2. Add 10 nF to 100 nF capacitors from CLK and DT to ground, which forms an RC filter with the 10 kΩ pull-ups. 3. Avoid delay() in the main loop if you poll rather than use interrupts.
For a menu, change one item per detent and use the push switch to select. For motor shaft speed and position, see motor encoders.
Try it: RC filter calculator
Change the values and the diagram and results update as you type.
Read a KY-040 rotary encoder with Arduino
You need: Arduino Uno, KY-040 rotary encoder module, 5 jumper wires · Tools: Computer with Arduino IDE
Get the parts3 of 3 found in our store · ₹676
Arduino UnoArduino Uno R3 Development Board with USB Cable₹385
KY-040 rotary encoder moduleM274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC₹46
5 jumper wiresDupont Jumper Wires (40 pcs)₹49
- 1Wire powerConnect + to 5V and GND to GND.
- 2Wire the signalsConnect CLK to D2, DT to D3 and SW to D4.
- 3Install the libraryInstall the Encoder library by Paul Stoffregen.
- 4UploadCreate Encoder knob(2,3) and print knob.read()/4 when it changes.
- 5TestTurn the knob both ways and press it; swap CLK and DT if the direction is reversed.
Common mistakes to avoid
- Using pins without interrupts and missing fast turns.
- Forgetting to divide the count by 4 (or 2) per detent.
- Long delay() calls in the loop when polling.
- No pull-up on the bare encoder's switch pin.
- Expecting an absolute position after power loss.
Frequently asked questions
What is the difference between a rotary encoder and a potentiometer?
A potentiometer gives an absolute position over a limited turn. An encoder turns endlessly and reports steps and direction.
Why does my encoder count go up and down randomly?
Contact bounce. Use a state-table library, add small capacitors to ground, or both.
Why does my encoder count 4 per click?
The library counts every edge of both signals. Divide by 4, or by 2 on some encoders.
Does the KY-040 work at 3.3 V?
Yes. Power it from 3.3 V so the pull-ups match ESP32 or Pico logic.
Does a rotary encoder remember its position?
No. Incremental encoders only report changes. Save the value in memory or use an absolute encoder.
Parts for this guide
Calculators
Terms in this guide
Projects that use it
Put this into practice: 5 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.


