Site icon Circuit Ideas for You

KY040 Encoder Module Circuit with Arduino

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:

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,

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:

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)

Exit mobile version