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.
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)
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.
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 |
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);
}
}