Logo

Interfacing Rotary Encoder Switch with Arduino

Rotary Encoder Switch

A Rotary Encoder Switch is an electromechanical device that converts rotational motion into digital signals. It's commonly used in control systems, digital volume knobs, and menu navigation interfaces.

Working Principle of Rotary Encoder Switch

The Rotary Encoder works by generating digital pulses as its shaft is rotated. These pulses can be interpreted by a microcontroller like Arduino to detect the direction and amount of rotation. Many encoders also include a push-button switch when pressed down.

Types of Rotary Encoders

Incremental Encoder

  • Outputs two square waves (A and B) 90° out of phase.
  • Determines rotation direction based on signal sequence.
  • Counts pulses to determine how far the knob has rotated.

Absolute Encoder

  • Each position has a distinct binary output.
  • Gives exact shaft position at any time.
  • Used in high-precision applications.

Requirements

1. Arduino Board (Uno, Mega, etc.)

2. Rotary Encoder Module

3. Jumper wires

4. Breadboard (optional)

Pin Configuration of Rotary Encoder

Typical Rotary Encoder Module

  • GND: Connect to Arduino GND.
  • VCC: Connect to +5V on Arduino.
  • CLK (A): Outputs pulses for rotation.
  • DT (B): Direction indicator signal.
  • SW: Push-button switch output.

Wiring the Rotary Encoder to Arduino

Connect the CLK and DT pins to digital pins on the Arduino. Connect VCC to 5V, GND to ground, and SW to another digital pin if the push-button functionality is needed. Use internal pull-up resistors to avoid floating signals.

Algorithm

  1. Initialize Components

    • Define CLK, DT, and SW pins in your code.
    • Set them as INPUT and enable internal pull-up resistors if necessary.
  2. Write the Code

    • Track changes in the CLK and DT states.
    • Compare signal changes to detect rotation direction.
    • Increment or decrement a counter based on the direction.
  3. Read the Push Button

    • Monitor the SW pin for LOW signal (button press).
    • Trigger custom actions when button is pressed.
  4. Test the Setup

    • Upload the code to Arduino.
    • Rotate the encoder and observe changes in value on the serial monitor.
    • Press the switch and ensure the input is detected correctly.

Arduino Code

1const int encoderCLK = 8;
2const int encoderDT = 9;
3
4int lastCLK = LOW;
5int encoderPos = 0;
6
7void setup() {
8  pinMode(encoderCLK, INPUT);
9  pinMode(encoderDT, INPUT);
10  Serial.begin(9600);
11}
12
13void loop() {
14  int currentCLK = digitalRead(encoderCLK);
15
16  if (currentCLK != lastCLK && currentCLK == LOW) {
17    if (digitalRead(encoderDT) != currentCLK) {
18      encoderPos++;
19      Serial.print("Rotary Encoder: Clockwise to ");
20    } else {
21      encoderPos--;
22      Serial.print("Rotary Encoder: Counter-Clockwise to ");
23    }
24    Serial.println(encoderPos);
25  }
26
27  lastCLK = currentCLK;
28}
29

Applications of Rotary Encoder Switches

  • Digital volume control
  • Menu navigation in embedded systems
  • Position control in CNC and robotics
  • User interface for 3D printers
  • Industrial control panels
  • Home automation knob controls

Conclusion

The Rotary Encoder Switch is a powerful tool for creating intuitive and responsive user interfaces in your Arduino projects. It offers precise control with simple wiring and flexible coding possibilities.