KY-040 Rotary Encoder

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

KY-040 Rotary Encoder: Incremental rotary input

What is the KY-040 Rotary Encoder?

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). The order in which CLK and DT change tells you the direction, and counting the edges tells you how far it turned. A typical KY-040 has 20 detents per revolution.

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 about 5 V and includes some pull-up resistors, though many designs still enable the microcontroller pull-ups for stability.

KY-040 Rotary Encoder pinout

PinFunctionType
CLK CLKDigital quadrature ADigital output
DT DTDigital quadrature BDigital output
SW SWDigital push switchDigital output
VCC VCC (+)PowerPower in
GND GNDPowerPower in

Specifications

Operating voltage
5 V (max 5 V)
Interface
quadrature
Dimensions
18 × 30 × 30 mm

Verified from the Tinkered component library · Generic.

Circuit requirements

  • Power VCC from about 5 V (3.3 V also works on many boards) and share a common ground. Connect CLK, DT, and SW to three digital inputs.
  • Read direction by sampling DT at the moment CLK changes: the two phases being in or out of step indicates clockwise versus counterclockwise.
  • Debounce the signals. The mechanical contacts bounce, so add a small RC filter or debounce in software, otherwise a single detent can register as several steps.
  • The SW (push switch) pin needs a pull-up and its own debounce, just like any momentary button.

Board compatibility

The KY-040 Rotary Encoder connects to any of the microcontrollers supported in Tinkered. Mind the 5 V logic level on 3.3 V boards.

Common KY-040 Rotary Encoder 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. Read the encoder frequently, or use pin-change interrupts, so no transitions are missed.

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 an open switch to ground. Without a pull-up it floats and reports random presses, so enable a pull-up and debounce it.

KY-040 Rotary Encoder datasheet

Official KY-040 Rotary Encoder datasheetGeneric

Popular KY-040 Rotary Encoder projects

Menu navigation dial

Scroll a display menu with rotation and select with the push switch, a common interface for OLED projects.

Digital volume or brightness knob

Turn the encoder to raise or lower a value with no end stop, like a stereo volume control.

Parameter tuning input

Adjust setpoints such as temperature or speed on the fly in a control project.

Retro jog wheel or game input

Use continuous rotation as a jog wheel for media scrubbing or a paddle-style game control.

Build with the KY-040 Rotary Encoder.

Drop the KY-040 Rotary Encoder into a circuit, write firmware, and simulate it in your browser, then deploy to real hardware. All in one editable Tinkered project.