Ky-040 Rotary Encoder Sensor Module with Arduino


Welcome to the world of rotary encoder interfacing with Arduino! In this guide, we’ll explore How to Interfacing Ky-040 Rotary Encoder Sensor Module with Arduino. The KY-040 module enables Arduino projects to detect rotational movements and encode precise position information, making it ideal for applications such as rotary knob controls, motor speed control, and robotics. Let’s delve into the details of the KY-040 Rotary Encoder Sensor and learn how to integrate it with Arduino for rotational sensing.

Hardware Required

You will require the following Hardware Components for the, How to Interfacing Ky-040 Rotary Encoder Sensor Module with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Link
Ky-040 Rotary Encoder Sensor1Buy Link
37 in 1 Sensor kit (Optional)1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires5Buy Link
Breadboard1Buy Link

What is the KY-040 Rotary Encoder Sensor?

The KY-040 Rotary Encoder Sensor is a sensor module designed to detect rotational movements and encode precise position information. It consists of a rotary encoder and a tactile switch integrated into a single module. The rotary encoder utilizes an incremental encoding mechanism to detect both the direction and magnitude of rotational movements. The KY-040 module provides a simple and reliable solution for rotational sensing in Arduino projects.



  1. Operating Voltage:
    • Typically operates at 5V, compatible with Arduino’s voltage levels.
  2. Encoder Type:
    • Incremental rotary encoder with quadrature output signals.
  3. Resolution:
    • Provides high-resolution position information, typically with multiple pulses per revolution.
  4. Switch Type:
    • Includes a built-in tactile switch for detecting button presses or mechanical clicks.
  5. Dimensions:
    • Compact form factor suitable for integration into control panels, enclosures, and robotic systems.



Pin Configuration

NoPin NameDescription
2(+)Voltage supply (typically 5V or 3.3V)
3SWSwitch (Push button)
4DTDigital signal output for rotation
5CLKClock signal output for rotation


  1. Rotational Sensing:
    • Detects rotational movements with high precision and accuracy using the incremental encoding mechanism.
  2. Bi-Directional Operation:
    • Provides bidirectional rotation detection, allowing both clockwise and counterclockwise movements to be encoded.
  3. Integrated Button Switch:
    • Includes a tactile switch for additional functionality such as selection or confirmation actions.
  4. Simple Interface:
    • Simple interface with Arduino using digital input pins and minimal external components.
  5. Low Friction Design:
    • Features a low friction design for smooth and effortless rotation, ensuring reliable operation over time.

Circuit Diagram

The following circuit shows you the connection of the, How to Interfacing Ky-040 Rotary Encoder Sensor Module with Arduino . Please make the connection carefully


Circuit Connections

ArduinoRotary Encoder Sensor

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow


//For more Projects:

int encoderPinA = 8; // CLK pin
int encoderPinB = 9; // DT pin
int encoderBtn = 10; // 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;
if (bool_CW) {
Serial.print("Clockwise | ");
} else {
Serial.print("Counter-Clockwise | ");
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");


  1. Rotary Knob Controls: Use the KY-040 sensor for implementing rotary knob controls in user interfaces for adjusting parameters or settings.
  2. Motor Speed Control: Integrate the KY-040 sensor into motor control systems for adjusting motor speed or position based on rotational inputs.
  3. Robotic Navigation: Incorporate the KY-040 sensor into robotic systems for encoding precise position information and navigating environments.
  4. User Interfaces: Develop user interfaces with rotary input capabilities for interactive displays, multimedia systems, and industrial control panels.
  5. DIY Projects: Include the KY-040 sensor in do-it-yourself (DIY) projects such as home automation, 3D printers, and CNC machines for precise control and automation.


The KY-040 Rotary Encoder Sensor Module offers a versatile and reliable solution for rotational sensing with Arduino. With its high-resolution encoding, bidirectional operation, and integrated button switch, the KY-040 module provides endless possibilities for incorporating rotary input into Arduino projects. Let’s explore the potential of rotary encoder interfacing and unlock new opportunities for control and automation!

How to Measure Angle and Direction of Rotation with Arduino and Rotary Encoder

Leave a Comment