In our last article, you may have learned how to connect a rotary encoder on Arduino development board UNO.
How to Connect Rotary Encoder with Arduino Board UNO?
For this article, it’s about how to use the rotary encoder connected on the Arduino UNO. We will show both clockwise and counterclockwise code values with signed numbers.
Figure 1: Arduino programming board UNO with a rotary encoder connected.
* Arduino UNO board
* Rotary encoder
* 1602 LCD display
* Wires
* Bread board
Rotary encoder, also known as shaft encoder, is a kind of electromechanical device, which can convert the angular position or movement of the shaft into analog or digital signal and output it to other receiving devices.
There are two types of rotary encoders: absolute encoders and incremental encoders.
The output of absolute encoders indicates the current axis position, making it an perfect angle sensor. The output of incremental encoders provides information about the axis movement, and its position is usually read as position, velocity and distance information.
Figure 2: Components and pin distribution of an incremental encoder on top and bottom side.
The following circuit diagram shows how to use rotary encoder on Arduino UNO. You can assemble circuits on a breadboard or PCB.
Figure 3: Circuit diagram of connecting rotary encoder and LED display screen with Arduino development board UNO.
There is a disk with evenly distributed contact areas connected to common pin C and two other separate contact pins A and B, as are shown below.
Figure 4: Working principle and output waveform of incremental encoder.
When the disk starts to rotate gradually, pins A and B will start to contact the common pin, therefore two square wave output signals will be generated.
If only the pulses of the signal are counted, either of the two outputs can be used to determine the rotation position. However, if we want to know the direction of rotation too, we need to read both signals at the same time.
Figure 5: Comparison of output waveform of incremental encoder when it rotates clockwise and counterclockwise.
We can see that the two output signals are 90 degrees different from each other. If the encoder rotates clockwise, output A will precede output B.
Therefore, if we calculate the steps of the signal from high to low or from low to high each time, we can see that the values of the two output signals are opposite.
And the vice versa, if the encoder rotates counterclockwise, the output signal will be of the same value.
With this in mind, we can easily program the controller to read the position and rotation direction of the encoder.
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- #define outputA 6
- #define outputB 7
- int counter = 0;
- int aState;
- int aLastState;
- void setup() {
- pinMode (outputA,INPUT);
- pinMode (outputB,INPUT);
- Serial.begin (9600);
- lcd.begin(16,2);
- // Reads the initial state of the outputA
- aLastState = digitalRead(outputA);
- }
- void loop() {
- aState = digitalRead(outputA); // Reads the "current" state of the outputA
- // If the previous and the current state of the outputA are different, that means a Pulse has occured
- if (aState != aLastState){
- // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
- if (digitalRead(outputB) != aState) {
- counter ++;
- lcd.clear();
- } else {
- counter --;
- lcd.clear();
- }
- Serial.print("Position: ");
- Serial.println(counter);
- lcd.setCursor(0, 0);
- lcd.print("Position: ");
- lcd.setCursor(10, 0);
- lcd.print(counter);
- }
- aLastState = aState; // Updates the previous state of the outputA with the current state
- }
10/25/2021
In our last article, you may have learned how to connect a rotary encoder on Arduino development board UNO.
10/24/2021
Rotary encoder is an input device to help users interact with the system. It looks like a radio potentiometer, but it’s unique that it outputs a series of pulses.
10/23/2021
As a speed type air compressor, centrifugal compressor can generate air flow moving in radial direction.
10/22/2021
Screw compressor is another type of commonly used compressor. We’ve listed the following 11 problems and solutions for reference of anyone or business in need.
10/21/2021
Piston compressors have an irreplaceable role in many areas but they are not easy to maintain. Therefore, it is very important for many companies to ensure smooth operation of this type of compressor.
10/20/2021
With the development of motor technology, compressor is considered as "general machinery", especially in coal chemical industry, machinery industry and other industries.
10/19/2021
As is known to us, rotary encoders consist of incremental encoders and absolute encoders. And absolute encoders are much more expensive than incremental encoders.
10/18/2021
What are the precautions for maintenance of rotary encoders? Rotary encoders are used as speed and displacement feedback sensors, which are mainly applied in servo motors, asynchronous motors
10/16/2021
A rotary encoder converts the angular position or motion of a shaft or axle to analog or digital output signals, so it is also called a shaft encoder. Absolute encoder and incremental encoder are the two main types.
10/14/2021
As both manufacturers and end customers know, in a refrigeration or air conditioning system, the compressor is the equipment core by continuously sucking back and exporting refrigerant
IWAYA DENKI Water Pump 25CJT0752
Panasonic Servo Motor MHMF022L1V2M
Honeywell Solenoid Valve VE4025B1045
ROTEN Mechanical Seal 8E5K-22-X
NMB-MAT Fan 04015KA-05M-AT
Copeland Compressor ZPT470KCE-TFD-425
NORGREN Air Cylinder RM/92020/JM/25
MINDMAN Air Cylinder MCJT-11-80-25