Rotary Encoder is a sensor or we can say a device that is used to convert the angular position of an axle into digital output signals. Also used to detect the direction of the axle or knob where it’s being turned. The other name for Rotary Encoder is “Shaft Encoder“.
The most convenient example of the use of a Rotary Encoder in our daily life is the volume control knob of the radios. Packaging, food and beverage, printing, life industry, and automation are some industries in which rotary encoders are used regularly. It is used in controlling the speed of the conveyor belt as well.
How does Rotary Encoder Works?
There is a disc placed inside the encoder which is connected to common pin C (GND) and other two separate contacts pin A and pin B.
So as you move the knob. pin A and pin B make contact with common pin C in a specific order depending on which way you are rotating the knob.
Signals are produced when they come into contact with common pin C. Due to the fact that one pin comes in contact with the common ground before the other, these signals are 90° out of phase with one another. This is known as quadrature encoding.
When the knob is turned in the clockwise direction, pin A gets connected to the ground before pin B. Whereas when the knob is turned anti-clockwise, pin B gets connected to the ground before pin A
We can detect which way the knob is being turned by keeping a note of each pin’s connection and disconnecting from the ground.
How Rotary Encoder different from Potentiometer
Rotary encoders are the modern digital equivalent of potentiometers and are more versatile than them.
Rotary encoders can rotate 360° without stopping whereas potentiometers can only rotate 3/4 of the circle.
Potentiometers are used in situations where you need to know the exact position of the knob. Whereas, rotary encoders are used in situations where you need to know the change in position rather than the exact position.
Hardware Overview
The Rotary Encoder appears with a disc-like structure with an LED light source, also having a light detector and output signal processor. There are two types of rotary encoders – absolute and incremental. The absolute encoder reports the exact position of the knob in degrees while the incremental encoder reports how many increments the shaft has moved.
Features of the sensor :
- The output is controlled based on the shaft’s rational displacement.
- With an incremental encoder, the timing of phase A and B’s output determines the rotation direction.
- Absolute encoders do not require a re-start after going back to origin.
- We can also determine the rotation’s direction.
- Select the best sensor from a variety of resolution and output options.
PinOut of Shaft Encoder
Shaft Encoder has total of 5 pins and their functions are:-
GND – It is the ground connection.
VCC – VCC is 3.3 – 5 volts of the positive supply voltage.
SW – SW is for pressing down the voltage. It is basically the active low push button switch.
DT – The DT (output B) is similar to CLK output but it lags behind by a 90-degree phase shift. The direction of the rotation is determined by this output.
CLK – CLK is the output pearls to determine the amount of rotation. Every time when the knob is turned in the other direction by just one detained the CLK output goes through one cycle of going high and then low.
Materials required
SL. No. | Material Required | Unit Used |
1. | Rotary Encoder | 1 |
2. | Arduino | 1 |
3. | wires | As per requirements |
4. | Breadboard | 1 |
Circuit connection
Let’s link the Arduino to the rotary encoder. The relationships are rather straightforward. Start by connecting the module’s +V pin to the Arduino’s 5V input and its GND pin to the ground.
The CLK and DT pins should now be connected to digital pins #2 and #3, respectively. The SW pin should now be connected to digital pin #4.
CODE
// Rotary Encoder interface with arduino
//
//
//check more cool projects on www.electronicsmith.com
#define CLK 2
#define DT 3
#define SW 4
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}
FAQs
What principles of measurement does a rotary encoder use?
Rotational encoders count the number of rotations, as well as the rotational angle and location. It is also possible to measure linear movement with linear encoders. In response to the shaft’s amount of rotational movement, this sort of encoder produces a pulse string.
How is encoder accuracy calculated?
Encoder accuracy is measured in arcminutes or arcseconds, with an average high accuracy encoder being 20 arcminutes (0.3 degrees) or better, and certain precision devices being on the order of 5 arcseconds (0.0014 degrees). There are various error sources that can reduce the accuracy of an encoder reading.
What are the limitations of an encoder?
Encoders’ greatest drawbacks are that they sometimes have delicate elements and can be rather sophisticated. This limits their allowed temperature and reduces their tolerance to mechanical damage. It would be difficult to find an optical encoder that can withstand temperatures higher than 120°C.
How do I increase encoder resolution?
However, signal decoding allows for an incremental encoder’s resolution to be raised. Square wave signals are produced by incremental encoders, and by counting both the leading and trailing edges of a single signal (signal A), the encoder’s resolution is doubled.
Does a rotary encoder need a resistor?
Since you would use the internal pullups on the pins and operate without a VCC supply, resistors are typically not required.
2 thoughts on “How does a Rotary Encoder Work And Interface It With Arduino?”