Component guide · Beginner · 3 min read
KY-040 / EC11 Rotary Encoder Module

Pinout, Wiring and Code
By Geekobot Learning Team · Updated 29 Oct 2026
On this page
The KY-040 is a rotary encoder module with a push button: turning the knob produces two square-wave signals (CLK and DT) whose order shows direction, and pressing it closes the SW switch. It has 20 detents per turn and spins endlessly, so it is ideal for menus, volume and setting values. Read it with interrupts for reliable counting.
Key specs at a glance
- Type
- Incremental mechanical encoder (EC11 style)
- Resolution
- 20 detents (pulses) per revolution on most modules
- Outputs
- CLK (A), DT (B) quadrature; SW push button
- Supply
- 3.3 to 5 V
- Rotation
- Continuous, no end stops
- Pull-ups
- 10 kΩ on CLK and DT (module dependent)
Models in this guide
These are alternatives. Pick the one that fits your project; you only need one.
Option 1M274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC₹46
Option 2EC11 Rotary Encoder Module with Knob Cap – 5-Pin Header₹93
Option 3Green Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V₹62
Option 4DFRobot Fermion EC11 Rotary Encoder Module Breakout – 360° Rotation, 3.3-5V₹389
What the KY-040 rotary encoder is
Inside the encoder, two contacts open and close in a fixed quadrature pattern as the shaft turns. If CLK changes before DT, the knob turned one way; if DT changes first, it turned the other way. Counting the changes gives position.
Unlike a potentiometer, it has no start or end, which makes it perfect for scrolling menus on an LCD or OLED.
KY-040 rotary encoder pinout
The pins on the KY-040 rotary encoder and what each one does.
| Pin | Function |
|---|---|
| CLK | Output A |
| DT | Output B |
| SW | Push button, pulls LOW when pressed |
| + | 3.3 to 5 V |
| GND | Ground |
- CLK
- Output A
- DT
- Output B
- SW
- Push button, pulls LOW when pressed
- +
- 3.3 to 5 V
- GND
- Ground
Wiring the KY-040 rotary encoder
Use interrupt pins for CLK and DT. On an ESP32 any GPIO can take an interrupt. Enable INPUT_PULLUP on SW because many modules have no pull-up on the button.
| KY-040 rotary encoder pin | Arduino Uno | ESP32 |
|---|---|---|
| CLK | D2 (interrupt) | GPIO 32 |
| DT | D3 (interrupt) | GPIO 33 |
| SW | D4 | GPIO 25 |
| + | 5V | 3V3 |
| GND | GND | GND |
- CLK
- Arduino Uno:
- D2 (interrupt)
- ESP32:
- GPIO 32
- DT
- Arduino Uno:
- D3 (interrupt)
- ESP32:
- GPIO 33
- SW
- Arduino Uno:
- D4
- ESP32:
- GPIO 25
- +
- Arduino Uno:
- 5V
- ESP32:
- 3V3
- GND
- Arduino Uno:
- GND
- ESP32:
- GND
Example code
The lookup table ignores contact bounce automatically, because a bounce produces an invalid transition that adds 0. Some modules give 2 edges per click; change the divider if each click counts double.
const int CLK = 2, DT = 3, SW = 4;
volatile int pos = 0;
volatile uint8_t last = 0;
void readEncoder() { // full quadrature decode
static const int8_t table[] = {0,-1,1,0, 1,0,0,-1, -1,0,0,1, 0,1,-1,0};
uint8_t now = (digitalRead(CLK) << 1) | digitalRead(DT);
pos += table[(last << 2) | now];
last = now;
}
void setup() {
Serial.begin(9600);
pinMode(CLK, INPUT_PULLUP); pinMode(DT, INPUT_PULLUP); pinMode(SW, INPUT_PULLUP);
last = (digitalRead(CLK) << 1) | digitalRead(DT);
attachInterrupt(digitalPinToInterrupt(CLK), readEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT), readEncoder, CHANGE);
}
void loop() {
static int shown = 0;
int detents = pos / 4; // 4 edges per click on most modules
if (detents != shown) { shown = detents; Serial.println(shown); }
if (digitalRead(SW) == LOW) { pos = 0; delay(200); }
}Setup and tuning
mechanical contacts bounce. If counts are jittery, add 10 nF capacitors from CLK and DT to GND.
for long value ranges, step by 10 when the knob turns fast.
for motors use an optical or magnetic encoder instead.
What you need
Shop this list5 of 6 found in our store · ₹729
KY-040 incremental rotary encoder module: the classic moduleM274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC₹46
EC11 rotary encoder module with knob cap: with knobEC11 Rotary Encoder Module with Knob Cap – 5-Pin Header₹93
Rotary encoder module 20 CPR: alternative boardGreen Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V₹62
DFRobot Fermion EC11 rotary encoder breakout: 3.3 to 5 V breakoutDFRobot Fermion EC11 Rotary Encoder Module Breakout – 360° Rotation, 3.3-5V₹389- Uno R3 development board: the controller used in the wiring tableFind this part
0.96 inch OLED display 128x64: to show readings without a computer0.96" OLED Display 128x64₹139Closest match, check the specs
Alternatives and when to choose them
- Potentiometers: absolute position with end stops, simplest to read.
- Rotary encoders guide: encoder types and resolution.
- Motor encoders: for measuring motor speed.
Try it: Encoder resolution calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Polling in a slow loop and missing steps.
- Forgetting INPUT_PULLUP on the SW pin.
- Counting every edge as one click, so each click moves 2 or 4 steps.
- Using it to measure a motor's precise angle.
Frequently asked questions
What is the difference between a rotary encoder and a potentiometer?
A potentiometer gives an absolute position over about 270 degrees; an encoder gives steps and direction and turns endlessly.
Why does my encoder skip or count backwards?
Contact bounce or polling too slowly. Use interrupts, the lookup-table method and small capacitors.
How many pulses per turn?
Most KY-040 modules have 20 detents per revolution.
Does it work with ESP32?
Yes, at 3.3 V on any GPIO with interrupts.
Do I need a library?
No, but libraries such as Encoder or ESP32Encoder simplify the code.
Can it read the knob position after power-off?
No. It only counts changes; save the value to EEPROM if you need it.
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.

