Optics Lens
how rotary encoder works with arduino!
by:HENGXIANG
2020-05-27
The rotary encoder is a good input device for any project (such as the menu for a 3D printer.
So in this tutorial we will learn how the rotary encoder works and how to use it with Arduino.
Let\'s start!
It\'s easier to understand if you don\'t want to read everything you can watch my videos.
The rotary encoder, also known as the shaft encoder, is an electric encoder.
A mechanical device that converts the angle position or movement of an axis or shaft into an analog or digital output signal.
There are two main types of rotary encoder 1)Absolute -
The output of the absolute encoder indicates the position of the current axis, making it an angle sensor. 2)Incremental -
The output of the incremental encoder provides information about the movement of the shaft, usually processed elsewhere into information such as position, speed and distance.
Note: We are using incremental types in the project, so we will focus on this.
The incremental encoder will immediately report the change in position (
Clockwise and anti-clockwise)
This is an essential capability in some applications.
However, it does not report or track the absolute location.
Therefore, the mechanical system monitored by the incremental encoder may have to move to a fixed reference point to initialize the position measurement.
I have a pull on this breakout board that already has the required-
There is resistance on it.
It has five pins.
Clock, data, switch, Vcc, gnd structure: the inside of the knob we rotate is connected to the disk (Pic -2).
If we rotate clockwise or counter-clockwise
It moves clockwise accordingly.
The gray part is GND and the gold touch point is connected to Vcc.
In addition to our clock and data lines, there are two contact points at a specific distance.
Work: When we rotate the encoder, the two outputs will change depending on the position of the encoder.
Two pulses are generated.
If you look closely, the phase difference between the two signals will be 90 degrees.
If the encoder rotates clockwise, the clock will boot, and if the encoder rotates back, the clock will boot-
The data then guides clockwise.
Look at the state change of the two signals clockwise. there will be opposite values.
The same value clockwise.
Now, if we program Arduino accordingly, we can get it to work with our project. Example 1: (Counter. ino)
Now, in this example, CLK is connected to pin 3, DT is connected to 4, SW is connected to 5, and then we have some variables to store the data.
In the void setup section CLK, DT and SW are set to input, and then the serial command is started, where the current position is first stored in the last stateIn void section, where we read the current state of the clock, and use it, otherwise check if it has changed. If the state !
= Last state indicates that it has changed its position. And if data !
= Then the statement will increase or decrease the CW else of the counter accordingly.
The print counter variable is then printed using serial.
If the switch is pressed, the counter will reset to 0 and print it, and finally, put the state into the last example of state 2 :(LEDBrightness. ino)
In this example, I put the counter value in the analog write function and use the constraint function to limit 0-
255 to make it work note: it would be better if you open the code in the Arduino IDE and read it after looking at the JLCPCB $2 PCB prototype (10pcs,10*10cm)
So in this tutorial we will learn how the rotary encoder works and how to use it with Arduino.
Let\'s start!
It\'s easier to understand if you don\'t want to read everything you can watch my videos.
The rotary encoder, also known as the shaft encoder, is an electric encoder.
A mechanical device that converts the angle position or movement of an axis or shaft into an analog or digital output signal.
There are two main types of rotary encoder 1)Absolute -
The output of the absolute encoder indicates the position of the current axis, making it an angle sensor. 2)Incremental -
The output of the incremental encoder provides information about the movement of the shaft, usually processed elsewhere into information such as position, speed and distance.
Note: We are using incremental types in the project, so we will focus on this.
The incremental encoder will immediately report the change in position (
Clockwise and anti-clockwise)
This is an essential capability in some applications.
However, it does not report or track the absolute location.
Therefore, the mechanical system monitored by the incremental encoder may have to move to a fixed reference point to initialize the position measurement.
I have a pull on this breakout board that already has the required-
There is resistance on it.
It has five pins.
Clock, data, switch, Vcc, gnd structure: the inside of the knob we rotate is connected to the disk (Pic -2).
If we rotate clockwise or counter-clockwise
It moves clockwise accordingly.
The gray part is GND and the gold touch point is connected to Vcc.
In addition to our clock and data lines, there are two contact points at a specific distance.
Work: When we rotate the encoder, the two outputs will change depending on the position of the encoder.
Two pulses are generated.
If you look closely, the phase difference between the two signals will be 90 degrees.
If the encoder rotates clockwise, the clock will boot, and if the encoder rotates back, the clock will boot-
The data then guides clockwise.
Look at the state change of the two signals clockwise. there will be opposite values.
The same value clockwise.
Now, if we program Arduino accordingly, we can get it to work with our project. Example 1: (Counter. ino)
Now, in this example, CLK is connected to pin 3, DT is connected to 4, SW is connected to 5, and then we have some variables to store the data.
In the void setup section CLK, DT and SW are set to input, and then the serial command is started, where the current position is first stored in the last stateIn void section, where we read the current state of the clock, and use it, otherwise check if it has changed. If the state !
= Last state indicates that it has changed its position. And if data !
= Then the statement will increase or decrease the CW else of the counter accordingly.
The print counter variable is then printed using serial.
If the switch is pressed, the counter will reset to 0 and print it, and finally, put the state into the last example of state 2 :(LEDBrightness. ino)
In this example, I put the counter value in the analog write function and use the constraint function to limit 0-
255 to make it work note: it would be better if you open the code in the Arduino IDE and read it after looking at the JLCPCB $2 PCB prototype (10pcs,10*10cm)
Custom message