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.

Arduino Uno: rotary encoderlive0.000 s 0.00x
Hold the button
This is the simulator itself, running here. Click anything to open it in the editor.
The state table method, from the encoder example
uint8_t now = pair();
if (now != state) {
  quarters += STEP[(state << 2) | now];
  state = now;
  if (now == 3) {
    if (quarters >= 4) count++;
    if (quarters <= -4) count--;
    quarters = 0;
  }
}
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.

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.

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.

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.