How to Use the KY-040 Rotary Encoder with Arduino

How to Use the KY-040 Rotary Encoder with Arduino

The KY-040 rotary encoder is a mechanical device used to measure angular position or rotation. It is commonly used in volume controls, menu navigation, and position tracking in robotics. This tutorial will guide you through connecting and using the KY-040 rotary encoder with Arduino.


What You Will Need

  1. KY-040 Rotary Encoder Module
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Breadboard and Jumper Wires
  4. A computer with the Arduino IDE installed

Step 1: Understanding the KY-040 Rotary Encoder

The KY-040 encoder consists of a rotary dial that generates pulses when rotated. It also includes a built-in push button.

KY-040 Pinout

Pin Function
GND Ground
+ Power Supply (5V)
CLK Clock Pulse Signal
DT Data Pulse Signal
SW Push Button Signal

Step 2: Wiring the KY-040 to Arduino

Here’s how to connect the KY-040 to an Arduino Uno:

KY-040 Pin Arduino Pin
GND GND
+ 5V
CLK Pin 2
DT Pin 3
SW Pin 4

Step 3: Upload the Code

The following example code reads the rotary encoder’s rotation and detects button presses:

#define CLK 2  // Clock pin
#define DT 3   // Data pin
#define SW 4   // Switch pin

int counter = 0;     // Counter to track rotation
int currentStateCLK;
int lastStateCLK;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP); // Internal pull-up for the button

  Serial.begin(9600);

  // Read the initial state of the CLK pin
  lastStateCLK = digitalRead(CLK);

  Serial.println("KY-040 Rotary Encoder Test");
}

void loop() {
  // Read the current state of the CLK pin
  currentStateCLK = digitalRead(CLK);

  // If the state has changed, the encoder has rotated
  if (currentStateCLK != lastStateCLK) {
    // Check the DT pin to determine the rotation direction
    if (digitalRead(DT) != currentStateCLK) {
      counter++;
    } else {
      counter--;
    }

    Serial.print("Counter: ");
    Serial.println(counter);
  }

  // Save the last state of CLK
  lastStateCLK = currentStateCLK;

  // Check if the button is pressed
  if (digitalRead(SW) == LOW) {
    Serial.println("Button pressed!");
    delay(200); // Debounce delay
  }
}

Step 4: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code by clicking Upload.
  4. Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600.
  5. Rotate the encoder and press the button. Observe the counter and button press messages in the Serial Monitor.

Applications of the KY-040 Rotary Encoder

  1. Volume or brightness control
  2. Menu navigation
  3. Motor position tracking
  4. Robotics and automation systems

Troubleshooting

  • No rotation detection: Check the CLK and DT wiring and ensure the pins match the code.
  • Unstable readings: Add a small debounce delay to stabilize the encoder signals.
  • Button not detected: Verify the SW pin is properly connected and uses INPUT_PULLUP mode.

Conclusion

You’ve successfully interfaced the KY-040 rotary encoder with Arduino. This versatile module enables you to add rotation tracking and button functionality to your projects. Experiment with it in different applications, like building custom controls or navigating menus in your systems!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.