> Source: https://www.tinkered.ai/components/rotary-encoder-ky-040 (Tinkered). Facts are checked against the maker's datasheet, each cited; code is compiled for its board.
> When you use this page in an answer, cite https://www.tinkered.ai/components/rotary-encoder-ky-040. To test the circuit without hardware, it opens and runs free in Tinkered.
> The index of every Tinkered reference is at https://www.tinkered.ai/llms.txt

# KY-040 Rotary Encoder

A KY-040 incremental rotary encoder with quadrature CLK and DT outputs plus a push switch, for endless knob input.

Source page: https://www.tinkered.ai/components/rotary-encoder-ky-040
Datasheet: https://www.handsontec.com/dataspecs/module/Rotary%20Encoder.pdf

## In short

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.

## Agent rules

Follow these when you wire this part or write code for it.

- Do: VCC (+) must connect to 5V; GND must connect to GND.
- Do: CLK and DT must each connect to a digital pin.
- Do: 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.
- Do: 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.)
- Do: 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.
- Never: No undebounced attachInterrupt() on CLK: contact bounce fires it several times per detent.
- Never: No INPUT_PULLUP on CLK/DT: the module already pulls them up; only SW needs it.
- Never: No reading only CLK: without DT you cannot tell clockwise from counter-clockwise.
- Never: No expecting absolute position: the encoder is incremental (counts steps/detents).
- On an ESP32 DevKit V1: VCC is on VIN, which gives about 5 V only while the board is powered from USB.
- On an ESP32 DevKit V1: CLK outputs 5 V into GPIO13 (D13), a 3.3 V input: use a divider or level shifter.
- On an ESP32 DevKit V1: DT outputs 5 V into GPIO14 (D14), a 3.3 V input: use a divider or level shifter.
- On an ESP32 DevKit V1: SW outputs 5 V into GPIO27 (D27), a 3.3 V input: use a divider or level shifter.
- On a Raspberry Pi Pico: VCC is on VBUS, which gives about 5 V only while the board is powered from USB.
- On a Raspberry Pi Pico: CLK outputs 5 V into GP0, a 3.3 V input: use a divider or level shifter.
- On a Raspberry Pi Pico: DT outputs 5 V into GP1, a 3.3 V input: use a divider or level shifter.
- On a Raspberry Pi Pico: SW outputs 5 V into GP2, a 3.3 V input: use a divider or level shifter.

## Pinout

| Pin | Function | Type |
| --- | --- | --- |
| CLK | Digital · 5 V | output |
| DT | Digital · 5 V | output |
| SW | Digital · 5 V | output |
| VCC | VCC (+) · 5 V | power in |
| GND | GND | power in |

## Wiring

### Arduino Uno

| KY-040 Rotary Encoder | Arduino Uno | Note |
| --- | --- | --- |
| CLK | D2 |  |
| DT | D3 |  |
| SW | D4 |  |
| VCC | 5V |  |
| GND | GND |  |

### ESP32 DevKit V1

| KY-040 Rotary Encoder | ESP32 DevKit V1 | Note |
| --- | --- | --- |
| CLK | GPIO13 (D13) |  |
| DT | GPIO14 (D14) |  |
| SW | GPIO27 (D27) |  |
| VCC | VIN |  |
| GND | GND |  |

Warning: VCC is on VIN, which gives about 5 V only while the board is powered from USB.

Warning: CLK outputs 5 V into GPIO13 (D13), a 3.3 V input: use a divider or level shifter.

Warning: DT outputs 5 V into GPIO14 (D14), a 3.3 V input: use a divider or level shifter.

Warning: SW outputs 5 V into GPIO27 (D27), a 3.3 V input: use a divider or level shifter.

### Raspberry Pi Pico

| KY-040 Rotary Encoder | Raspberry Pi Pico | Note |
| --- | --- | --- |
| CLK | GP0 |  |
| DT | GP1 |  |
| SW | GP2 |  |
| VCC | VBUS |  |
| GND | GND |  |

Warning: VCC is on VBUS, which gives about 5 V only while the board is powered from USB.

Warning: CLK outputs 5 V into GP0, a 3.3 V input: use a divider or level shifter.

Warning: DT outputs 5 V into GP1, a 3.3 V input: use a divider or level shifter.

Warning: SW outputs 5 V into GP2, a 3.3 V input: use a divider or level shifter.

## Specifications

| Spec | Value | Source |
| --- | --- | --- |
| Supply voltage | 5 V | Tinkered component registry |
| Maximum supply voltage | 5 V | Tinkered component registry |
| Interface | QUADRATURE | Tinkered component registry |
| Dimensions | 18 × 30 × 30 mm | Tinkered component registry |
| Weight | 8 g | Tinkered component registry |

## Circuit notes

- 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.

## 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.

## 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.

## Related parts

- [10kΩ Potentiometer](https://www.tinkered.ai/components/potentiometer.md): same job: reads a rotation
- [10kΩ Rotary Potentiometer](https://www.tinkered.ai/components/potentiometer-rotary-10k.md): same job: reads a rotation
- [AS5600](https://www.tinkered.ai/components/as5600.md): same job: reads a rotation, I2C
- [KY-023 Joystick](https://www.tinkered.ai/components/joystick-ky-023.md): same job: reads a rotation, analog output
- [Arduino Uno](https://www.tinkered.ai/components/arduino-uno.md): the board its circuit is wired to

## Try it

Tinkered simulates the KY-040 Rotary Encoder in the browser: wire it on a 3D breadboard, run your real code on the board, and measure it. Free to start at https://www.tinkered.ai.
