• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Circuit Ideas for You

Get free circuit ideas online.

  • Home
  • Privacy Policy
  • About
Home » KY040 Encoder Module Circuit with Arduino

KY040 Encoder Module Circuit with Arduino

Last updated on 22 November 2025 by Admin-Lavi Leave a Comment

Rotary encoder is a sensor that tell rotation and direction.

It is used in volume knobs, motors, robot wheels and menu selections.

Arduino can read the encoder pulses and show value in serial monitor.

This project for KY040 Encoder Module Circuit with Arduino is very simple and beginner friendly.

Arduino Coding:

int clk = 6;
int dt = 7;
int counter = 0;
int lastState;

void setup() {
Serial.begin(9600);
pinMode(clk, INPUT);
pinMode(dt, INPUT);
lastState = digitalRead(clk);
}

void loop() {
int currentState = digitalRead(clk);
if (currentState != lastState) {
if (digitalRead(dt) != currentState) {
counter++;
} else {
counter--;
}
Serial.println(counter);
}
lastState = currentState;
}

Coding Explanation:

  • clk pin gives pulse when turning.
  • dt pin tells direction.
  • Arduino store last state.
  • If state changes then rotation happen.
  • If dt is different from clk then clockwise.
  • If dt same then anticlockwise.
  • Counter increase or decrease.
  • Serial monitor show value.

Circuit Working:

KY040 Encoder Module Circuit Diagram with Arduino

Parts List:

Part NameQuantity
Arduino Uno1
KY040 Rotatory Encoder module1
USB cable 1

Power goes from USB to Arduino.

Arduino gives 5V and GND to the rotary encoder.

Encoder knob turns and makes two pulse signals on CLK and DT pins.

Arduino reads change on CLK.

At that moment Arduino checks DT.

If DT is different from CLK clockwise.

If DT is same as CLK anticlockwise.

Arduino increases or decreases counter value.

Serial monitor shows the new counter value.

This is how the circuit reads rotation and direction.

Formula with Calculation:

Basic formula:

angle = steps × stepAngle

where,

  • angle is the total rotation amount.
  • steps means how many pulses the encoder counted
  • stepAngle means degrees per one step (example: 18° per step)

So when knob turns Arduino counts steps.

Then multiply steps by degrees per step.

We get the total rotation angle from starting point.

Example:

steps = 5

stepAngle = 18°

angle = 5 × 18 = 90°

It means the knob rotated 90 degrees.

How to Build:

To build a KY040 Encoder Module Circuit with Arduino follow the below connection steps:

  • Take all the parts as shown in circuit diagram above.
  • Encoder GND pin goes to Arduino GND.
  • Encoder + pin goes to Arduino 5V.
  • Encoder CLK pin goes to Arduino pin 6.
  • Encoder DT pin goes to Arduino pin 7.
  • Encoder SW pin is not used in this example.
  • If we want to see numbers on PC, then open serial monitor.

Conclusion:

KY040 Encoder Module Circuit with Arduino is easy.

Only two output pins needed.

Code read pulses and show count.

We can use this for menu, robot wheel, motor position and many projects.

Good for learning digital sensors and interrupts.

References:

Fully Working KY-040 Rotary Encoder Source Code, Fully Accurate (no jumps)

Filed Under: Arduino Projects, Sensors and Detectors

About Admin-Lavi

Lavi is a B.Tech electronics engineer with a passion for designing new electronic circuits. Do you have questions regarding the circuit diagrams presented on this blog? Feel free to comment and solve your queries with quick replies

Previous Post: « Solenoid Door Lock Circuit with Arduino
Next Post: Simple Push Button Circuit with Arduino »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar




Categories

  • Alarm Circuits (45)
  • Amplifier Circuits (67)
  • Arduino Projects (99)
  • Audio Circuits (114)
  • Automobile Circuits (19)
  • Battery Charger Circuits (65)
  • Free Energy (13)
  • Heater and Temperature Controllers (13)
  • High Voltage (1)
  • Indicator Circuits (45)
  • Inverter Circuits (20)
  • IoT projects (1)
  • LDR Circuits (26)
  • LED and Lamps (148)
  • Meters and Testers (30)
  • Motor Controllers (22)
  • Oscillator Circuits (39)
  • Power Supply Circuits (104)
  • Remote Control Circuits (10)
  • Security and Protection (29)
  • Sensors and Detectors (126)
  • Solar Circuits (29)
  • Timer Circuits (41)
  • Transistor Circuits (87)
  • Transmitter Circuit (17)
  • Tutorials (8)
  • Water Level Controller (6)

Recent Posts

  • 12V Battery Low Voltage Alarm Circuit
  • Low Voltage Alert Circuit for 9V Battery
  • Digital Code Lock Circuit using IC 4017
  • Darkness Activated LED Circuit
  • Fan Start 10 Second Delay Timer Circuit

Recent Comments

  1. TDHofstetter on DIY Variable Power Supply Circuit using Arduino
  2. feathbuff on Simple Micro Ampere Meter Circuit
  3. Admin-Lavi on Simple School Project Multimeter Circuit
  4. choke on Simple School Project Multimeter Circuit
  5. Admin-Lavi on Analog to Digital Converter Circuit using IC 555

Copyright © 2026 | New Circuit Ideas