How to Use Rotary Encoder on Arduino Board UNO

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.



rotary encoder and LED display screen 1602 connected to Arduino programming board UNO

Figure 1: Arduino programming board UNO with a rotary encoder connected.

1. Components Required

* Arduino UNO board
* Rotary encoder
* 1602 LCD display
* Wires
* Bread board

2. Rotary Encoder

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.



illustration of components and pin distribution of an incremental encoder on top and bottom side.

Figure 2: Components and pin distribution of an incremental encoder on top and bottom side.

3. Wiring

The following circuit diagram shows how to use rotary encoder on Arduino UNO. You can assemble circuits on a breadboard or PCB.



illustration of circuit diagram of connecting a incremental encoder and LED display screen with Arduino development board UNO

Figure 3: Circuit diagram of connecting rotary encoder and LED display screen with Arduino development board UNO.

4. How the Rotary Encoder Works

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.



working principle and output waveform marked as Output A and Output B of incremental encoder

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.



illustration of waveform of incremental encoder when it rotates clockwise and counterclockwise.

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.

5. Source Code

  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  3. #define outputA 6
  4. #define outputB 7

  5. int counter = 0;
  6. int aState;
  7. int aLastState;

  8. void setup() {
  9. pinMode (outputA,INPUT);
  10. pinMode (outputB,INPUT);

  11. Serial.begin (9600);
  12. lcd.begin(16,2);
  13. // Reads the initial state of the outputA
  14. aLastState = digitalRead(outputA);

  15. }

  16. void loop() {
  17. aState = digitalRead(outputA); // Reads the "current" state of the outputA
  18. // If the previous and the current state of the outputA are different, that means a Pulse has occured
  19. if (aState != aLastState){
  20. // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
  21. if (digitalRead(outputB) != aState) {
  22. counter ++;
  23. lcd.clear();
  24. } else {
  25. counter --;
  26. lcd.clear();
  27. }
  28. Serial.print("Position: ");
  29. Serial.println(counter);
  30. lcd.setCursor(0, 0);
  31. lcd.print("Position: ");
  32. lcd.setCursor(10, 0);
  33. lcd.print(counter);

  34. }
  35. aLastState = aState; // Updates the previous state of the outputA with the current state

  36. }

Search for rotary encoders for Arduino UNO on OKmarts



shop for leading brands of incremental encoders for Arduino development board UNO on OKmarts

Recent related posts

Submitted Successfully
Submission Failed