How to use Rotary Encoder with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
last week I posted a video showing how to use rotary potashometer with Arduino in a sample project we were using a pot to light up all LEDs in this 8x8 LED Matrix this week we'll look at Rotary encoders the electronic parts that are also the type of position sensors but they achieve the same objective in a completely different way if you want to see how rotary encoders work and how they differ from rotary potashometers stick around [Music] here is the rotary encoder it looks similar to Rotary potentiometer but there are some distinct differences encoder's shaft can turn endlessly in any direction unlike potoshometer shaft which can only turn by 270 degrees potentiometer shaft turns smoothly while encoders shaft gives some intermittent resistance when turning accompanied by the clicking sound also when pressing the shaft it works as a momentary push button which is not the case for the potashometer rotary encoder has five pins usual ground and 5 volt pins and then CLK and DT pins which are output pins that produce electronic pulses when the shaft is rotated you would ask why two pins and not just one this will be explained later in this video and finally SW is an active low push button output when the shaft is pressed the voltage on that pin goes low now let's look closer at the encoder when turning the shaft the signal at the CLK pin looks like this the signal at the DT pin looks very similar but it is not the same it lags behind CLK by 90 degrees phase shift why this shifted output is used to determine the direction of rotation this is actually pretty smart idea but before I will show you the method to figure out which direction the shaft is turning let's look at how the encoder is built and how the two out outputs the primary one and the shifted one are produced inside the encoder is a slotted disk that is connected to the common ground it also contains two contact pins when you turn the shaft those pins connect to the Common Ground pin in a particular order according to the direction in which you are turning the shaft when they come into contact with the Common Ground signals are generated these signals are 90 degrees out of phase with each other as one pin comes into contact with the common ground before the other pin so before we start writing the code to detect if the shaft is moved and in which direction let's connect the encoder to Arduino first starting with connecting ground to ground and positive pin of an encoder to 5 volt pin of Arduino then we connect CLK pin to Arduino digital pin D3 this pin can be configured to trigger interrupts if you are not familiar with using Hardware interrupts please check this recent video of mine so here we would use falling mode which means we'll check and react when the signal on digital pin D3 would change from high to low in this case ISR interactive service routine called shaft moved would be executed and finally we have DT pen connected to Arduino digital pin 4. let's do a quick simulation and see if we can figure out in which direction the shaft is turning using interrupt and the shifted signal read at the DT pin rotary encoder unlike potentiometer does not remember the current position it can be turned infinitely to the right and to the left so whenever you power the project that is using the encoder the current shaft position is the starting position and in the code it would be represented by counter variable this variable stores the relative count of impulses detected at CLK pen since the start in this example we have 0 as is the starting point shaft is turned to the right until we reach the moment in time where the signal on CLK pin drops from high to low that triggers the interrupt service routine and in this ISR we check what was the reading at the DT pin at the time when interrupt was triggered you could see that we read High signal and if you look closely you'd realize that this only happens when we turn the shaft to the right this way we just detected rotation direction the shaft is turning clockwise we also increase the counter by one if we continue turning the shaft to the right we'll get to the another situation where interrupt would be triggered again DT pin returns High signal direction is still clockwise and we increase the counter to two let's check what happens when we turn the shaft to the right when we get to the point where signal on clock pin goes from high to low we check and see what is the current signal on a DT pin and this time it is low that indicates that the direction is counterclockwise now we decrease the counter by one continuing we encounter yet another interrupt triggering event and the counter is now back to zero one more and you will see that the counter could go to negative territory when turning the shaft each time the signal changes on either of the two pins you hear and feel a distinct click so in this scenario the counter would change every two clicks of the shaft knowing what we know now we are ready to write simple code that will recognize the direction of the shaft movement and keep track of the relative position of the shaft since the schedule started so we need two variables one would be counter and we would choose to start at zero and the other one is the variable representing direction we start with blank value as at the start the direction is not yet known in setup we open the serial port and declare that interrupt at Pin 3 that will execute ISR called shaft moved and it will be executed when the signal at Pin 3 would change from high to low since we also need information from DT pin we declare it as input in Loop we only send the current values of Cam enter and dir variables to the serial monitor nothing more so all the fancy stuff happens in the ISR function when this function is executed we know that the signal at clock pin has just changed from high to low if we at this exact moment check the signal at DT pin and it is high we know that we are turning the shaft to the right and thus we increase the counter by 1 and set the dir variable to clockwise CW we do the similar if statement for the situation when we read low at the DT pin in this case we decrease the counter and change the direction to counterclockwise I always like to protect the ISR routine against glitches and situation where triggering event is interpreted as two triggering events and the ISR is performed twice unnecessarily that additional code makes sure that ISR is only performed when at least 5 milliseconds passed since the last execution of that ISR let's connect the encoder to Arduino starting with ground 5 volts then clock pin data pin and finally SW pen let's load the code and open the serial monitor you can see that you can increase the counter by turning the shaft right and decrease it by turning the shaft left the counter value can go below zero if you watched my video about potentiometers you would remember that at this stage we discussed map function where we would map the reading of the potentiometer that was within the potentiometer range to whatever range we needed in our code we cannot really use map function in this code AS encoder has an infinite range so if we wanted to control a sample range like this one 5 to 10 how could we do it for starters we have to change the initial value of the counter variable so it is set within controlled range I chose to set it to the lower end of the range in this example and then in ISR we change two lines of code so we only change the counter variable if we are moving within defined range when we move outside that range further turning on the shaft does not affect the comp and it stays at border value only when we move the shaft in the opposite direction the counter value can change again let's reload the changed code and check the result in serial monitor we start with counter value 5 and we are turning shaft clockwise we can increase the counter up to 10. if we turn shaft further to the right the counter value remains unchanged same with turning the shaft to the left we can go down to 5 and then the counter would not decrease further in the potentiometer video we are controlling the range from 0 to 63 which was representing LEDs in this 8x8 Matrix let's control a different range today in one of my videos I created the single digit module that was using shift register I will not explain how this works but if you want you can always check it out this video would explain the concept of using shift register to control multiple seven segment displays and those two would show you how I built that particular module for the purpose of this video the only thing you need to know is that there is function called display digit that outputs the desired digit you can find the link to the entire code including the shift register stuff in the description below connecting this module to Arduino is also of the topic let's create the project in which we control this display with rotary encoder with one extra twist pressing the push button will reset the range to zero with the module connected we can now write the code we'll not write it from scratch but rather adjust the code from the previous example the range is now from 0 to 9 so we need to adjust the initial counter value to 0 first also we need to update range border values in the ISR routine will not use dir variable so let's get rid of this declaration plus the two obsolete lines in the ISR code will not output anything to the serial monitor so let's get rid of print commands in the loop function we would call the display digit function instead to display the current counter value since we are not using serial monitor we do not need to open serial Port we are nearly done we need to program the push button to do it we Define the second interrupt on digital pin 2 which is the PIN to which we connected SW pin of rotary encoder it is also triggered upon the change of the signal from high to low since the push button is active low we need to activate built-in pull up resistor for the digital pin 2. we do it by declaring this pin as input pull up this interrupt has a different interrupt service routine called reset to zero which resets counter value back to zero let's load the code and see how this works we can control the display let's see the button works it does so generally encoder works but have you noticed every now and then the digit skips back to the previous value and this can be quite annoying I was looking for quite a while for a solution to this and the answer was to change the interrupt mode from falling to low I'm still unsure why this would make a difference but as you can see it clearly does if you can explain this to me by all means enlighten me in the comment section below so I think this is time to compare the encoder to potentiometer and pinpoint five major differences between the two first and foremost encoder is a digital component and potentiometer is analog so we use digital read method to read the value from encoder pin and we are able to use interrupts for potentiometer we need to use analog method to read analog values from the potentiometer pin potentiometer has a fixed range and remembers the current position within that range it also recognizes the direction in which the shaft is turning encoder does not have a fixed range and moving within the range then remembering the position and recognizing in which direction the shaft is rotating has to be programmed in the Arduino sketch thus potentiometers are used in situation where you need to know the exact position of the shafts whereas rotary encoders are used in situations where you need to know the changed in position rather than the exact position the reading from encoder is more stable that is of course after fixing the problem we encountered in this video values from a potentiometer fluctuate a bit we also had a problem and had to use tweaks to represent border values of the controlled range for potentiometers to control the desired range we used map function to convert the pots range to the desired range for encoder we cannot really use map function and to control desired range we need to code it and finally the encoder has push button functionality which can prove very useful at times and potashometer does not so if you need that functionality you need additional components in your project there here are five main differences have I missed anything and that brings us to the end of this video I hope you learned something new I definitely did thanks for all the likes and subscriptions they help this channel to grow still waiting for your super thanks you know where to find them see you in my next video over and out [Music] [Applause]
Info
Channel: Mario's Ideas
Views: 40,865
Rating: undefined out of 5
Keywords: Arduino, nano, rotary encoder, encoder, clk, dt, potentiometer, tutorial, how to, instructable, simple project, interrupt, ISR
Id: Z0B-FhelaJ8
Channel Id: undefined
Length: 14min 44sec (884 seconds)
Published: Sat Oct 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.