

A shaft encoder is a device that converts the rotational position or motion of a shaft into an electrical signal. This signal can represent either the absolute position of the shaft (absolute encoder) or the relative motion (incremental encoder). Shaft encoders are widely used in control systems, robotics, industrial automation, and motor feedback applications. They provide precise feedback for position, speed, and direction, making them essential in systems requiring accurate motion control.








The specifications of a shaft encoder can vary depending on the type and model. Below are general specifications for a typical incremental rotary shaft encoder:
Below is a typical pinout for a 5-pin incremental shaft encoder:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply input (5V to 24V DC) |
| 2 | GND | Ground connection |
| 3 | A | Channel A output signal |
| 4 | B | Channel B output signal (90° phase shift from A) |
| 5 | Z (optional) | Index pulse output (one pulse per revolution) |
Note: Always refer to the datasheet of your specific encoder model for exact pinout details.
Below is an example of interfacing an incremental shaft encoder with an Arduino UNO to read position and direction:
// Define encoder pins
const int encoderPinA = 2; // Channel A connected to digital pin 2
const int encoderPinB = 3; // Channel B connected to digital pin 3
volatile int encoderPosition = 0; // Variable to store encoder position
int lastEncoded = 0; // Variable to store the last encoder state
void setup() {
pinMode(encoderPinA, INPUT_PULLUP); // Set pin A as input with pull-up
pinMode(encoderPinB, INPUT_PULLUP); // Set pin B as input with pull-up
// Attach interrupts to encoder pins
attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Print the encoder position
Serial.print("Encoder Position: ");
Serial.println(encoderPosition);
delay(100); // Delay for readability
}
void updateEncoder() {
// Read the current state of the encoder pins
int MSB = digitalRead(encoderPinA); // Most significant bit
int LSB = digitalRead(encoderPinB); // Least significant bit
int encoded = (MSB << 1) | LSB; // Combine the two bits
int sum = (lastEncoded << 2) | encoded; // Combine with previous state
// Determine direction based on state transitions
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
encoderPosition++; // Clockwise rotation
} else if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
encoderPosition--; // Counterclockwise rotation
}
lastEncoded = encoded; // Update the last state
}
Note: Ensure the encoder is connected to interrupt-capable pins on the Arduino (e.g., pins 2 and 3 on the UNO).
Q: Can I use a shaft encoder with a Raspberry Pi?
A: Yes, but ensure you use GPIO pins capable of handling high-speed signals. You may also need external circuitry for signal conditioning.
Q: What is the difference between absolute and incremental encoders?
A: Absolute encoders provide the exact position of the shaft, while incremental encoders provide relative motion information.
Q: How do I calculate the speed of rotation?
A: Measure the time between pulses on the A or B channel and use the encoder's PPR value to calculate the speed.
By following this documentation, you can effectively integrate a shaft encoder into your projects for precise motion control and feedback.