Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Interface Rotary Encoder with Arduino

Introduction

Rotary encoders are used to measure the angular position or rotation of a shaft, which makes them suitable for applications like volume control, robot navigation, or menu navigation in user interfaces. The encoder sends out two signals, often called CLK (clock) and DT (data), which allow the Arduino to detect rotation direction and steps.

Required Components

  • Arduino UNO, Nano
  • Rotary encoder (e.g., KY-040)
  • Breadboard
  • Jumper wires
  • Optional: Push button connected to the encoder

Pinout

Circuit Diagram / Wiring

  • ROTARY VCC → 5V (Arduino)
  • ROTARY GND → GND (Arduino)
  • ROTARY CLK → Pin D2 (Arduino)
  • ROTARY DT → Pin D3 (Arduino)
  • ROTARY SW → Pin D4 (Arduino)

Arduino Code / Programming

Use the following code to read the rotary encoder values and display them in the Serial Monitor:

#define CLK 2  // Pin connected to CLK
#define DT 3   // Pin connected to DT
#define SW 4   // Pin connected to SW (optional)

int counter = 0;  // Stores the position count
int currentStateCLK;
int lastStateCLK;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);  // Optional: For the switch

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

  // Begin Serial communication
  Serial.begin(9600);
}

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

  // If the previous state and the current state of CLK are different, then the encoder has rotated
  if (currentStateCLK != lastStateCLK) {
    // Check if DT is different from CLK
    if (digitalRead(DT) != currentStateCLK) {
      counter++;  // Clockwise rotation
    } else {
      counter--;  // Counterclockwise rotation
    }
    // Print the counter value to the Serial Monitor
    Serial.print("Position: ");
    Serial.println(counter);
  }

  // Update the last state of CLK with the current state
  lastStateCLK = currentStateCLK;

  // Optional: Check if the push button is pressed
  if (digitalRead(SW) == LOW) {
    Serial.println("Button Pressed");
    delay(50);  // Debounce delay
  }
}

Explanation of the Code

  • Pin Definitions: The pins for CLK, DT, and the switch are defined.
  • Setup Function: Initializes the encoder pins as inputs, reads the initial CLK state, and starts Serial communication.
  • Loop Function:
    • Reads the current state of the CLK pin.
    • If the state changes, it checks the DT pin to determine the rotation direction and updates the counter.
    • Optionally, checks the SW pin to detect button presses.
  • Debouncing: Adds a small delay after detecting a button press to avoid reading multiple presses.

Testing and Troubleshooting

  • Check Connections: Make sure the pins are connected correctly.
  • Monitor Output: Open the Serial Monitor to view the encoder position and button state.
  • Adjust Speed: The encoder may rotate too quickly or slowly, so you can modify the code to suit your application.

    Leave a Reply

    Your email address will not be published.

    Need Help?