• 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 1 July 2026 by Admin-Lavi Leave a Comment

A rotary encoder is a sensor that detects rotation and direction; also people use it in volume knobs, motors, robot wheels and menu selection systems.

Arduino can read the encoder pulses and show value in serial monitor; further this project for KY040 Encoder Module Circuit with Arduino is very simple and beginner friendly.

Arduino Code:

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:

ComponentsQuantity
Arduino Uno1
KY040 Rotatory Encoder module1
USB cable 1

Power goes from USB to Arduino and then Arduino gives 5V and GND to the rotary encoder; then encoder knob turns and makes two pulse signals on CLK and DT pins.

Furthermore, Arduino reads change on CLK and at that moment Arduino checks DT.

If DT is different from CLK clockwise and if DT is same as CLK anticlockwise, then Arduino increases or decreases counter value.

Finally, serial monitor shows the new counter value and 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 and lastly, 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:

  • First, take all the parts as shown in circuit diagram above.
  • Next, encoder GND pin goes to Arduino GND, encoder + pin goes to Arduino 5V and encoder CLK pin goes to Arduino pin 6.
  • Then encoder DT pin goes to Arduino pin 7 and encoder SW pin, not used in this example.
  • Finally, if we want to see numbers on PC, then open serial monitor.

Conclusion:

Overall, the KY040 Encoder Module Circuit with Arduino is simple and requires only two output pins; the code also reads the pulses and displays the count.

Moreover, we can use this for menu, robot wheel, motor position and many projects and is also good for learning digital sensors and interrupts.

Filed Under: Arduino Projects, Automation Circuits, Mini 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

  • 555 Timer Projects (137)
  • Alarm Circuits (81)
  • Amplifier Circuits (94)
  • Arduino Projects (103)
  • Audio Circuits (189)
  • Automation Circuits (150)
  • Automobile Circuits (40)
  • Battery Charger Circuits (88)
  • DIY Projects (435)
  • Free Energy (12)
  • Heater and Temperature Controllers (32)
  • High Voltage (24)
  • Hobby Circuits (241)
  • Indicator Circuits (63)
  • Inverter Circuits (17)
  • IoT projects (12)
  • LDR Circuits (47)
  • LED and Lamps (201)
  • Meters and Testers (44)
  • Mini Projects (390)
  • Motor Controllers (26)
  • Oscillator Circuits (70)
  • Power Supply Circuits (244)
  • Radio Frequency (8)
  • Remote Control Circuits (12)
  • Renewable energy (20)
  • Security and Protection (128)
  • Sensors and Detectors (236)
  • Solar Circuits (31)
  • Timer Circuits (63)
  • Transistor Circuits (178)
  • Transmitter Circuit (19)
  • Tutorials (33)
  • Voltage Regulator (55)
  • Water Level Controller (10)

Recent Posts

  • Low Current Controlled Battery Charger Circuit using LM723 IC
  • 741 Op-Amp Treble Booster Circuit
  • Simple Diode and Transistor Based Audio Limiter Circuit
  • Simple 500mW Speaker Driver Circuit
  • Easy DIY LM386 Audio Amplifier Circuit

Recent Comments

  1. Admin-Lavi on High Voltage Fence Charger Circuit
  2. etere on High Voltage Fence Charger Circuit
  3. Admin-Lavi on High Voltage Fence Charger Circuit
  4. egidio grzinic on High Voltage Fence Charger Circuit
  5. Logic OR Gate Circuit using Transistors - Circuit Ideas for You on Automatic Street Light Circuit using LDR

Copyright © 2026 | New Circuit Ideas