KY-040 Rotary Encoder
A KY-040 incremental rotary encoder with quadrature CLK and DT outputs plus a push switch, for endless knob input.
- Supply voltage5 V
- Maximum supply voltage5 V
- InterfaceQUADRATURE
- Dimensions18 × 30 × 30 mm

The KY-040 is an incremental rotary encoder module with a built-in push switch: turning the shaft produces two quadrature signals on CLK and DT, and pressing it pulls SW to ground. Wire + to 5V (Joy-IT wires it to 3.3V on a Raspberry Pi), GND to GND, and CLK, DT and SW to three digital pins (Joy-IT KY-040 sensor kit documentation). To read direction, check DT each time CLK changes: if DT differs from CLK the knob turned clockwise, otherwise counter-clockwise (Joy-IT KY-040 sensor kit documentation).
The KY-040 is an incremental rotary encoder on a small breakout board. Unlike a potentiometer, it turns continuously with no end stops and reports change rather than an absolute position, so it is ideal for scrolling through menus or adjusting a value up and down without limits.
It has five pins: CLK, DT, SW, plus + (VCC) and GND. Rotating the shaft produces two square waves on CLK and DT that are 90 degrees out of phase (quadrature). Which output changes first tells you the direction, and counting the steps tells you how far it turned. The shaft clicks through detents, and one step per detent is the usual way to count.
Pressing the shaft closes a momentary switch on the SW pin, giving a built-in button for select or confirm actions. The board runs from 5V, measures 18 x 31 x 30 mm, and mounts through a panel with an M7 x 0.75 threaded bushing.
KY-040 Rotary Encoder pinout
| Pin | Function | Type |
|---|---|---|
| CLK | Digital5 V | output |
| DT | Digital5 V | output |
| SW | Digital5 V | output |
| VCC | VCC (+)5 V | power in |
| GND | GND | power in |
How to wire the KY-040 Rotary Encoder
Pin by pin, for the board you have.
| KY-040 Rotary Encoder | Arduino Uno | Note |
|---|---|---|
| CLK | D2 | |
| DT | D3 | |
| SW | D4 | |
| VCC | 5V | |
| GND | GND |
| KY-040 Rotary Encoder | ESP32 DevKit V1 | Note |
|---|---|---|
| CLK | GPIO13 (D13) | |
| DT | GPIO14 (D14) | |
| SW | GPIO27 (D27) | |
| VCC | VIN | |
| GND | GND |
- VCC is on VIN, which gives about 5 V only while the board is powered from USB.
- CLK outputs 5 V into GPIO13 (D13), a 3.3 V input: use a divider or level shifter.
- DT outputs 5 V into GPIO14 (D14), a 3.3 V input: use a divider or level shifter.
- SW outputs 5 V into GPIO27 (D27), a 3.3 V input: use a divider or level shifter.
| KY-040 Rotary Encoder | Raspberry Pi Pico | Note |
|---|---|---|
| CLK | GP0 | |
| DT | GP1 | |
| SW | GP2 | |
| VCC | VBUS | |
| GND | GND |
- VCC is on VBUS, which gives about 5 V only while the board is powered from USB.
- CLK outputs 5 V into GP0, a 3.3 V input: use a divider or level shifter.
- DT outputs 5 V into GP1, a 3.3 V input: use a divider or level shifter.
- SW outputs 5 V into GP2, a 3.3 V input: use a divider or level shifter.
Before you wire it
- VCC (+) must connect to 5V; GND must connect to GND.
- CLK and DT must each connect to a digital pin.
- POLL CLK and DT with digitalRead() inside loop(). A mechanical detent bounces for a few ms, so an interrupt on CLK fires several times per click and over-counts unless it is debounced in the ISR. Polling works the same on every board and needs no interrupt-capable pin, which on an Uno is only D2/D3.
- Count ONE step per detent on the CLK falling edge: when CLK goes HIGH→LOW, read DT, if DT != CLK → clockwise (count++), else counter-clockwise (count, ). (Counting on every edge double-counts each detent.)
- SW (push switch) connects to a digital pin with pinMode(pin, INPUT_PULLUP): ACTIVE-LOW (pressed reads LOW). SW is the only pin that needs INPUT_PULLUP.
- No undebounced attachInterrupt() on CLK: contact bounce fires it several times per detent.
- No INPUT_PULLUP on CLK/DT: the module already pulls them up; only SW needs it.
- No reading only CLK: without DT you cannot tell clockwise from counter-clockwise.
- No expecting absolute position: the encoder is incremental (counts steps/detents).
- Power + from 5V on an Arduino (3.3V on a Raspberry Pi or other 3.3V board, so the outputs match its logic) and share a common ground. Connect CLK, DT, and SW to three digital inputs.
- Read direction by sampling DT at the moment CLK changes: if DT differs from CLK the shaft turned clockwise, otherwise counter-clockwise. Count one step per detent on one CLK edge, since counting every edge double-counts each click.
- The board pulls CLK and DT up; SW switches to ground with no pull-up of its own, so enable the microcontroller's internal pull-up on the SW pin (pressed reads LOW).
- Debounce the signals. The mechanical contacts bounce for a few milliseconds, so poll the pins often in loop() or add software or RC debouncing; an undebounced interrupt on CLK fires several times per click.
Specifications
- Supply voltage
- 5 V
- Maximum supply voltage
- 5 V
- Interface
- QUADRATURE
- Dimensions
- 18 × 30 × 30 mm
- Weight
- 8 g
From the maker's datasheet.
Common mistakes
No debouncing on CLK and DT
The contacts bounce as they change, so one physical click can be counted as several steps or jump direction. Add an RC filter or software debounce to get one count per detent.
Polling too slowly
If the loop is busy and does not read the pins often enough, fast turns skip counts. Keep loop() free of long delays so CLK and DT are sampled often; if you use an interrupt, debounce it inside the handler.
Treating it like a potentiometer
An incremental encoder has no absolute position and no analog output. Reading it on an analog pin gives nothing useful; it must be decoded from the two digital quadrature signals.
Forgetting the button needs a pull-up
The SW pin is a switch to ground with no pull-up on the board. Without one it floats and reports random presses, so enable INPUT_PULLUP on that pin and debounce it.
KY-040 Rotary Encoder questions
Does the KY-040 work with 3.3V boards like the ESP32?
Yes. It is a passive switch encoder, so power + from the board's 3.3V pin and the CLK, DT and SW levels match its logic; Joy-IT wires it this way to a Raspberry Pi (Joy-IT KY-040 sensor kit documentation). On a 5V Arduino, power it from 5V.
How do I tell which way the KY-040 is turning?
CLK and DT are two square waves 90 degrees apart. When CLK changes, read DT: if DT differs from CLK the shaft turned clockwise, and if they match it turned counter-clockwise (Joy-IT KY-040 sensor kit documentation).
Why does my KY-040 skip steps or count twice per click?
Double counts come from counting every edge or from contact bounce: count one step per detent on a single CLK edge and debounce. Skipped steps come from reading too slowly, so keep long delay() calls out of loop() or debounce an interrupt handler.
Do I need pull-up resistors for the KY-040?
CLK and DT are pulled up on the board, so set them as plain INPUT. The SW button has no board pull-up, so use INPUT_PULLUP on its pin; it reads LOW while pressed.
KY-040 vs a potentiometer: which should I use?
A potentiometer gives an absolute position over a fixed turn, read on one analog pin. The KY-040 turns endlessly and reports steps and direction on two digital pins, plus a push button, which suits menus and values with no fixed end.
Build something with the KY-040 Rotary Encoder
- Build this
Menu navigation dial
Scroll a display menu with rotation and select with the push switch, a common interface for OLED projects.
- Build this
Digital volume or brightness knob
Turn the encoder to raise or lower a value with no end stop, like a stereo volume control.
- Build this
Parameter tuning input
Adjust setpoints such as temperature or speed on the fly in a control project.
- Build this
Retro jog wheel or game input
Use continuous rotation as a jog wheel for media scrubbing or a paddle-style game control.
This page as plain Markdown, for ChatGPT, Claude or your own agent. /components/rotary-encoder-ky-040.md