Using Rotary Encoders with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop will be working with rotary encoders will learn how rotary encoders work and how to connect them to an Arduino will also control a servo motor and read the speed of a DC gear motor we're going in circles today so welcome to the workshop [Music] hey welcome to the workshop today we're going to be working with rotary encoders now a rotary encoder is a device that can measure rotation usually it measures rotation on a shaft and it translates this rotation into a series of pulses these pulses can tell the controller attached to the encoder how much the shaft is moved and in what direction it has moved now we're going to examine two different applications for rotary encoders today the first application is as a control now you probably have some equipment around your home that uses a rotary encoder for example a lot of audio equipment uses this as a volume control and you can tell it's using a rotary encoder instead of a potentiometer because the control is allowed to move more than 360 degrees you can continually rotate it another thing is that rotary encoders in this application usually move in discrete little steps so it's not a linear motion you can feel the steps as you're rotating the control it's also used in test equipment both my oscilloscope and my signal generator for example use rotary encoders on their controls another application for rotary encoders is to look at the motion on a motor and the motors that I'm using on my db1 robot have rotary encoders on the back and so that's another way to use an encoder in this particular case the encoder doesn't go in discrete steps it's a free-flowing device but it does the same thing it gives out pulses to indicate both the amount of rotation and the direction so what we will do is look at how rotary encoders work and then do a couple of experiments with both types of rotary encoders so let's examine the two types of rotary encoders that we'll be working with today rotary encoders measure angular movement they can measure both the degree of movement and its direction they can be used as controls and in this use they are much more versatile than potentiometers they can also be used as rotation sensors for motors and other mechanical devices there are several different methods of encoding the output of rotary encoders some encoders give an incremental output while other ones can give an absolute output rotary encoders can use magnetic or optical sensors some devices even use lasers a very common type of rotary encoder and the type that we'll be working with today is the incremental encoder this is sometimes called the quadrature or relative encoder this type of encoder uses two pulsed output signals to indicate both direction and degree of rotation now here is an example of a rotary encoder in action in this example you will notice that the top set of pulses precede the bottom set of pulses the pulses themselves indicate the degree of rotation the fact that the top set perceives the bottom set indicates that the rotation is in a clockwise direction now here is the same example only this time the rotation is counterclockwise notice that now the bottom set of pulses precede the top set of pulses here's the rotary encoder control that we are going to be using in our experiments the pin notes are as follows the bottom pin is the ground the next pin is the VCC our positive power supply the third pin is attached to a momentary contact push-button switch that can be activated by pushing in the shaft of the rotary encoder after that we have the two outputs output a which is labeled DT and output B which is labeled CLK now here's the rotary encoder on the back of the gear motor that we will also be looking at today the connections to the encoder are as follows the VCC or positive supply voltage the ground output a and output B now they just want to show you the two different rotary encoders we're going to be working with the day the first one is a simple control and this one is widely available at you got it in one of those thirty seven and one sensor kits but you can also get these on Amazon and eBay and also if you have a local electronic store they're bound to have these as well it's a very simple device as you can see it's got some pin outs on the bottom the encoder up over here and it looks a lot like a potentiometer except you can turn it around 360 degrees if you want to and on the back of the board they just have a few pull-up resistor so really a very basic device now the back of this motor also has a rotary encoder built into it and it's got a connector on the back here now this is a little bit difficult to see I know because it is a black connector with a black or writing on it but basically it's VCC ground and a channel a and a channel B output over here and this will connect to your microcontroller so you can read the encoder so these are the two devices we're going to get working with so let's start by hooking this one up to an Arduino so now we've seen a little bit about how rotary encoders work and we've looked at a couple of rotary encoders both a motor one and a control one so let's start off with our control one I'm going to show you how to hook this up to an Arduino and then we will write a very simple sketch that can measure the rotation and what direction it is rotating in so let's go and take a look at how we hook that up right now so here's how we're going to hook up our rotary encoder test you will need an Arduino Uno a rotary encoder module a couple of LEDs I'm showing a green one and a red one but you can use any two colors that you like and a couple of dropping resistors for those LEDs I'm showing 220 ohms here but any value from 150 to 470 ohms would work well we'll begin by connecting the ground from the Arduino to the ground pin on the rotary encoder we'll connect the Arduino saw volt output to the v+ pin on the encoder digital pin four on the Arduino will be connected to the CLK pin on the encoder and digital i/o pin five on your Arduino connects to the DT pin on the module now connect pin eight of the Arduino to one side of a drop-in resistor connect the other side to the anode of one of the LEDs this will be the LED that indicates a clockwise rotation connect pin nine of the Arduino to one side of a drop-in resistor and the other side of that resistor to the anode of the other LED this will indicate counter clockwise motion finally the ground of the Arduino is connected to both of the cathodes on the LEDs and this completes the wiring now let's take a look at the sketch we'll use to test our rotary encoder now here is the sketch that we're going to use to demonstrate the operation of our rotary encoder this sketch does not require any additional libraries we'll start off by defining the input pins that we are using from the CLK and the DT pin on the encoder then we'll define the LED pins for the clockwise and counterclockwise LED incidentally there's nothing special about any of these pin numbers and if you want the wire your encoder or LEDs differently you may do so and just change the numbers in the sketch to match then we're going to define a counter the counter is the value that the encoder is going to be changing so it will either go up or down will initialize ours at 0 will also define two integers the one that holds the current state of the CLK pin and one that holds its previous state and by doing this we're going to be able to determine if the encoder is actually moved we'll also define a string called ink gear and that basically is just the direction of the encoder and we're going to populate that with clockwise or counterclockwise accordingly now the setup is very simple we'll set the encoder pins up as inputs and the LED pins up as outputs and we set up our serial monitor before we leave the set up we also need to populate the previous state CLK value and will populate it with the current state of the CLK pin then we go into the loop now in the loop we start off by reading the current state of the cilk pin and assigning it to the current state CLK variable then we go and check to see if the encoder is moved if the encoder is moved the current state and the previous state will be different from each other and so if that happens we know the encoder is moved and we need to determine what direction it has moved in now the direction can be determined by looking at the other pin to see if it is at the same state or the different state to the current state of the CLK pin if it is at a different state then we are turning counterclockwise so we're going to take our counter and decrement it which is what this statement does the anchor variable will be populated with CCW so we know we're going counterclockwise will turn the clockwise LED off and the counterclockwise LED on otherwise we're going clockwise and so we do the opposite we increment the counter and Qadir now says CW for clockwise we will turn the clockwise LED on and the counter clockwise LED off then we'll go to the serial monitor and we'll print the direction which will be either CW or CCW and we'll print the value of the counter and then we just update the previous state variable with the current state and we start the loop over again so it's a pretty simple sketch let's take a look at it and see how it works now now here's our rotary encoder demonstration it's a fairly simple circuit the green LED is going to indicate when I'm turning the encoder clockwise and the red one counter clockwise and of course I'll also have it displayed on my serial monitors let's just go ahead and turn my encoder I'm going to turn a clockwise and as you can see the green LED is lit and the values are going up now if I turn it counterclockwise my red LED is lit and the values are going down and after that can bring them below zero and they'll become negative numbers and again if I go up the green LED and the numbers are incrementing so a very basic but effective demonstration of the use of a rotary encoder so now we've seen how we can hook a rotary encoder up to an Arduino and how we can use the Arduino to measure both the amount of rotation and the direction of rotation for our next experiment I thought we'd do something that's a little bit more fun than just displaying the results on a serial monitor now one great thing about rotary encoders is that they are precision devices unlike using a pot for a control you can really narrow down the exact value that you want with a rotary encoder so what I've done with the next experiment is I've hooked one up to a servo motor and with the rotary encoder we can move the servo in one degree increments so we can actually dial up the position very very precisely of course this depends upon the accuracy of the servo motor itself and a lot of these hobbyist servo motors like the SG 90 that I'm going to use are not necessarily the most precision devices in the world but this is still a much more accurate method of positioning the servo shaft than by using a potentiometer so let's go and take a look at the hookup for that and then I'll fill you how I modified the original sketch in order to include a servo now for this experiment I'm going to start off with the same connection from the encoder to the Arduino I've removed the two LEDs because they aren't used in this application I'm going to add a servo motor and I'm also going to add a power supply for the servo motor now you could power the servo from the Arduino Skybolt supply but I really don't recommend it I prefer to have a separate power supply for the servo motor so I'm using my 5 volt bench supply but you could also use a sick battery as well start by connecting the positive side of the power supply to the positive terminal of the servo motor it's red wire next connect the negative side or the ground side of your power supply to the servo ground which on many servos is a brown or a black wire you also need to connect the servo ground to the ground on the Arduino this is an important connection so don't forget it finally connect pin nine of the Arduino to the servos control pin and this completes the wiring now let's take a look at the sketch that we're going to use to control our servo motor with our rotary encoder now here's the sketch that we're going to use to control our servo motor with the rotary encoder you will notice that this sketch is very very similar to the previous demonstration sketch for the rotary encoder it really just adds the servo functions in so we'll start by including the servo library this is a library that was included with your Arduino IDE so you won't need to install anything extra then as we did in the last sketch will define the inputs from the encoder the CLK and the dt input will create a servo object called my servo a game will set some integers up to represent the counter the current state of the CLK line in its previous state then we go into the setup we set up the connections to the rotary encoder as being inputs and set up our serial monitor and now we attack our servo control lead to pin nine on the arduino if you elect to use a different pin you'll need to change that of course just make certain that you use a pin that's capable of pulse width modulation and will initially assign the state of previous state CLK to the current state of the CLK line now and the loop will start off again by taking the current state of the CLK and this is again the same as we saw before we'll check to see if they are different and if solo pulses occurred and then we will see if the dt line is the same or different to determine whether we're going clockwise or counterclockwise now one thing we have done differently is that we've taken the counter here so if we're going counterclockwise we decrement the counter but we look and see if the counter has gone below zero if it has gone below zero we'll just set it to zero because we don't want a value for our servo of less than zero same thing for the clockwise rotation we'll we'll increment the counter and then we'll see if it's gone above 180 and if it has will set it to 180 because that is the maximum for our servo motor and then we will go and move the servo motor to the value of the counter and we will write those values onto the serial monitor and then once again update the previous state CLK to equal the current state C okay and start the loop over so it's a very simple addition to the original demo sketch so now let's hook it up to a servo and take a look at it in action now here's my hookup for my servo motor controller my rotary encoder over here and of course my servo I'm powering the servo off my 5 volt bent power supply and naturally my Arduino is over here so let's look at the serial monitor and move the encoder and as you can see I can position the servo moves in very small increments as you would expect and that position that it's reading on the serial monitor could actually correspond to the number of degrees the servo motor is moved let's bring it all the way up and as you can see once I get to 180 no matter how much I move it it stays at 180 and we'll move it down and as we get to zero it stays at zero and so this will allow me to position the servomotor any way I want for example if I want to place it at let's say a ninety degree angle there we go it's exactly at ninety degrees so this will allow you to use the rotary encoder to position the servo motor very precisely so this actually would be a very useful circuit to have so now that we've seen this let's continue our look at rotary encoders this time we'll look at the rotary encoders that are included on the back of a DC gear motor so now that we've looked at the rotary encoder as a control let's move on and look at one that measures rotation on a motor now as I showed you the motor that I'm using in my db1 robot has a rotary encoder built-in and it's on the back of the motor and there's a connector on the back or you can connect to the pins on a rotary encoder now this connector has four pins on it two of them are the VCC and the ground and obviously that's for the power supply the other two are output a and output B and these are the two outputs from the rotary encoder what I want to do before I hook it up to an Arduino though is hook it up to an oscilloscope so you can take a look at the two different output pulses and their relationship to each other so let's go and do that first now here's a little test setup that it is dragged up in order to measure the output of the rotary encoder that is on the bottom of the DC gear motor that I'm using on my robot frame and as you can see I've got an oscilloscope hooked up over here the two traces will represent the outputs the two outputs that come from the rotary encoder and I'm powering the rotary encoder with my five volt bench supply I'm going to use the 12 volt supply to rotate the motor now I'm not going to adjust the motor speed or anything I'm just going to give it the full 12 volts and we can watch the scope and take a look at the waveforms that we get so let's connect volts up to our motor and you can see the waveform now as you can see the waveforms are out of phase with each other in fact they're about 180 degrees out of phase with each other you'll also see a little bit of noise I'm picking up on this and that's something of course that I would try to eliminate in a actual final design with a pull-up resistor or something like that but as the motor rotates the two of these waveforms are delivered by the rotary encoder and the phase difference between the two of the waveforms can indicate the direction and of course the pulses are indicating the speed and so there you go a fairly simple demonstration of how the output of the rotary encoder on a DC gear motor looks [Applause] [Music] for this experiment you're going to require an arduino uno a gear motor with a rotary encoder a sight Tron MD 10 C PWM motor driver a power supply for the motor I'm using a 12 volt supply because that's what my motor is rated at and a potentiometer any value from 5 K up will work we'll begin by connecting the 5 volt output of the Arduino to the VCC on the rotary encoder on the back of the motor we'll connect the ground from the Arduino to the ground connection on the encoder and we'll connect pin 3 of the Arduino to the channel a output on the rotary encoder in actual fact we're only using one of the outputs and you could just as easily use channel B next we'll connect pin 10 of the Arduino to the PWM input on the motor driver we'll connect pin 12 of the Arduino to the dir or direction-- pin on the motor driver in actual fact this connection is optional because we are not controlling motor direction in the sketch we're going to be using will also connect the Arduino z' ground to the ground input on the motor driver now the potentiometer will connect one side of the pot to the 5 volt outlet from the Arduino will connect the other side to the Arduino is ground the wiper of the pot will be connected to analog pin a 0 next we'll connect the motor to the 2 outputs on the MD 10 C driver and finally the motor power supply will connect the ground pin to the negative side of the 12-volt DC supply and the positive pin to the positive side of the supply and this completes the wiring now let's take a look at the sketch we'll be using in order to control our motor and read back at speed here's the sketch that we're going to use to read the speed of our gear motor with the rotary encoder now we will start off by defining a constant called ink count Rev and this is the number of pulses per rotation that you're in coder gives out now every motor and encoder combination is going to have a different value here and you'll need to get this value from the specification sheets of your motor now my motor says it is three hundred and seventy four pulses for every rotation and that's why I put the value 374 here so of course unless you're using the same motor encoder that I'm using you will need to change this after that we define the pin that we are connecting the encoder to now it's an interrupt pin and we're using pin number three which internally is interrupts number one we'll also define the pins that we're connecting the motor driver to now the md-10 C motor driver has two inputs a pwm pin and a derp pin which controls the direction in actual fact this sketch is not changing the derp pin but we have it defined over here just so that you can add to it later and add Direction control if you wish to do that then the pin that we've connected our potentiometer wiper to which is analog pin a zero then after that we define a variable for encoder value this is the number of pulses that we are picking up from the encoder now we define a time interval of 1000 this is going to be in milliseconds and it represents a one-second interval because we are going to be measuring pulses on one second intervals we also define two other variables we will need for that calculation previous Millis and current Millis which again will represent milliseconds then we define a variable to represent the RPM which is what we are trying to measure with this sketch and also a variable for the pwm signal we are sending to the motor that will define the motor speed now then we'll go into our setup routine the setup we'll start by setting up our serial monitor we'll set the Inc in pin the one we've got connected to the encoder as an input and we'll use the internal pull-up resistor to make certain we don't get any noise on the line we'll set the PWM and the dur pins up outputs then we will attack our interrupts now we're going to use the ink in pin because that's the one we're connected to and we're going to call the update encoder interrupt handler which you will see in a moment and we'll do this on the rising side of the pulse then we also have to set the initial value that we're using for our counter that counts time so we set previous Millis to the value of the Millis function now if you're not familiar with the Millis function it calculates the number of milliseconds that have elapsed since the Arduino was powered up or a last reset okay now before we go in the loop let's just take a quick look at the update encoder interrupt handler it's very simpler with the previous ones we saw all that it does is it increments the encoder value so every time a pulse is detected an interrupt will be triggered and the encoder value will be incremented by one okay back to the loop now in the loop we're going to control the speed of the motor we need to get the motor PWM value so we'll read the speed control using an analog read and then we take a map because that value is going to be a value from 0 to 1023 because of the Arduino is 10 bit a 2 D converter we need to map it to a value of 0 to 255 then after that we will write that to the actual motor controllers so that we can set the speed of the motor now we want to update the RPM we're only going to do this every second and this is where our counter goes into play we'll set the current Millis to the value of Millis right now and we'll see if it is greater than a thousand which is our interval time and if it is then a second has elapsed we'll reset the previous melas to the current Millis value and then we'll go and calculate the RPM now the RPM calculation is actually pretty simple the encoder value is the number of pulses we've received in a second since RPM is revolutions per minute we need to multiply that by 60 and then we divide by the egg count Rev which is a number of pulses we get per revolution our motor and that ends up being the RPM of the motor then we just go over to the serial monitor and display all of this information will display the PWM value and then we use this command to place a tab character in there so that all of our columns are nicely lined up we'll display the number of pulses we've got from the encoder another tab and then finally the speed in rpm then after that we'll reset the encoder value and go back up to the top of the loop and do it over and over again so it's actually a fairly simple sketch let's load it and take a look at it in action now so here's my test setup to test out my motor speed rpm readers get now I've got the Arduino over here of course the potentiometer and the sight Tron motor driver up over here you will see that I have the motor that is mounted on the chassis from my robot I'm only going to be using this one this is the connection over here to the motor encoder itself which is the black part at the bottom of the motor and of course the wheel that is going to be spinning and so I'm going to move the potentiometer [Music] and as you can see as I go higher we're getting readings on our serial monitor and of course the motor is moving and we would a bit higher coming up towards full speed and here we are at full speed I'm getting about 94 rpm which is pretty close to what my motor is rate of that and I'll move it down [Applause] and it will read the speed now I'm going to put it back up over here and do something that I probably shouldn't do and that's restricting my motor and you can see on the display that the RPM the back is going down so it actually ends reading the speed of the motor shaft so that seems to work pretty well alright so that about wraps it up for today's video I hope that you enjoyed it and that's inspired you to start using rotary encoders in some of your own designs they make for a very professional design and in a lot of cases they are a much better choice than using a potentiometer as a control now for those of you who have been following the series on building the dp1 robot well now you know how we can read the pulses from the motor that we're using on the robot and I'll be using that to build a full-fledged motor controller for it so stay tuned because you'll be seeing an episode about that very very soon now if you need some more information about rotary encoders or if you want to get the sketches that I use today check out the article that accompanies this video you will find the link below the video to the article in the drone bot workshop website now while you're on the website please consider subscribing to the newsletter the newsletter isn't a sales letter it's my way of keeping in touch with you it's also a way for me to find out what videos you want me to make and on that subject I'm going to be adding a new feature very very soon that will allow newsletter subscribers to start suggesting videos that I make so if you want to get on that please sign up for the newsletter and while you're in a subscribing load if you haven't done so already please subscribe to the YouTube channel you can just hit the subscribe button below this video and that way you will know when I make new videos and you will have my gratitude eternally for doing that so until we meet next time please take care of yourselves and I hope to see you again very soon here in the workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 616,757
Rating: undefined out of 5
Keywords: incremental encoder, rotary encoder, arduino, motor encoder
Id: V1txmR8GXzE
Channel Id: undefined
Length: 32min 9sec (1929 seconds)
Published: Sat Mar 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.