Introduction
Rotary encoders are a fundamental component of many modern systems, from robotics to CNC machines. They offer a simple yet accurate way to track rotation and angular position, which is crucial in controlling motor speeds and positions.
In this article, we’ll explore how to interface a “Quadrature Rotary Encoder” with an Arduino microcontroller and learn how to use the encoder’s output to control motor position.
What is Quadrature Rotary Encoder?
A Quadrature Rotary Encoder is a type of rotary encoder that generates two square wave signals that are out of phase with each other. These two signals are called A and B channels, and they are used to track the rotation and direction of a rotary shaft. The encoder typically has a disc with evenly spaced slots or marks around its circumference, which is attached to the rotary shaft. As the shaft rotates, the slots or marks interrupt an optical or magnetic sensor, which generates the A and B signals.
The A and B signals are out of phase with each other by 90 degrees, which means that their waveforms are shifted in time by a quarter of a cycle. This phase relationship allows the encoder to determine the direction of rotation, as well as the amount of rotation.
Hardware Components
You will require the following hardware Interfacing a Quadrature Rotary Encoder with Arduino.
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Creamic Capacitor | – | 2 |
Resistor | 1KΩ | 2 |
Quadrature Rotary Encoder | – | 1 |
Jumper Wires | – | 1 |
Quadrature Rotary Encoder Pinout
Quadrature Rotary Encoder Arduino Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | Quadrature Rotary Encoder |
---|---|
D5 | OUT A |
GND | GND |
D4 | OUT B |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5
// LED Outputs
#define ledCW 8
#define ledCCW 9
int counter = 0;
int currentStateCLK;
int previousStateCLK;
String encdir ="";
void setup() {
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
}
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter --;
encdir ="CCW";
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);
} else {
// Encoder is rotating clockwise
counter ++;
encdir ="CW";
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);
}
Serial.print("Direction: ");
Serial.print(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
Code Explanation
This Arduino code sets up the pins and initializes the variables for reading input from a rotary encoder, which has two input pins: inputCLK and inputDT. The code also sets up two LED output pins, ledCW and ledCCW, which will indicate the direction of rotation.
In the setup()
function, the input pins are set as inputs and the LED pins are set as outputs. The serial monitor is also set up to display the direction of rotation and the counter value. The initial state of inputCLK is read and assigned to the previousStateCLK
variable.
In the loop()
function, the current state of inputCLK is read and compared to the previous state. If they are different, a pulse has occurred and the code checks the state of inputDT to determine the direction of rotation. If inputDT is different than inputCLK, the encoder is rotating counterclockwise, and the counter and direction variables are updated accordingly. If inputDT is the same as inputCLK, the encoder is rotating clockwise. The counter and direction variables are updated, and the LED pins are set to indicate the direction of rotation.
Finally, the direction and counter values are printed to the serial monitor. The previousStateCLK
variable is then updated with the current state to be used in the next loop.
This code allows an Arduino to read the direction and speed of rotation from a rotary encoder and use that information to control other components in a system, such as motors or servos.
Applications
Here are some applications of Quadrature Rotary Encoders:
- Robotics: Used to control the position and movement of robot arms, grippers, and other components.
- CNC Machines: Used to control the position and movement of the tool head or workpiece.
- Printers: Used to control the position and movement of the print head and paper feed mechanisms.
- Industrial Automation: Used to control the position and speed of conveyor belts, motors, and other components in manufacturing and assembly lines.
- Gaming: Used in gaming controllers, such as joysticks, to track the position and movement of the controller.
- Audio Equipment: Used in audio mixers and other equipment to control the position and movement of knobs and sliders.
- Medical Devices: Used in medical equipment, such as MRI machines and surgical robots, to control the position and movement of components.
- Aerospace: Used in aircraft and spacecraft to control the position and movement of flight control surfaces and other components.
Conclusion
Quadrature rotary encoder with an Arduino is a straightforward process that can add a lot of functionality to your projects. By using the encoder’s output to control motor position, you can build precise motor control systems that are ideal for robotics, CNC machines, and more.