Arduino KY-040 Rotary Encoder: Wiring, Code and Simulator

To build this Arduino KY-040 Rotary Encoder project you need an Arduino Uno and a KY-040 rotary encoder. A KY-040 encoder: CLK and DT in quadrature tell the direction, SW resets the count. Make the 5 connections in the wiring table, upload the Arduino C++ code, and turn the knob with the + and - buttons in the Inspector, or press it to reset.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1KY-040 rotary encoderCLK and DT are switches to GND with pull-ups: clockwise, CLK falls before DT. SW is a push button to GND (use INPUT_PULLUP).

Wiring

#FromTo
1Arduino Uno pin 2KY-040 rotary encoder CLK
2Arduino Uno pin 3KY-040 rotary encoder DT
3Arduino Uno pin 4KY-040 rotary encoder SW
4Arduino Uno 5VKY-040 rotary encoder VCC
5Arduino Uno GNDKY-040 rotary encoder GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a KY-040 rotary encoder from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 2 to KY-040 rotary encoder CLK.
  4. Wire 2. Connect Arduino Uno pin 3 to KY-040 rotary encoder DT.
  5. Wire 3. Connect Arduino Uno pin 4 to KY-040 rotary encoder SW.
  6. Wire 4. Connect Arduino Uno 5V to KY-040 rotary encoder VCC.
  7. Wire 5. Connect Arduino Uno GND to KY-040 rotary encoder GND.
  8. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  9. Run it. Press Run. Turn the knob with the + and - buttons in the Inspector, or press it to reset.

Arduino C++ code

// Rotary encoder: CLK on pin 2, DT on pin 3, the push switch SW on pin 4.
// When CLK falls, DT tells the direction: HIGH means clockwise.
const int CLK = 2, DT = 3, SW = 4;
int position = 0;
int lastClk;

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

void loop() {
  int clk = digitalRead(CLK);
  if (clk != lastClk && clk == LOW) {
    position += digitalRead(DT) == HIGH ? 1 : -1;
    Serial.print("Position: ");
    Serial.println(position);
  }
  lastClk = clk;
  if (digitalRead(SW) == LOW) {
    position = 0;
    Serial.println("Reset to 0");
    delay(250);
  }
}

What to try

Turn the knob with the + and - buttons in the Inspector, or press it to reset.

Frequently asked questions

How do I wire the KY-040 Rotary Encoder project on an Arduino Uno?

Make 5 connections: Arduino Uno pin 2 to KY-040 rotary encoder CLK; Arduino Uno pin 3 to KY-040 rotary encoder DT; Arduino Uno pin 4 to KY-040 rotary encoder SW; Arduino Uno 5V to KY-040 rotary encoder VCC; Arduino Uno GND to KY-040 rotary encoder GND.

What parts do I need for this Arduino project?

An Arduino Uno and a KY-040 rotary encoder.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A KY-040 encoder: CLK and DT in quadrature tell the direction, SW resets the count.

More Arduino Uno projects