What is KY-040 Rotary Encoder Module & How to Use It ?
What is KY-040 Rotary Encoder Module, Arduino Example, Encoder Types, Encoder and Arduino Wiring Diagram
Introduction to the KY-040 Rotary Encoder
In the world of electronics and microcontrollers, rotary encoders play a crucial role in various applications that require precise rotary motion sensing. The KY-040 rotary encoder, in particular, has gained popularity due to its affordability and versatility. Whether you are a hobbyist, a student, or a professional, understanding how to utilize the KY-040 rotary encoder can open up a multitude of possibilities for your projects.
Rotary Encoder Module is a nifty little device that allows you to track the rotation and direction of a rotary encoder. A rotary encoder is like a fancy knob that you can twist to control things, such as volume or navigation menus. The module is specifically designed to work with Arduino boards, making it easy to incorporate rotary encoders into your projects without much hassle.
What is a Rotary Encoder?
If you’ve ever wondered how devices like volume control knobs or speedometers work, rotary encoders are the unsung heroes behind their smooth and precise operation. A rotary encoder is a device that converts rotary motion into electrical signals. It provides a way to determine the position, speed, and direction of rotation of a shaft. As you twist the encoder knob, it generates pulses. These pulses are then decoded by the processor to determine the direction and magnitude of rotation.
Difference Between Incremental and Absolute Rotary Encoders
There are two main types of rotary encoders: incremental and absolute. Incremental encoders track relative movement, meaning they only provide information about changes in position. Absolute encoders, on the other hand, give you the exact position of the encoder within a full 360-degree revolution.
Overview of Arduino Rotary Encoder Module Components
The Arduino Rotary Encoder Module consists of a rotary encoder, a breakout board, and connection pins. The breakout board acts as an intermediary, providing a simplified interface for connecting the encoder to your Arduino board. KY-040 rotary encoder, a popular and affordable option for electronics enthusiasts. The KY-040 is a mechanical incremental rotary encoder with a built-in push-button switch. It features a knob that can be turned clockwise or counterclockwise, providing rotation detection, as well as a push-button that can be pressed by pushing down on the knob.

Understanding the Working Mechanism of KY-040 Rotary Encoder
Basic Principles of Rotary Encoder Operation
To grasp how a rotary encoder works, think of it like a fancy volume knob. As you turn the knob, it creates a series of electrical pulses that correspond to the direction and magnitude of rotation. These pulses are then interpreted by a microcontroller or other electronic circuits to determine the rotation information. The module typically operates at a voltage range of 3.3V to 5V, making it compatible with most Arduino boards. It offers reliable performance and accurate tracking of the encoder’s movements. The module also has built-in debouncing circuitry to eliminate any noise or false readings.
How to Connect and Interface the KY-040 Rotary Encoder with Arduino
Required Components and Tools
To connect the KY-040 rotary encoder to an Arduino microcontroller, you’ll need the encoder itself, an Arduino board, jumper wires, and a breadboard (optional but handy for prototyping).
Sample Circuit Diagram

Once the connections are made, you can use Arduino code to read the encoder’s input signals and implement various applications, such as controlling a motor’s speed or navigating through menus on an LCD display. Now that you understand the basics of the KY-040 rotary encoder, you’re ready to embark on exciting projects that involve precise control and detection of rotational movement. So, go forth and spin those encoders with confidence!
Reading the Rotary Encoder Input
Arduino Example
CODE
//Arduino and KY-040 rotary encoder module
int encoderPinA = 2; // CLK pin
int encoderPinB = 3; // DT pin
int encoderBtn = 4; // SW pin
int count = 0;
int encoderPinA_prev;
int encoderPinA_value;
boolean bool_CW;
void setup()
{
Serial.begin (9600);
pinMode (encoderPinA, INPUT);
pinMode (encoderPinB, INPUT);
pinMode(encoderBtn, INPUT_PULLUP);
encoderPinA_prev = digitalRead(encoderPinA);
}
void loop()
{
encoderPinA_value = digitalRead(encoderPinA);
if (encoderPinA_value != encoderPinA_prev)
{ // check if knob is rotating
// if pin A state changed before pin B, rotation is clockwise
if (digitalRead(encoderPinB) != encoderPinA_value)
{
count ++;
bool_CW = true;
}
else
{
// if pin B state changed before pin A, rotation is counter-clockwise
bool_CW = false;
count--;
}
if (bool_CW)
{
Serial.print("Clockwise | ");
}
else
{
Serial.print("Counter-Clockwise | ");
}
Serial.print(count);
Serial.print(" | ");
}
encoderPinA_prev = encoderPinA_value;
// check if button is pressed (pin SW)
if (digitalRead(encoderBtn) == LOW) Serial.println("Button Pressed");
else Serial.println("Button Released");
}
PROJECT TEST:
Turn on your Arduino board and launch the Arduino IDE’s Serial Monitor. Try turning the encoder shaft, then watch the Serial Monitor for the output. The serial monitor will display the Arduino’s rotational direction, shaft location, and button state (pressed or released).
FAQ
Can I use the KY-040 rotary encoder with other microcontrollers besides Arduino?
Yes, the KY-040 rotary encoder can be used with various microcontrollers that support digital input and output. While this article focuses on the Arduino platform, you can adapt the wiring connections and programming techniques to interface the KY-040 with other microcontrollers such as Raspberry Pi, ESP8266, or STM32.
How can I determine the resolution or steps-per-revolution of the KY-040 rotary encoder?
The KY-040 rotary encoder typically provides 20 steps per revolution. However, it’s always a good practice to consult the datasheet or product specifications for the specific KY-040 model you are using, as the resolution may vary between different manufacturers or versions.
Can I use multiple KY-040 rotary encoders in a single project?
Yes, you can use multiple KY-040 rotary encoders in a single project. Each KY-040 rotary encoder would require its own set of digital input pins on the microcontroller for reading the encoder signals. By implementing proper wiring connections and adjusting the code, you can easily incorporate multiple KY-040 rotary encoders to handle different rotary input requirements.
Are there any precautions to consider when connecting the KY-040 rotary encoder?
When connecting the KY-040 rotary encoder, it is important to ensure the correct orientation of the encoder pins and the proper voltage levels. Additionally, be cautious with ESD (electrostatic discharge) to prevent any damage to the KY-040 or the microcontroller. Always refer to the datasheet or product documentation for specific guidelines provided by the manufacturer.
