How to use encoders (Optical, Hall Effect, Quadrature)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
ahead of several future projects I wanted to create this tutorial on how to use various types of encoders specifically single phase encoders and quadrature encoders using both optical sensors and Hall effect sensors a lot of the great tutorials that I've seen online take a theoretical approach to how encoders work going through all the pulses and determining the direction of rotation and so forth and I'll definitely touch on all those things but I also want to give a practical demonstration as well as an explanation of the software and how it'll work to actually read this sensor so that it can be scaled to any arbitrary type of encoder these are the three encoders that I have on hand and I'll be using them to demonstrate various aspects of encoders at its simplest form an encoder is an electromechanical measurement device that gives information about the angular position of the rotating axis so as you can see just it's a disc that spins around and then there's going to be some sensors that we'll come to in a second and those sensors are going to give information about the position of this and the rotation over here so this one is just a single-phase encoder this one instead is gonna be a quadrature encoder as you can see it's the same principle as the previous motor engine code or it's a DC motor with a gearbox axle coming out this one's interesting in that the rod extends out both ends so you can see it comes up here so it'll be spinning both ways and then this is the encoder circuit and it's a little hard to focus in on but are these two chips here a Hall effect sensors and so they're gonna detect changes in the magnetic field specifically the magnetic field of this disc as it spins around and so I will come back to all of this and explain it in more detail I just kind of want to give you a brief overview of the different types we have here finally there's this one it's a quadrature encoder so similar to this one it's got two sensors 90 degrees out of phase to each other except instead of using magnets this one is again like this an optical and part so we'll start off here and then kind of build up and touch on all these other ones if you want to jump to a specific type of encoder and see how to use it see the software and so forth I'll put timestamps in the description down below so you can jump around the video a bit one quick point to clarify is that encoders don't necessarily need to be attached to motors these two obviously are attached to a motor and the reason for that is just that you can get information about the RPM of the motor which will be useful if you want to determine the speed at which the wheels spinning and so forth but in other applications you may not want the encoder directly attached to the motor you could have this on a free spinning axe or something and that's where this encoder would come in so yeah well while it does look like there may be a motor and a gearbox in there there's nothing in there it's just a free spinning axle here and this entire piece is the encoder so to begin our exploration encoders I'm gonna start off with this simple entry-level encoder as I mentioned this is just an optical disk and what I've built here is a small circuit so this white or rather clear LED over here it's a little hard to see but it's just above the tip of my finger is an infrared LED so that emits infrared light so as humans we can't see that but it is sending out light and then this black it looks like an LED to is an infrared receiver so basically if this is turned on and there's nothing blocking it then this infrared receiver will read a state of high if something's blocking it say my finger then no infrared light is getting to this receiver and it will be read in a state of low so basically I've connected it up to an Arduino here and then the output of the receiver is connected up to digital pin 2 which is an interrupt pin on the Arduino if you're familiar with what that is that's great if not I'll come back to that in a second but it's important that this is connected to either pin 2 or pin 3 on the Arduino Uno so I've gone ahead and powered up the board and as I mentioned before as a human we can't actually see the infrared light that's coming out of here and neither can we see it through the lens of my camera even if I make it darker in there you still won't see any infrared light coming out of that LED it's because there's an infrared filter built into the camera my phone while it might have an infrared filter it definitely isn't as great and so if I line this all up correctly you can see especially if I make a darker you can see the infrared light coming out of that emitter there so by positioning the optical encoder in between the infrared emitter and receiver essentially as this rotates around either lights going to be allowed through when there is a slot or it's going to be blocked when there's a piece of solid acrylic so I have my power supply over here I'm gonna use that just to connect up to the motor I'm gonna go ahead and turn it on let's move to the side for now this is definitely a bit of a sketchy setup but I'm just kind of for demonstration purposes here when I go ahead and power it up if we go take a look at the output received on the arduino ide we can have a look at those pulses as they're coming through you can see we get this nice clean square wave every time it's hired the lights shining through the slot and then when it's low the lights being blocked out so I guess the question now is how do we actually get this output onto the computer in the first place so I'm gonna dive into the short little program that I wrote for this as it's not very long at the start of the program we're defining the pin the encoders attached to in this case it's digital pin 2 like I said before it either needs to be digital pin 2 or digital pin 3 the line that I'll bring your attention to is in the setup routine specifically the attach interrupter command if you're not familiar interrupt commands are very useful when you are sampling a lot of data from a sensor very quickly and you want to make sure that you don't miss any of the inputs for example further down in the program you'll see that I'm printing out the value of the encoder this is actually quite a slow process and comparatively speaking takes a lot of time and so if we would execute that without doing anything else then would actually be missing pulses as they came through on the infrared receiver so what the interrupt routine does is it basically prioritizes the input from the sensor and any time the state changes specifically when it's rising going from a low state to a high state it's going to prioritize the function called pulse and this function is going to increase the count or the number of pulses that we've tracked so now if I go ahead and comment out this line and uncomment this line we'll see how the output changes specifically they were no longer getting a square wave out instead it's gonna steadily rise upwards as the count increases or the total number of pulses obviously as you saw earlier my setup for this experiment is a little precarious so the output is unstable but you'll see in the later part of this video we do get stable output when the encoder is properly installed now that we've finished discussing this simple hobby optical encoder I want to move on to other encoder mounted on a motor except this time it's a quadrature magnetic encoder so instead of having slots like in the optical encoder this one has instead alternating magnetic domains in this black disc that spins around if I go ahead and just pull this off the end it's a little bit tired being magnetic you'll notice that it catches on to the metal spine of my notebook in order to see those magnetic domains I'm just going to pour some iron filings on top of the magnet so as you can see the iron filings have moved to align with the magnetic field lines and in the center here we have six distinct clusters of iron filings so these are alternating north-south priority domains so every North Pole here has a neighboring South Pole on each side by knowing that there are six domain changes in this disk we can count the pulses sent from the magnetic sensor to the Arduino to figure out the RPM as well as the direction of rotation in the corners these two black surface mount devices are Hall effect sensors and what they do is detect changes in the magnetic field specifically the magnetic field of the disk as it's spinning around and they'll send a signal out through these wires to the Arduino when they detect a change in the local magnetic field these two sensors are out of phase by 90 degrees and what that enables us to do is actually determine the direction of rotation of the disc as its spinning whether it's going clockwise or counter clockwise this was something we couldn't do with the previous optical encoder because it only had one channel we'll go have a look at the output of these two devices to see how we can determine the direction of rotation of the encoder so just like in the previous example the two outputs are square waves let's call one of the Hall effect sensors channel a and the other sensor channel B you'll notice that because the sensors are out of phase by 90 degrees to each other the puts up also 90 degrees out of phase from each other so how can we determine direction well let's say for arbitrary sake we're travelling clockwise in this direction by looking at the rising edge pulse of output a so that's going to be every time that output a changes from a low state to a higher state so here here and here and comparing it to what channel B is saying at that exact time we can see that when it's going from low to high output B is high same here output B is high and here output B is high now what if instead we were traveling in the opposite direction I'll indicate this with the green arrow we still want to look at the output from when it goes from low to high but because we're going in the opposite direction it's actually gonna be this rising edge here and you can see that every time that channel a goes from low to high output B isn't a low state output B is in a low state output B is in a low state and this is always going to happen so by writing a program that looks for every time a transitions from low to high and then checking whatever the state of B is we can determine whether it's spinning clockwise they will always be high or if it's spinning anti-clockwise B will always be low furthermore just like before by counting the number of pulses we can determine the RPM and the total number of revolutions the last thing about having two outputs is that we get a higher resolution than before so in this circuit the mode is connected up to the motor driver which is going to control the speed and direction of rotation then the two output signal lines from the two sensors are going to digital pins 3 and digital pins to the motor drive is controlled by these two signal lines from digital pin 10 and 11 and then finally there's logic level power going at 5 volts to the sensors on the motor i've connected an external power supply to power the motor driver and by extension the motor itself as it's never really a good idea to draw power directly from the Arduino so when I go and upload this code and power it all on here we can see the output from the two different channels as I drew previously and you'll notice that they're out of phase by 90 degrees so let's go into the software and see how we can change it to count the number of pulses as well as the direction of rotation so overall you'll notice that this code is fairly similar to the previous code that I demonstrated we're defining the encoders the mode are the counting everything we've kind of doubled up because this time we have two sensors instead and here we are declaring the direction variable and just the conventions sake I'm going to define one as being clockwise - one being counterclockwise in the setup it's all pretty standard stuff just declaring the pin modes like before we're using the interrupt service routine on the rising pulses so that's going from low to high on both of the encoder sensors and that's gonna run routines pulse and pulse B which are defined down the bottom here recall from earlier that we wanted to check what the output of B was when a was rising from low to high therefore every time that we get a rising signal from pulse a we're gonna want to check the direction the check Direction function is Ferries fairly short and what this is doing is basically comparing encoded B and seeing if that one's in a high state as we said before we know that it's spinning clockwise so we're going to set the direction to 1 or clockwise and then if encoder B isn't in a high state it's obviously gonna be in a low state so we're gonna say it's moving counterclockwise but we're only gonna be checking the direction when we called pulse a because as I mentioned before we just want to be checking what the output of B is when a is our rising pulse and lastly we'll just increment the count of pulses by adding the direction so if it's spinning clockwise that's going to be 1 so we're going to be increasing higher higher and if the output is minus one then the count is going to be decreasing so this is just an arbitrary convention that I've chosen for myself and in here I just have a selection of different things that we may want to print out and see as a user so in order to see those two square wave pulses I was just using these two functions if we want we can go ahead and comment these out and uncomment these go ahead and upload this and then when we hope to see the output so now when I go and power on the motor we'll see that it's steadily declining and that's because it's spinning anti-clockwise I take a second and pause here and redo the wiring and make it spin in the opposite direction you'll see it's starting to climb back up and obviously you can change the direction and the speed inside the software but for these purposes I didn't really want to focus on that and there you have it last but not least we have this optical quadrature encoder which is very similar to the previous magnetic quadrature encoder except that it's not mounted to a motor and it's just a free spinning axle whereas this smaller quadrature encoder only had six pulses per revolution this one actually has a thousand and 24 per revolution so that gives it a much higher precision or accuracy and one other nice feature is that in addition to having output a and output B it has a third output Z and output Z it only sends a signal once per revolution so most of the time it's just going to be continuously flat and then once per revolution it's going to send out a single pulse and that single pulse out of the thousand and 24 of all these other pulses can be used for homing the position of this upon initial startup you have no way of knowing exactly where an encoder is because the square waves just look symmetrical regardless of your current angle so instead if you power up the encoder and then slowly spin it until it hits this pulse then you know that you're at the same exact position each time and you can go from there track your position relative to that initial coordinate so the wiring for this one is fairly straightforward we just have the three outputs this time so digital pin to digital pin 3 and digital pin 4 and then the power outputs as well overall this program is pretty much the same as the previous ones there's a few important changes but they're kind of beyond the scope of this video so instead I've written about them in a blog post that I'll link in the description down below primarily the issue is that the interrupt pins available on the Arduino only digital pin 2 and digital pin 3 but because we have a third channel output we need somehow to get an additional interrupt pin and that's what this library here handles and you can find more details about that in the description down below but essentially yeah we're just using this special function to enable the interrupt pins because the third channel only puts out one pulse per revolution we can use this as a homing function and to do that we can just reset the two counters for some purposes you may also want to have a cumulative count which won't clear after each rotation so you can count multiple rotations you'll notice that the check Direction function is the same from last time as well because we still want to figure out what direction we're spinning and we're just outputting the interesting variables here so I'll go ahead and upload this and we can see what the output looks like so here you can see the rotation right here approximately here is our initial starting position actually not I'm gonna put a piece of tape on the output so that you can see the orientation CS with a little bit of it obviously the resolution on this is very very good so it's hard to get the precise zero point but I'm oscillating around it and if I rotate it by a little bit and what you're seeing with these sawtooth functions is it's actually capping out 1,024 and bouncing back to zero because that's where we're resetting the counter after each revolution so after each time I spin this around it's gonna drop back down but are the overall continuous one that's the cumulative count of a and the cumulative count of B if I spin in the opposite direction you'll see them begin to converge back down towards each other and now I just like wiggle back and forth lastly let's just see what happens when I remove everything and we just look at the direction so here every time that it's one we're spinning clockwise and then every time that we're spinning and you clockwise it's minus one and so forth
Info
Channel: Will Donaldson
Views: 103,379
Rating: undefined out of 5
Keywords: encoders, encoder, optical, Hall effect, Hall sensor, IR, infrared, magnetic, single phase, quadrature, square wave, program, software, diy, tutoiral, arduino
Id: dMBrR4gDi3s
Channel Id: undefined
Length: 20min 43sec (1243 seconds)
Published: Fri Mar 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.