Introduction: How Rotary Encoder Works With Arduino!
A rotary encoder is a great input device for any project such as a 3D printer's menu. So in this tutorial, we will learn how a rotary encoder works and how to use it with Arduino.
So let's get Started!
Step 1: Watch the Video !
If you don't want to read all the stuff you can watch my video it will be much easier to understand.
Step 2: Overview
A rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to an analog or digital output signals.
There are two main types of rotary encoder
1) Absolute - The output of an absolute encoder indicates the current shaft position, making it an angle transducer.
2) Incremental - The output of an incremental encoder provides information about the motion of the shaft, which typically is processed elsewhere into information such as position, speed, and distance.
Note: One we use in our project is Incremental type so we will focus on that.
Step 3: What Is Incremental Rotary Encoder ?
An incremental encoder will immediately report changes in position(Clockwise & Counterclockwise), which is an essential capability in some applications. However, it does not report or keeps track of absolute position. As a result, the mechanical system monitored by an incremental encoder may have to be moved to a fixed reference point to initialize the position measurement.
Step 4: Construction & Working
One I have comes in this breakout board which already have the required pull-up resistors on it.
It has five pins - Clock, Data, Switch, Vcc, GND
The knob which we rotate is internally connected to the disk(Pic - 2). If we rotate clockwise or counter-clockwise it will move accordingly. The grey portion is GND and Golden contact points are connected to Vcc. There are two contact point placed at a specific distance apart which are nothing but our CLOCK and DATA line.
As we rotate our encoder, the two output will change depending upon the position of the encoder. Which will generate two trains of pulses.
If you look closely those two signals will be 90 degrees out of the phase. If the encoder rotates clockwise then CLOCK will lead and if the encoder rotates counter-clockwise then DATA will lead.
And taking a look at state change for clockwise two signals will have opposite values and for counter-clockwise same values.
And now if we programme our Arduino accordingly we can get it to work with our project.
Step 5: Programming
Example 1: (Counter.ino)
Now in this example, CLK is connected to pin 3, DT to 4 and SW to 5, then we have some variables to store data. In void setup section CLK, DT and SW are set as input and then started the serial command and here stored the current position in the last state
In void loop section first, we read the current state of the clock and using it else check it if it has changed.
If the state != last state means it has changed the position. And if data != state then CW else CCW which will increase or decrease counter accordingly.
Then using serial print printed the counter variable. And if the switch is pressed counter will reset to 0 and print it, in the end, put the state into the last state
Example 2: (LEDBrightness.ino)
In this example, I have put the counter value in analog write function and used constraint function to limit the rage between 0-255 in order for it to work
Note: it's better if you open the code in Arduino IDE and read it once
Step 6: Thank You !
Check out JLCPCB
$2 PCB Prototype (10pcs,10*10cm): https://jlcpcb.com
That's it if you like my work
Feel free to check out my YouTube channel for more awesome stuff: https://www.youtube.com/c/Nematics_lab
You can also follow me on Facebook, Twitter etc for upcoming projects