Understanding and using quadrature encoders - The Byte Sized Engineer | DigiKey

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when working on projects sometimes it's really useful to be able to track the rotation of an object whether that's the shaft of a spinning motor or a knob that you turn to select an item on a user interface my name is Zach and I'm the bite-sized engineer in this video I'm going to be talking about encoders and how to read them with a microcontroller and I'll even go over a couple of mistakes that I've made that you should avoid encoders come in two different varieties there's the electromechanical kind that actually have physical switches that open enclos inside them then there's the contactless kind that use magnetic fields or light first I'm going to talk about electromechanical encoders you may have used this in a project before it's called a rotary encoder this component has a little shaft that rotates and there are little detents in there that as you spin it it kind of falls into place and you can feel that physical feedback that you're turning the knob rotary encoders are different from from potentiometers in a number of ways first there are no end limits you can rotate this as many times as you want without ever running into an end stop the other big difference is that potentiometers will vary the resistance on their output whereas a rotary encoder outputs Square pulses that can be read by a microcontroller or other digital logic components by keeping track of the number of pulses coming out of the rotary encoder you can determine how far you've turned the shaft to show you what I'm talking about I drew a little diagram here I've got a little shaft that is spinning clockwise here and I've placed a limit switch near that spinning shaft if I add a little nub sticking out of that spinning shaft as it rotates it will come into contact and close that limit switch we can read that pulse with a microcontroller and determine that the shaft is in this exact position the other cool thing we can do with encoders is determine how fast a shaft is spinning if we measure the time between pulses we can calculate how fast that shaft is spinning I'll demonstrate this concept a little bit more clearly using this stepper motor the stepper motor is slow slowly spinning around but it's a little hard to see so I'm going to attach my little disc that I 3D printed to help visualize what I'm talking about if you look closely you can see that this disc is not perfectly Circle it has a bit of a nub sticking out where I have the black Sharpie now I'll attach a little limit switch that will come into contact with that nub as it spins around I've connected my multimeter to the limit switch and I'm going to put it in continuity mode and then I'll turn on the motor as the shaft spins around and comes in contact with the limit switch you can hear the multimeter beep this is working great but what if I want more resolution right now I only have one nub that comes in contact with that limit switch so it gives me one pulse per revolution of the shaft if I wanted to get more resolution out of this crude encoder I could add more Nubs on here that would increase the number of pulses per Evolution each time one of those Nubs comes in contact with the limit switch it will give me a pulse I can replace this disc with a new disc that has two Nubs which will give me two pulses per Revolution I've added a little bit more to my diagram I've connected the normally closed terminal to ground and the normally open Terminal up to 5 volts using a pull-up resistor the common terminal is connected to a gpio pin on my microcontroller as the shaft spins these high points come in contact with the switch and I can read those pulses using my microcontroller the resolution of the encoder is measured in how many pulses per revolution in this case I have four Nubs so that would give me four pulses per revolution of the shaft I'm representing the opening and closing of the switch using a square wave now obviously this is a very crude representation of an encoder real encoders have much higher resolution and you'll get hundreds of pulses per Revolution let's say my encoder has 200 pulses per Revolution with this design I can keep track of how much the shaft has turned and I can also calculate how fast it's turning but the big thing that I'm missing here is being able to tell whether the shaft is spinning clockwise or counterclockwise as those high points are coming in contact with the switch there's no way to tell whether the shaft is spinning clockwise or counterclockwise for that we'll need a second switch I tie the normally closed terminal to ground and I tie the normally open up to 5 volts using a pull-up resistor the common terminal is then fed to a second GPI open pin on my microcontroller let's call the first switch a and that's going to be represented by the color blue and the second switch is going to be B and that's going to be represented by The Color Purple to start out neither switch will be closed that means my GPI open pins A and B will both be reading a logic low if I were to draw that on my Square wave diagram I would start here at the beginning and I also have switch B at a low State as the shaft rotates switch a will close which gives me a logic high at this point switch B is still open so it is still a logic low the shaft continues to rotate closing switch B which makes the microcontroller read a logic High signal as the shaft continues to rotate switch a will eventually open which will make the microcontroller read a logic low that would be right here in our Square wave diagram notice that switch B is still closed so it's reading a logic High while switch a is open reading a logic low finally the shaft continues to rotate causing switch B to open which results in a microcontroller reading of a logic low this pattern will continue as the shaft rotates as you study these square waves you'll notice something really important they are not in sync the rising edge of each signal does not align they're actually offset by 90° another way to say that is that signal B is trailing signal a it is 90° out of phase I can also draw this as a truth table at the beginning of my pattern both signals are low as I move to the right signal a becomes High while signal B is still low then as I move further to the right both signals are high and then as I continue signal a goes low while signal B is still high and then finally it repeats the pattern where both signals are low this type of pattern has a name it's called quadrature encoding the reason it's called a quadrature output is because there are four different possibilities that the outputs could be in low low high low high high and low high by reading both signal A and B not only can we read the position and speed of the shaft we can also determine whether it is spinning clockwise or counterclockwise I should point out that yes it's possible to build an encoder like this using electromechanical switches but this is not a good idea and the main reason is that electromechanical switches like this have a lot of bouncing bouncing is when the contact inside of an electromechanical switch makes and breaks contact many times before it settles into a steady state each of those quick little bounces is going to be read as an encoder pulse and if you're trying to keep track of your position that could get really difficult most encoders don't use electromechanical switches they use contactless sensing such as hall effect sensors or Optical sensors here's an example of a DC motor that has an encoder on the back instead of using electromechanical switches like this it has two hall effect sensors and the disc that spins around is actually a magnet with North and South Poles instead of high spots coming into contact with electromechanical switches the hall effect sensor reads the change in polarity of the magnetic field let me connect this motor up to a power supply and an oscilloscope to show you the signals as you can see the motor is spinning clockwise if we look at the waveform we can see that the rising edge of a is leading the rising edge of B now if I swap the polarity of the power supply the motor is going to spin counterclockwise and if we look at the waveform we can see that signal a is now trailing signal B this tells me that the motor is spinning counterclockwise in the microcontroller code you set up an interrupt service routine within the interrupt service routine you compare the state of B to the state of a if the two states are different then the shaft is spinning clockwise if the two states are the same then the shaft is spinning counterclockwise I'm going to head over to the computer and write a quick little uino sketch to show you exactly what I mean so the first thing I'm going to do in my Arduino sketch is to Define a couple of pins I want to connect the encoder a signal to an interrupt pin so I'm going to pound Define and I'm going to put encoder a and then I'm going to assign it the pin number two because on the Arduino Nano that I'm using pin two and pin three are the only pins that are capable of interrupt so I'm going to use pin two as an interrupt and then for the other encoder signal I'm going to use pin 4 then I'm going to go into my setup function and I'm going to type in serial begin and I'm going to set my B rate so that I can use the serial output as a debug interface then I'm going to set up my interrupt service routine then I'm going to use the digital pin to interrupt function and pass in encoder a as the argument so that it converts that PIN number to the right interrupt number for the function I'm going to type in a function name that I haven't created yet but I'm going to call it update motor position and then the mode is going to be changed and that means that anytime there is a change in state that means a rising Edge or a falling Edge it's going to trigger this interrupt so I'm going to create a function and it returns nothing so it's a void and I'm going to call it update motor position and within that function I'm going to use an if statement to compare the state of encoder B with encoder a so I'm going to digital read the state of encoder B and compare it to the digital read of encoder a and if they are different then the motor Position will increase otherwise the motor position decreases so I'm going to go back up to the top and make a global variable called motor position I know that this variable is going to keep track of some numbers that might get very large so I'm going to use the type long and because this variable is going to be changed within an interrupt service routine I need to make the variable volatile and then from there it's pretty simple we go into our main Loop and I'm just going to do a Serial print and I'm going to print out the motor position so that's just going to be always updating and showing me what my motor position is okay so now I can upload this and I can see that my serial monitor is printing out the position of zero I don't have the motor plugged in it's not spinning and to test this out I can actually just grab the motor by hand right now and spin this and watch that number change so that's the motor position changing and if I go the other way I can see that it comes back down so this is a very simple way to read a motor encoder that uses quadrature output earlier this year on the bite-size engineering Channel I made a safe cracking robot and I used an encoder on a stepper motor that was spinning the dial and I needed to use all of this stuff that I showed you here to get it to work properly I made a really big mistake that cost me several weeks of headaches and troubleshooting and it's actually right here in this code do you remember when I did the attach interrupt function and I set the mode to change well when I originally rode it I was just keeping track of the rising edges this worked well as long as the motor was going in the same direction but as soon as I changed the direction of the motor I actually lost a couple of Pulses from the encoder and I didn't notice this for weeks and weeks and and weeks and it was super frustrating so I'm giving you this cautionary tale because I spent weeks banging my head against the wall trying to troubleshoot I thought it was all sorts of other things and ended up being just a really simple fix I was using the wrong mode for the attach interrupt function I hope this video helps you understand encoders a little bit better and helps you avoid some of the mistakes that I've made when using these devices [Music]
Info
Channel: DigiKey
Views: 5,262
Rating: undefined out of 5
Keywords: Arduino, Electronics, Encoders, Engineering, Motor Encoder, Quadrature, Rotary Encoders
Id: o1Oev-B7ScM
Channel Id: undefined
Length: 11min 36sec (696 seconds)
Published: Tue Feb 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.