Rotary Encoder

A rotary encoder is an electromechanical device that converts the angular position or motion of a shaft into digital signals. It typically has two output signals (A and B) that allow detection of rotation direction and speed. Rotary encoders are commonly used in control systems, robotics, and user interfaces (e.g., volume knobs) for precise position tracking without requiring end stops or absolute positions.

Rotary encoder on a small PCB
Fig.: Rotary encoder on a small PCB

Features/Specifications

Resolution 20 pulses per revolution (PPR)
Supply Voltage 3.3V – 5V DC
Output Digital (quadrature A/B channels)
Shaft Diameter 6 mm
Operating Temperature -40°C to +85°C
Switch Integrated push-button (momentary)

📄 Rotary encoder datasheet (1118 kB)

Functionality

Unlike a potentiometer, the rotary encoder has no start or end point. It can be rotated infinitely in both directions. Therefore, its position cannot be read directly, only the pulses it emits when turned. Some rotary encoders also include an additional button. Two signals are provided for evaluation, which allow determination of whether and in which direction the encoder is currently being turned. Depending on whether a rising or falling edge on S2 follows the signal from terminal S1, the rotation direction is either clockwise or counterclockwise.

Signal edges on the rotary encoder in clockwise direction
Fig.: Signal edges on the rotary encoder in clockwise direction
Signal edges on the rotary encoder in counterclockwise direction
Fig.: Signal edges on the rotary encoder in counterclockwise direction

Connections

Terminal diagram of a Rotary encoder
Fig.: Terminal diagram of a Rotary encoder
Pin Function
5V Power supply (typically 5V)
KEY Push button output (active low when pressed)
S2 Encoder signal B (second quadrature output)
S1 Encoder signal A (first quadrature output)
GND Ground connection

Used Components

Setup & Programming

Arduino Uno

Circuit setup with a Rotary Encoder and an Arduino Uno
Fig.: Circuit setup with a Rotary Encoder and an Arduino Uno

The following programm detects the rotation direction or button press and outputs the result to the serial console of the Arduino IDE.

byte value, valueOld;
int encoderValue = 0;

#define PIN_S1 3
#define PIN_S2 2
#define PIN_KEY 7

void setup() {
    pinMode(PIN_S1, INPUT);
    pinMode(PIN_S2, INPUT);
    pinMode(PIN_KEY, INPUT);
    Serial.begin(9600);
    valueOld = digitalRead(PIN_S1);
}

void loop() {
    if (digitalRead(PIN_KEY) == LOW) {
        Serial.println("Key pressed");
    }

    value = digitalRead(PIN_S1);
    if (value != valueOld) {
        if (digitalRead(PIN_S2) != value) {
            encoderValue++;
        } else {
            encoderValue--;
        }
        valueOld = value;
        Serial.println(encoderValue);
    }
}
Output of the result in the serial console
Fig.: Output of the result in the serial console
Last edited by Christian Grieger on 2025-05-16
X
  1. [top]
  2. Features/Specifications
  3. Functionality
  4. Connections
  5. Used Components
  6. Setup & Programming