How to use rotary encoders

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so what is the most important thing about an electronic circuit I would say that we can use it as seamlessly as possible and for that we need special devices like buttons and knobs and that sort of thing now on this channel we have learned how to use Simple push buttons slightly more complicated push buttons like those built into a game controller we have built our own capacitive touch sensor and we have learned how to read out the position of a potentiometer and today I want to add the rotary encoder to this list specifically an incremental rotary encoder with a built-in push button now you can find these devices everywhere like in your car's radio on the bench in your power supply in your oscilloscope or even in the scroll wheel of your mouse so today let's learn how these rotor encoders work and how we can use them in our Electronics projects to see how they work let's plug one into a breadboard and connect our 4.5 volt battery pack to the power rail so that vdd is on red and ground is on blue and now we can just connect these terminals to LEDs the upper two terminals here are just the push button so when we insert an LED and a resistor and push down the encoder we can turn the LED on and off so that's pretty simple but the lower three terminals are more interesting they are called a b and c c is the common contact and usually located in the middle between a and b for now let's connect this C terminal to ground next let's place a yellow LED between terminals a and vdd and a blue LED between Terminal B and vdd and now we can simply turn the rotary encoder and watch what happens when we turn the rotary encoder clockwise a special sequence shows up on the LEDs and when we turn it counterclockwise instead a slightly different sequence shows up instead this happens very fast so here is a slow motion version of what we saw before clockwise first and then counterclockwise here is how these patterns look like as a diagram clockwise goes to the right and counterclockwise goes to the left this pattern is called gray code and by comparing the current code to the previous code we can always uniquely determine the direction in which the encoder was turned now if you find this whole gray coating confusing then imagine instead that the left arm of the student is pin a and the right arm is pin B when the motion is from left to right she moves up a and then B and when the motion is the other way around she moves B and then a and this is exactly the same idea as in Gray code but you gotta ask what makes gray code so special right if you look at the timing diagram you see that from one step to the next only one pin changes never both at the same time now why is that useful well if one of those context bounces and doesn't give a clean signal then the worst thing that could ever happen is that we get off by one step but never more than that and that is just really really useful in practice and last many rotary encoders have little stops built in and the one that we're using today has one stop every four steps that's that little bump that you can feel when you're turning a rotary encoder and you can actually also hear it and later in the video we want to keep that in mind so let's take our rotary encoder apart and see how it generates this code internally this upper aluminum part here is held in place by some tabs that we can carefully Bend aside and then the whole thing comes apart into three functional pieces in total the knob a contact wheel and the base which contains sliding contacts as well as a dome switch in the middle pushing down the knob pushes down the metal dome which then connects these two terminals here at the top of the base so the push button part is quite simple but what about the rest if you look closely at the metal sliders on each side you'll see that they each consist of three prongs the other ones go to pin a and pin B on either side but the middle ones connect to the center Pin C so if you place something conducting on top of one of these prongs they act as simple switches with a common contact C and this is where the contact wheel comes in the shiny Parts on the contact wheel are conducting and the black parts are insulating when we superimpose this onto the base and slowly rotate the wheel we see that the shiny Parts close the A and B switches depending on the position of the wheel and the repeating pattern makes sure that the switches are turned off and on exactly in the gray code pattern and if you flip the contact wheel around you see that it is indexed with little bumps and during normal operation those are pressed against a copper spring in the knob base acting as little detents so this is what generates the bumpy feeling when turning the rotary encoder and if you don't like that just remove the copper spring and you'll have a seamless rotor encoder because this is all so small I decided to build a bigger rotary encoder so we can take another look at it this toy car wheel here is ideal as a contact wheel because it has the same repeating pattern if we now take some simple limit switches and arrange them in just the right way we can make them spit out gray code just as well it is a bit fiddly to get it right as you can tell from these holes here that I didn't end up using but after connecting our yellow and blue LED from before we see that turning the wheel clockwise and counterclockwise generates exactly the same LED pattern as our small rotary encoder that we just took apart so a rotary encoder is basically two switches A and B with a common contact C that are activated in a funny pattern when the rotary encoder is turned and there is of course also the push button one way to read out this status with a microcontroller is to connect the common pin and one side of the push button to ground and to connect a b and the other end of the push button to vdd with pull-up resistors this way when the switches are open the microcontroller reads A logical one and when the switch is closed it reads A logical zero also in our case we don't actually need to install these pull-up resistors because the big 16 f1455 has built-in ones that we can activate in software okay and speaking of that if you want to follow along the next steps then here's what you need the pix16 f1455 microcontroller a rotary encoder and a knob a breadboard and battery pack two capacitors and 5 LEDs and resistors you also need the pick kit 3 and the connecting cable and as always you can find links on where to buy all of these components in the companion article on friendlywire.com the link is in the the description and in our final schematic we have our pix16f1455 microcontroller here and as usual we run it off of a 4.5 volt battery pack with a bulk capacitor and a 100 Nano Hertz bypass capacitor for stability this here is our rotary encoder and we connect the common pin to ground and its two terminals A and B two inputs of the pix16 f1455 and we do the same with the internal push button then we also have some LEDs that we will use to make sure that our rotary encoder works properly and this symbol up here is the picket 3 that we need to transfer our program onto the microcontroller and it is connected to the pix16f1455 Via bdd and ground as well as with the three Wireless master clear program data and program clock just as usual place the 400 pin breadboard in front of your with Row 1 facing to the top insert the pix16 f1455 in Row 3 with its Notch facing towards the top as well connect vdd at Pin 1 grounded pin 14 and make sure that the power rails on either side of the breadboard are connected to each other insert the bypass capacitor C2 between pins 1 and 14 of the pix16 at 1455 and the chunky bulk capacitor C1 in the power rail making sure that the negative terminal is connected to the blue ground rail next place the rotary encoder in row 12 with the three ABC terminals facing to the left and the two push button terminals to the right I find that sometimes these rotary encoders don't really fit snugly into breadboards but if you twist the leads by 90 degrees with some pliers they go in a lot more easily alright after this brief Intermezzo it's time to insert the LEDs with the anodes facing up and their cathodes going into ground via their current limiting resistors pin a of the rotary encoder goes to pin 3 of the pick pin B goes to pin 2 and Pin C goes to ground on the other side connect one of the button terminals to pin 4 of the pick and the other one to ground and you can do that either way around it doesn't matter because it's just the push button and last it's time for the Led anodes Led 3 goes to pin 10 led1 goes to pin 8 LED 2 goes to pin 9 LED 4 goes to pin 7 and led 5 goes to pin 6. now you can place the aluminum knob on the rotary encoder and connect the 4.5 volt battery pack to the power Rail and connect the picket 3. the connections of the picket 3 are a bit hard to see so pause this screen if you want to see the details and to make it a bit easier I try to match the colors of the wires to those in the schematic and last plug the USB end of your picket 3 into your computer and then we're done with this circuit but of course now we have to think about how to program the microcontroller to actually read out the rotary encoder now for this video I thought it would be nice to just talk about the main idea of how the program works but if you want a detailed line-by-line explanation go check out the companion article on friendlywire.com now if you don't care so much about programming then just jump to this timestamp right here and I'll see you in a couple of minutes so here is the main idea of this program and here are the two parts first we have to read out the rotary encoder and second we have to control the LEDs when the program first starts up only the leftmost LED is on and by turning the rotary encoder we can move the glowing LED around and when we press the built-in push button just for fun we invert all LEDs of course this is just a demo and in the real world we would do something more useful but you get the idea so how do we read out the rotary encoder the idea is to Define a variable called V that we set to zero at the beginning when we turn the rotary encoder clockwise we increase it and when we turn it counterclockwise we decrease that variable we have five LEDs and our rotary encoder does four small steps for each of those tactile bumps that we feel so the variable V will be between 0 and 19. so we have reduced our problem to a simple question how do we figure out the direction in which the rotary encoder was turned so as we saw gray code is super convenient when it comes to error correction and it even has a nice intuitive interpretation but microcontrollers don't understand gray code directly so the first thing we do is convert the four possible states of gray code into ordinary numbers from 0 to 3 and that is what this function here does it literally just goes through the four possible cases of gray code and gives us the corresponding number based on whatever state the two pins A and B have now in a second step we need to check if the status of the rotary encoder has changed and in our program we do that with a so-called interrupt service routine or ISR for short and you can see that routine here on the screen right now the way it's set up this function is called automatically a few thousand times per second and whenever it is called it reads out the current position of the rotor encoder here with the conversion function we just talked about so the current position of the rotary encoder is stored in the variable POS which is an abbreviation for position as a number between 0 and 3. then we calculate the difference between the position that we just read to the previous position which is stored in the variable POS underscore before we can look at our Gray diagram and put in the converted numbers as well and then we can see that a clockwise rotation means a difference of negative one or three and the counterclockwise rotation means the difference is either 1 or negative 3. and back in the code this is exactly what these lines check here if we turned clockwise we increase the value of v and if we turn counterclockwise we decrease it instead in all other cases either nothing happened because the difference is zero or something went wrong and in this code here we will just ignore it and that's it okay so now we have converted the position of the rotary encoder into our variable V that is between 0 and 19 and when we divide it by 4 we get a number between 0 and 4 and that tells us what led has to be turned on in the program we do all of that in our main Loop that is a simple structure that just goes on and on and on forever in this line here we do our division by 4 and in these if statements here we just check for all five different cases now the funny looking SW stores the inverted status of our push button this means that the expression exclamation mark SW is 1 whenever the push button is pressed and it is zero if it is not pressed if SW is 1 then this is what this code looks like and if SW is zero then it looks like this instead so all that this SW part does is make the code look a bit more complicated but it takes care of inverting the LED pattern in a compact way now the whole source code is a bit longer and it contains pieces that set the speed the program runs at sets up the inputs and outputs as well as the interrupt service routine and other things if you want to learn more about it check out the companion article on friendlywire.com but using the mplab IDE we can now compile this code into a hex file and we are ready to flash it onto the pix16f1455 using the picket 3. to all of you who jumped here from earlier in the video Welcome Back we just went through the main idea of the code and now we have our hex file that's basically the code that the microcontroller can understand and you can download that file directly from my website no coding needed and the link is in the description now start the mplab ipe select the pix16 f1455 under device and click on apply and then select the pickkit 3 as your tool click on connect at the pick 16 f1455 should be detected within a few seconds now click on browse and select the hex file we just downloaded and click on program the picket 3 LEDs start blinking and after a few seconds we are done we cannot disconnect the picket 3 from the breadboard and play around with the horror rotary encoder sometimes when we move it very fast it misses a step but this can happen because this rotary encoder here is really not a Precision device but you know for everyday use it should be fine and at this point we can remove the contacts from the tiny onboard rotary encoder and connect the pix16 f1455 instead to our homemade rotor encoder that we built a few minutes ago that looks a little bit messy but it works just as well let me know if I could convince you to use a rotary encoder in your next project I really hope you enjoyed this tutorial and if you did feel free to have a look around on my channel my name is Jens this is friendly wire thank you so much for watching let me know what else you want to learn and I'll see you next time
Info
Channel: FriendlyWire
Views: 29,977
Rating: undefined out of 5
Keywords: rotary encoder, rotary encoder microcontroller, Gray code, PIC16F1455, PIC microcontroller rotary encoder, rotary encoder tutorial
Id: 9j-y6XlaE80
Channel Id: undefined
Length: 14min 11sec (851 seconds)
Published: Sat May 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.