Source: https://mokxi.com/parts/rotary
Updated: 2026-11-10

Part

# Rotary encoder, KY-040

A shaft that counts clicks, either way around, with a switch under it. Turn it with the pointer or the wheel.

See every part

- 5 pins
- 2 settings
- In 7 projects
- In 3 lessons

Drawn live by the editor's own code, at the size you see it.

At a glance

## The Rotary encoder, KY-040 in brief

What it is, and every setting the properties panel gives it.

The KY-040 is the rotary encoder module most kits include, a clicking shaft with no end stop and a push switch underneath. Its pins are VCC, GND, CLK, DT and SW. CLK and DT are two contacts a quarter cycle apart, so turning the shaft walks them through a four-state Gray code and the order gives the direction. The module has 10k pull-ups on CLK and DT but none on SW, which needs INPUT_PULLUP. On an Uno the usual wiring is CLK to pin 2 and DT to pin 3 with SW on pin 4; on an ESP32, power it from 3.3 volts. Mokxi plays each detent out as four quarter steps 8 milliseconds apart, so a polling loop can see them, and each contact chatters once for 400 microseconds by default. That is the common mistake made visible: a sketch that only watches CLK fall counts three clicks for one, where a full quadrature state table does not. Mokxi includes the Encoder, RotaryEncoder and ESP32Encoder libraries. A fast spin plays at a steady rate rather than a real hand’s speed, and SW does not bounce.

Family

Inputs

Pins

5

detents

20 (or 24, 30)

20, 24 or 30 per revolution, for the on-screen angle.

bounce

true

on by
default.

Pins

## 5 pins

Named as the editor names them, so a wire lands where you expect it.

Pin

What it does

GND

Common return for all three contacts.

VCC

Supply for the pull-ups.

SW

Shorts to ground when the shaft is pressed. Click or hold the knob to press it; drag around the knob or use the scroll wheel to turn it.

DT

The other half.

CLK

One half of the quadrature pair.

How to use it

## How to use the KY-040 rotary encoder with an Arduino

CLK to pin 2, DT to pin 3, SW to pin 4, VCC to 5 V and GND to GND, with all three pins set to INPUT_PULLUP. Read CLK and DT together as a two-bit state, and every time it changes, look the old and new states up in a sixteen-entry table: a real quarter step is +1 or -1, and four of them back at rest are one click.

The short tutorial method, counting a click whenever CLK falls, also counts the contact bounce. Press the knob in this circuit to switch to that method and watch one click count as several on the serial monitor. Turn the part’s bounce property off and both methods agree, which shows the chatter is the whole difference.

Part pin

Board pin

CLK

Pin 2

DT

Pin 3

SW

Pin 4

VCC

5 V

GND

GND

On an ESP32, power the module from 3.3 V; the ESP32Encoder library compiles here and counts the same quadrature.

Hold the button

This is the simulator itself, running here. Click anything to open it in the editor.

Open this project in the editor Read the full tutorial

The state table method, from the encoder example

How it is modeled

## What is true about the Rotary encoder, KY-040, here

In its help article’s own words: what the simulation gets right, and where it stops.

### Modeled

One click of the shaft is a whole cycle of the Gray code (four quarter steps),
played out on the timing wheel a few milliseconds apart rather than landing on one
timestamp, because a real shaft takes tens of milliseconds to cross a detent and
firmware polling in a loop only sees the sequence at all if the states are
separated in time. The contacts also chatter, once per quarter step, on by default,
because that is the whole lesson: a sketch that watches only for a falling edge on
CLK counts three clicks for one.

### Not modeled

No mechanical detent feel or click torque beyond the picture. This is the
electrical behavior, not the shaft's mechanics.

The turn is played out at a fixed rate. However fast the knob is dragged, the four
quarter steps of a click arrive 8 milliseconds apart, and a queue of up to 64 pending
clicks drains at that rate, so a fast spin here is a steady 31 clicks a second rather
than the acceleration a real shaft gives. A sketch that measures turn speed is
measuring this model, not a hand.

The chatter is one extra edge per quarter step, 400 microseconds wide, every time: a
fixed pattern rather than the variable burst a real contact makes, and the same on
every quarter step. SW does not bounce at all, although the switch under a real
KY-040 is a tactile dome and does. The 10 k pull-ups on CLK and DT are the module's
own and are modeled; there is no contact resistance drift and no wear.

From Rotary encoder (KY-040), in full.

Projects

## See the Rotary encoder, KY-040 in a project

7 built-in projects use it. Each opens in the editor, already wired.

Lessons

## Where it turns up in a lesson

3 lessons place it in their own circuits.

### Learn articles

3 lessons

- Design Your Sim Racing Button Box in the Browser
- Prototype Your Keyboard Matrix Before You Solder
- Arduino Rotary Encoder (KY-040): Wiring and Code

Boards

## The boards it is shown on

Each of these 3 boards has a built-in project with the Rotary encoder, KY-040 in it.

- ESP32-C3-DevKitM-1
- Arduino Uno R3
- SparkFun Pro Micro

Libraries

## Arduino libraries for the Rotary encoder, KY-040

Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.

- Encoder
- RotaryEncoder
- ESP32Encoder
- Ai Esp32 Rotary Encoder

More parts

## The rest of the bench

Every one of these is drawn and simulated the same way.

Full-size breadboard

A real 0.1 inch grid with the rails and the center channel, 63 columns wide.

Jumper wires

Drag from any pin or hole to any other. Corners snap, and you can drag them.

Power

A supply rail at the voltage you choose.

Ground

The other end of every circuit.

LED

Lights when current flows. Five colors, and the brightness is what your eye would see.

Filament lamp

A tungsten bulb in a holder: an inrush when it is switched on cold, tens of milliseconds to warm up, and dim on half its voltage.

Resistor

Any value you like, with the color bands drawn to match.

ESP32-C3-DevKitM-1

A RISC-V board that runs your firmware at 160 MHz on the real memory map.

Pushbutton

A 12 mm tactile switch. Hold it while the simulation runs.

Raspberry Pi Pico

The RP2040 board on our own Cortex-M0+ core. Pick a program and press Run.

Raspberry Pi Pico W

Raspberry Pi Pico W (RP2040). The same board and the same pinout as a Pico, with the CYW43439 on it. The WiFi is simulated (no radio; real internet through the Cloud panel’s switch), and the on-board LED, which hangs off that chip rather than off GP25, is driven through the same path. Everything else is the Pico.

Raspberry Pi 4 Model B

Raspberry Pi 4 Model B: the 40-pin header, driven by main.py with RPi.GPIO or gpiozero.

See every part

## Wire up the Rotary encoder, KY-040

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.

Start building free Open the editor
