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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | KY-040 rotary encoder | CLK 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
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 2 | KY-040 rotary encoder CLK |
| 2 | Arduino Uno pin 3 | KY-040 rotary encoder DT |
| 3 | Arduino Uno pin 4 | KY-040 rotary encoder SW |
| 4 | Arduino Uno 5V | KY-040 rotary encoder VCC |
| 5 | Arduino Uno GND | KY-040 rotary encoder GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a KY-040 rotary encoder from the parts palette.
- Wire 1. Connect Arduino Uno pin 2 to KY-040 rotary encoder CLK.
- Wire 2. Connect Arduino Uno pin 3 to KY-040 rotary encoder DT.
- Wire 3. Connect Arduino Uno pin 4 to KY-040 rotary encoder SW.
- Wire 4. Connect Arduino Uno 5V to KY-040 rotary encoder VCC.
- Wire 5. Connect Arduino Uno GND to KY-040 rotary encoder GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator