How to Use Arduino to Read the Value of the Encoder
The method of reading an encoder value depends on the type of encoder. The reading methods for incremental and absolute encoders are as follows:
Incremental encoder:
Incremental encoders have two output signal channels: A phase and B phase. They are pulse signals offset by 90 degrees from each other, and by detecting the state change of the A and B channels, the direction and angle of rotation can be determined. Here are the basic steps to read an incremental encoder using an Arduino:
Connect the A and B channels of the encoder to the interrupt pins of the Arduino (for example, pins 2 and 3).
Define a global variable in the Arduino code to store the encoder count.
Write an interrupt service routine to handle the state changes of the A and B channels. In this routine, the direction of rotation is determined based on the state of the A and B channels, and the encoder count is updated.
Set the interrupt in the setup() function so that it triggers the interrupt service routine when the state of the A or B channel changes.
In the main loop, the encoderCount variable can be read to get the relative position and direction of rotation of the motor shaft.
Absolute encoder:
Absolute encoders output a digital signal that represents the absolute position of the motor shaft. Here are the basic steps to read an absolute encoder with an Arduino:
Connect the data pin of the encoder to the digital input pin of the Arduino.
Set the data pin to input mode in the setup() function.
Write a function to read the value of the encoder. In this function, the state of each data pin is read using the digitalRead() function and combined into an integer value.
In the main loop, you can call the readEncoder() function to get the absolute position of the motor shaft.
Note that these examples are for basic encoder types. Encoders in real applications may require more complex processing methods, such as solving the signal bounce problem or using a dedicated encoder interface chip. When using an encoder, refer to its data sheet to ensure proper connection and signal processing.
Incremental encoder:
Incremental encoders have two output signal channels: A phase and B phase. They are pulse signals offset by 90 degrees from each other, and by detecting the state change of the A and B channels, the direction and angle of rotation can be determined. Here are the basic steps to read an incremental encoder using an Arduino:
Connect the A and B channels of the encoder to the interrupt pins of the Arduino (for example, pins 2 and 3).
Define a global variable in the Arduino code to store the encoder count.
Write an interrupt service routine to handle the state changes of the A and B channels. In this routine, the direction of rotation is determined based on the state of the A and B channels, and the encoder count is updated.
volatile long encoderCount = 0;
void encoderInterrupt() {
int A = digitalRead(2);
int B = digitalRead(3);
if (A == B) {
encoderCount++;
} else {
encoderCount--;
}
}
Set the interrupt in the setup() function so that it triggers the interrupt service routine when the state of the A or B channel changes.
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(2), encoderInterrupt, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), encoderInterrupt, CHANGE);
}
In the main loop, the encoderCount variable can be read to get the relative position and direction of rotation of the motor shaft.
Absolute encoder:
Absolute encoders output a digital signal that represents the absolute position of the motor shaft. Here are the basic steps to read an absolute encoder with an Arduino:
Connect the data pin of the encoder to the digital input pin of the Arduino.
Set the data pin to input mode in the setup() function.
void setup() {
pinMode(dataPin1, INPUT);
pinMode(dataPin2, INPUT);
// ... More data pins, depending on the number of encoder bits
}
Write a function to read the value of the encoder. In this function, the state of each data pin is read using the digitalRead() function and combined into an integer value.
int readEncoder() {
int value = 0;
value |= digitalRead(dataPin1) << 0;
value |= digitalRead(dataPin2) << 1;
// ... More data pins, depending on the number of encoder bits
return value;
}
In the main loop, you can call the readEncoder() function to get the absolute position of the motor shaft.
Note that these examples are for basic encoder types. Encoders in real applications may require more complex processing methods, such as solving the signal bounce problem or using a dedicated encoder interface chip. When using an encoder, refer to its data sheet to ensure proper connection and signal processing.
Updated on: 31/10/2023
Thank you!