science tinker

Rotary encoder (KY-040)

A dial that turns forever in either direction and clicks as it goes. It reports each click and which way it was turned, and its shaft is also a push button.

Switches and controls · Elements inside: CuSnZnFeC

Function: what the KY-040 does in a circuit

A dial that turns forever in either direction and clicks as it goes. It reports each click and which way it was turned, and its shaft is also a push button.

  • Counts clicks as the dial is turned, in either direction
  • Lets the user scroll through menus or change a value step by step
  • Works as a push button when the shaft is pressed

KY-040 key facts

Typeincremental rotary encoder with push switch (KY-040 module)
Steps20 clicks per full turn
OutputsCLK (A) and DT (B), 90° out of step, plus SW
Supply5 V (3.3 V also works)
Pull-ups10 kΩ on CLK and DT on the module
Lifeabout 30,000 turns

KY-040 pinout

The connections, in the order they appear on the part:

No.Name
1CLK (output A)
2DT (output B)
3SW (push switch)
4+ (5V)
5GND

How to connect the KY-040 to an Arduino

Connect CLK to pin 2, DT to pin 3, SW to pin 4, + to 5V and GND to GND. Set SW as INPUT_PULLUP.

Example sketch:

const int CLK = 2, DT = 3, SW = 4;
int counter = 0;
int lastClk = HIGH;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int clk = digitalRead(CLK);
  if (clk == LOW && lastClk == HIGH) {     // one click
    if (digitalRead(DT) == HIGH) counter++;  // clockwise
    else counter--;                          // anticlockwise
    Serial.println(counter);
  }
  lastClk = clk;
  if (digitalRead(SW) == LOW) counter = 0;   // press to reset
  delay(1);
}

Run this in the free simulator

How it works

Inside, a wheel with metal fingers touches two contacts, A (CLK) and B (DT), one slightly after the other. Both rest HIGH. Turning one click clockwise pulls CLK LOW first and DT LOW a moment later; anticlockwise it is DT first. So when CLK falls, the sketch reads DT: still HIGH means clockwise, already LOW means anticlockwise. Unlike a potentiometer it has no end stops and gives steps, not a voltage. Pressing the shaft connects SW to GND.

What it is made of

  • Contacts: Phosphor bronze spring fingers on a brass code wheel CuSnZn
  • Shaft and case: Steel or zinc alloy, with a plastic body FeZnC
  • Board: Glass-fibre and epoxy with copper tracks and pull-up resistors CuSn

Where the materials come from: Copper from chalcopyrite; tin from cassiterite; zinc from sphalerite; iron from haematite.

The chemistry

Phosphor bronze (copper with tin and a little phosphorus) stays springy for hundreds of thousands of clicks, so the contacts keep pressing on the wheel.

Biology connection

The balance organs in your inner ear also sense turning and its direction: fluid in three curved canals lags behind when your head rotates and bends tiny hair cells one way or the other. How the ear hears and balances →

Safety and the environment

Read CLK often (every millisecond or with an interrupt) or fast turns are missed. Mechanical contacts bounce, so ignore changes that come within a couple of milliseconds.

Did you know? The volume dial on most modern car radios and the scroll wheel of a mouse are rotary encoders, not potentiometers.

Try it in the simulator

Build and code a circuit with a Rotary encoder in these practice projects:

Questions students ask

What does the KY-040 do?

A dial that turns forever in either direction and clicks as it goes. It reports each click and which way it was turned, and its shaft is also a push button. Main functions: counts clicks as the dial is turned, in either direction; lets the user scroll through menus or change a value step by step; works as a push button when the shaft is pressed.

What is the difference between a rotary encoder and a potentiometer?

A potentiometer gives a voltage that depends on where its shaft is, between two end stops. A rotary encoder turns forever and only reports each click and its direction, so the sketch must count the clicks itself.

What are the connections of the KY-040?

In order: CLK (output A), DT (output B), SW (push switch), + (5V), GND.

How do I connect the KY-040 to an Arduino?

Connect CLK to pin 2, DT to pin 3, SW to pin 4, + to 5V and GND to GND. Set SW as INPUT_PULLUP.

How does the KY-040 work?

Inside, a wheel with metal fingers touches two contacts, A (CLK) and B (DT), one slightly after the other. Both rest HIGH. Turning one click clockwise pulls CLK LOW first and DT LOW a moment later; anticlockwise it is DT first. So when CLK falls, the sketch reads DT: still HIGH means clockwise, already LOW means anticlockwise. Unlike a potentiometer it has no end stops and gives steps, not a voltage. Pressing the shaft connects SW to GND.

What is the KY-040 made of?

Contacts: Phosphor bronze spring fingers on a brass code wheel. Shaft and case: Steel or zinc alloy, with a plastic body. Board: Glass-fibre and epoxy with copper tracks and pull-up resistors.

Is the KY-040 safe? How should it be recycled?

Read CLK often (every millisecond or with an interrupt) or fast turns are missed. Mechanical contacts bounce, so ignore changes that come within a couple of milliseconds.

More switches and controls

Know someone who would use this?

It’s free for every learner. Send it to your class, team or club.

Updated · Free, no sign-up ·

Connected tools

The same idea from another angle, across the site.