Using Servo Motors with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop will be working with servo motors we'll learn how these versatile little devices work and how easy it is to control one using an Arduino will then look at the PCA 96 85 module which allows you to control 16 or more servo motors using the i2c bus we're moving into the right position so welcome to the workshop [Music] hello and welcome to the workshop today we're going to be making things move with servo motors now we've used other types of motors in our projects before we've looked at them in depth we took a very in-depth look at stepper motors not too long ago and we've used DC motors as the basis for a number of robot car designs that we've done we also looked at the l2 98n which is an H bridge module capable of controlling DC motors but although we've used servo motors in some of our projects we've never really taken an in-depth look at them and that's exactly what we're going to do today now I should mention that when I'm talking about servo motors I'm not talking about industrial type servo motors which can be very huge things that can cost hundreds or even thousands of dollars and that have digital control type of systems I'm also not talking about the small hobbyist digitally controlled motors which they're used in specialized applications such as model airplanes and model helicopters I'm speaking of your standard garden-variety analog servo motor the type that has three leads that we've seen in a number of our Arduino projects these things are great for robotics these things are great for IOT projects they're inexpensive they have a lot of torque if you need to move a sensor or a camera somewhere they're absolutely perfect for that anytime you need to position something precisely and that you don't need to go a full 360 degrees an analog servo motor might be exactly what you're looking for so let's start taking a look at servo motors now so let's take a look at servo motors a servo motor is a motor whose shaft position can be precisely controlled it uses an internal servo mechanism and some gearing in order to accomplish this the servo mechanism provides positional feedback so the motor always knows what position its shaft is currently in servos are used in industrial and hobbyist application will confine our discussion to hobbyist servo motors today hobbyists servomotors use DC motors internally a potentiometer is used to monitor the shaft position and act as a feedback sensor an internal controller board connected to this potentiometer adjust the position of the servo motor until the shaft is in exactly the required position the shaft position is controlled using pulse width modulation in a common hobbyist servo motor a pulse width of 1.5 milliseconds will cause the servo to rest at the 90 degree position reducing the pulse width to 1 millisecond will cause the servo to go to the zero degree position increasing it to 2 milliseconds will cause the servo to move to the 180 degree position most hobby servo motors travel a range of 180 degrees 270 degrees servo motors are also common you can also get continuous rotation servo motors these are servo motors that have had the potentiometer internally removed in a continuous rotation servo motor a pulse width of 1.5 milliseconds will cause a servo motor to come to a stop a pulse width of 1 millisecond will cause it to spin counterclockwise at full speed a 2 millisecond pulse width will cause the servo to spin clockwise at full speed hobbyists herbal motors have a 3-pin connector however the color codes used by different manufacturers tend to differ pin 1 of the servo is the ground connection pin 2 is the power connection and pin 3 is the control line the line that the pulse width modulation signal is sent to the servo motor on so now that we've seen how servos work I've got a few of them laid out here on the workbench for you now this is basically your standard servo motor this is an SG 90 it's got plastic gears it's a very very common servo motor you can do to get these in bags of five or ten for a very low price on eBay or Amazon this is a much larger servo motor as you can see NHS 422 has a lot more torque accepter operates the same way that this sg9 he does except it's capable of course a lot more power and it consumes more current and this looks a lot like the SD 90 but this is an FS 90 R and this is a continuous rotation servo motor so as you can see it's the same size and form factor as a standard SD 90 so you can fit it anywhere that you could mount an SG 90 but this is continuous rotation and on the back I don't know if you can see this there's a little adjustment you won't find any kind of adjustment on the bottom of this one here but you'll find an adjustment on the continuous rotation one this is in order to zero the motor what you do is you give it to 1.5 millisecond pulse that we use to set the motor at a zero point and then you adjust that until the motor stops and that way the motor is set up correctly now these are a number of the attachments that you get when you get a servo motor I've got a few of them out here these are sometimes called arms or horns and these attach to the front to the servo as you can see all of my servos have attachments on the front of them and this is how we mount things to our servo motors and so you'll find a lot of projects that are made for servo motors are already made to accept these mounts and horns that come with the servo motors so that's just to look at a few of the different servo motors that we will be working with today two of the key specifications when selecting a servo motor are speed and torque the speed of a servo motor is specified as the time required to move the shaft by sixty degrees for example 0.25 seconds per 60 degrees indicates that the shaft will move 60 degrees in quarter of a second torque is defined as the amount of force a servo motor can apply to a lever it is measured in ounce inches or kilograms centimeters and the two measurements can be converted I have a resource on the article on the website that will allow you to do that an example a servo motor rated at five kilograms centimeters means that one centimeter from the shaft position it can support a weight of five kilograms two centimeters from the shaft position it'll support half the weight two point five kilograms half a centimeter from the shaft position will allow it to support double the weight or ten kilograms there are a number of torque specifications associated with the servo motor the rated torque is the maximum torque at the servos rated speed the stall torque is the amount of torque that will cause the motor to stall peak torque is the maximum torque for very short time periods usually measured in milliseconds by knowing the speed and torque requirements of your application you can select the proper servo motor for the job so now we've seen how a servo works and we've seen some of the specifications that you would use when you're accusing a servo motor now another specification of course that you're going to want to specify is the size of the servo motor and as I showed you before I've got a couple of different sized servo motors here on the workbench I've also got something else on the workbench here that I wanted to show you this is a little testing device this allows you to test the servo motor it's a very handy thing to have in the field people who work with model airplanes and helicopters and that often carry one of these around just to make sure their servos are working and I want to show you how that works now this device is disconnected to my 5 volt power supply you can use any five or six volts supply to power it you simply plug the servo motor into it so I'm going to plug this and make sure you plug it in the right way around of course and it's got three different modes on one of them the boat I'm in right now as I turn the dial on the device the servo motor turns and that basically this shows that the servo is working the second mode and you press a little button over here there's a little push button switch under here this dist:0 m motor so this could be at the 90 degree mark right now and in the third mode it sweeps it back and forth a little bit so you can watch the servo work I'll tell you that with another servo over here this is this a standard servo motor I'll get in and zero it sweep it back and forth or just manually adjust the position now of course I also have a continuous rotation servo motor let's see what happens when we plug this into the tester you'll notice it's continually you're rotating and I can adjust the speed to a point in the middle where it will stop and then adjust the other way it'll go in the other direction and of course the second mode here should cause it to stop if it hadn't stopped I could adjust my little set screw here until it did and the third mode will just spin it back and forth in the two different directions so there you have it it's a handy little device they're only about two or three dollars so if you're going to work with servo motors I would advise you to pick one of these up so now that we've seen how servos work and how to specify them let's go on and actually do something practical with a servo motor okay so now it's time to do our first Arduino servo motor experiment now before we hook this up there's one thing I want to mention you may see a number of other diagrams including on the Arduino website itself or they've hooked the servo motor up to the 5 volt power supply from the Arduino Uno or Arduino mega you can do that if you really need to but I would advise against it I always power servo motors and any type of motor with its own independent power supply motors tend to put noise on the power supply lines and that can affect your digital circuitry they also can have sudden current demands and that can also cause a spike which can reset your Arduino or cause it to act erratically so I would highly recommend that you use a separate power supply of 5 to 6 volt power supply for your servo motor experiments I'm using the power supply in my workbench which is a 5 volt supply to do all of the experiments that I'm doing now if you absolutely must use the Arduino you don't have a 5 volt supply all the power supply stores in your neighborhood are closed right now or something well then what you could do is put a capacitor or something of a hundred microfarads or more across the power supply line from Arduino make sure to observe the polarity on that capacitor and that will act as a bit of a reservoir so that if your servo motor has a sudden load on it it won't put a sudden spike on to the power supply line which could affect the Arduino so now that I've said all that let's take a look at the hook up for our first Arduino servo motor experiment for our first experiment I'll be using an Arduino Uno although you can use a different model of Arduino if you wish you will also of course need a servo motor and you will need a power supply for that servo motor it is not a good idea to run a servo motor directly off of the Arduino zyv volt power supply a supply of 5 to 6 volts DC will suffice for most hobbyist servo motors we'll begin by connecting the ground of the Arduino to the negative side of the servo power supply will then connect the ground of the servo motor to the same connection next we will connect the positive side of the servo power supply to the servo power connector and finally the servo control line will be connected to pin 9 of our Arduino so the first sketch we're going to look at is a very simple one and it's included with your Arduino IDE is one of the example sketches so just go up the file and then go into examples and go down to servo and you will see two sketches knob and sweep where I could go to look at both of these sketches but let's look at sweep first so this is a sweep sketch now what the sweep sketch does is it just takes a servo motor and it sweeps to shaft from 0 to 180 degrees and then back again and it continues on and on and on so it's a very simple sketch but it shows you how to use a servo motor with an Arduino now to begin with will include Arduino servo library this is a library that's already been included with your IDE so you just need to have a statement here to include the library next we're going to create an object and in this case we're calling it my servo and as it notes here you can create multiple servo objects on one Arduino board keep in mind every servo needs to be associated with a pin that's capable of PWM the next thing we do is we define an integer call pause now this just takes the servo position and we'll define it as zero we're gonna sweep that from 0 to 180 degrees then we go into the setup and we simply attach this the object we've created my servo we attach it to the pin that the servo motor control line is attacked to which in our case is pin number nine so if you've used a different pwm pin you can just change the nine accordingly and then we go into the loop and the loop is basically a couple of four loops the first one going 4-0 to 180 and an increment of one it steps up from 0 to 180 and an increment of 1 the value of pause and for every step it writes that value over to the servo motor delays for 15 milliseconds and then continues the for loop so this will go from 0 to 180 now when you do a my servo right whatever value you put over here is the actual angle that the server will go to so if I put a 90 there the server will go to the 90 degree position a 45 will send it to 45 degrees so this will sweep it from zero to 180 and then we have another for loop which does exactly the opposite it starts off at 180 and drops down to zero and we'll just right pause from a value of 180 down to zero and delay by 50 milliseconds and then after that we come we complete the loop and start it all over and over and over again so this will sweep our servo shaft from 0 to 180 and then back again so now let's take a look at that in action all right I've got everything hooked up on a breadboard right now I'm using the bench power supply on my work bench to supply 5 volts for the servo motor and right now it's just disconnected otherwise the Arduino is actually running and everything is going so as soon as I connect up to power we can see the sweep sketch in action and there we go now if you reset the sketch it'll reset the servo there you can see the servo position reset to the end there and that's basically it so now let's take a look at another sketch that we can use to control our servo motor so now that we've seen the sweep sketch let's take a look at another sketch that's also part of the Arduino library examples this one's called the knob sketch now you're going to need an extra component for this sketch they're going to need a potentiometer of 10k or more so let's take a look at how we wire that up and then we'll move on to the knob sketch for our next experiment we will leave everything hooked up as we had in our first experiment we'll add a potentiometer to the circuit any value of 10k or higher will suffice we'll start by connecting one end of the potentiometer to the 5 volt output from the Arduino the other end of the pot will be connected to the ground of the Arduino and we will connect the wiper of the pot to the analog a zero input on our Arduino so let's take a look at the knob sketch now once again this is one of the demo sketches that was included with your Arduino IDE so just go into your example section on your sketches and go down to servo and load up knob now we start off by including the Arduino servo library so we'll include the servo library and once again we define an object called my servo that represents the servo motor then we'll define a couple of variables the first one is plot pin and that's the pin that the potentiometer is connected to so we've connected it to analog pin a 0 so we're defining that as a 0 and then an integer called value and that's the value that we're actually reading from that pot and we'll go into set and the only thing we do in setup is we attach the my servo object to the pin that we're using so again we're using pin number nine for our servo motor and then we go into the loop and it's very very simple we assign valve to the value of an analog read on the pot pin so this will give us a value from 0 to 1023 depending on the position of the potentiometer then we'll take Val and we'll change its value using a map command and we'll go the val is going to be our our variable and it's going to go from 0 to 1023 and we'll map that to a range of 0 to 180 so this will give us a different value for Val and that will represent the angle that we want to turn our servo motor shaft to and we'll do that with a my servo right and we'll district that value to it delay by 15 milliseconds and then go and repeat the loop over and over again so the result is the servo shaft should go to the position that we've got the pot in so let's take a look at it in action now now I've got the deluxe servo motor demo hooked up for you today in the workshop I've got not one but two servo motors hooked up to my Arduino and it was quite easy to hook two servo motors up I'm using the bench power supply so I've got plenty of current for both servos and the control line on each servo is going to pin 9 on the Arduino so the only difference between this and the schematic I showed you is that there's essentially a second servo motor in parallel now there's a difference between these two servo motors the one here with the single arm is a standard servo this is an SG 90 which is a very common servo motor you've probably used in projects before it's little plastic gear servo that's very inexpensive and it's sitting at the 90 degree position right now now this is a continuous rotation servo motor and it's at the stop position right now it's in the same body as an SG 90 so you can mount it anywhere that an SP 90 can go but again this is continued rotation so it can be made to rotate in one direction or the other by varying the pulse-width going to it now in order to monitor the pulse width I've got an oscilloscope hooked up today and you can see right now that a 1.5 millisecond pulse is being given to the servo this is the pot I'm using to control everything and with that size of pulse this servo motor here the standard one is sitting in the 90 degree position whereas a continuous rotation servo is in the stop position I'm going to move the pot and you can watch the scope as the the moment I'm going to shrink the size of the pulse and the standard servo is moving clockwise right now and this is spinning clockwise right now the continuous rotation one and the smaller and make the pulse this is right now at the end of its travel right now it's gone the full 180 degrees and this is spinning at full speed clockwise now I'm going to widen the pulse again and as I do you can see this servo moving back toward the 90 degree mark and this is slowing down we come to the middle and once again we're at the stop mode and then I'll go in the other direction and as you can see the continuous rotation servo is now spinning counterclockwise and this is moving more towards a zero Degree position and look at the oscilloscope you can see that the pulse width is increasing and has a pulse width increases toward its maximum this is down toward zero degrees and this has been in counter clockwise at full speed and so I've used a knob sketch demonstrate the operation of both a standard and a continuous rotation servo motor so from the examples we've looked at we can see it's pretty easy to control a servo motor using an Arduino and you can control a couple of servo motors with an Arduino you need to be using pins that are capable of pulse width modulation and an Arduino Uno has six of those pins other models of Arduino have different numbers of PWM enable pins but if you're going to control a lot of servo motors you're going to run into a problem first of all if you need to control more than six you're going to run out of pins if you're using an Arduino Uno secondly the servo library itself makes use of some of the Arduino internal timers and that may conflict with some of the other libraries that you're using for other sensors so that can be a problem when you're controlling servo motors even if you just have one or two if you need a lot of servo motors you can be taking a lot of your processors resources just controlling servo motors there is a better way of controlling servo motors with an Arduino or a Raspberry Pi or any microcontroller or microcomputer and that's to use an external board I'm going to be showing you a board called a PC a 96 85 now the PC a 96 85 is a 16 channel pulse width modulation Control Board so it can control any device that's controlled by PWM be it servo motors or LEDs which is another common use for it rather than the Arduino which disc controls a servo at 50 Hertz the PC a 96 85 is capable of a wide range of frequencies to control its PWM devices now this is an i2c bus device so it is connected to the Arduino or Raspberry Pi using the i2c bus it just needs a clock line and a data line as as a power and a ground it's got its own power supply so you can power your servos or LEDs from their own supply and you don't need the supply from the arduino now it's a really versatile board especially when you consider that you can cascade more than one of them on the same eye to see bus it's got a number of solder pads on it that allow you to set the i2c address so you can actually have up to 62 of these boards and each one of those boards can control 16 PWM devices now that's over 900 servo motors you could control with an Arduino if you have a need to control more than 900 servo motors please drop me a line and let me know what it is you're building because I'd love to see something that needs more than 900 servo motors so in any rate let's take a look now at the PC a 96 85 module the PC a 96 85 is a 16 channel pulse width modulation controller board capable of controlling both servo motors and LEDs the board uses the i2c bus and has a configurable address by configuring different addresses you can use up to 62 PC a 96 85 modules on one eye to see bus the module provides a separate power supply input for the servo motors or LEDs that you're controlling the PC a 96 85 is on a small circuit board with connections on both ends the first connection is the ground connection the next connection is the output and able connection when this line is brought high it will disable the outputs by default it is pulled low and does not need to be connected the third connection is the serial clock line of the i2c bus the next connection is the serial data line from the i2c bus after that the VCC connection this is the logic voltage for the circuit and generally comes from the or controller that's connected to it there is also a connection labeled v+ this is the power supply for the servo outputs there is another connection for the servo power supply on a 2 pin terminal block on the top of the module this is the preferred connection as it is reverse polarity protected whereas the v+ connections are not reverse polarity protected next to that connector are a series of solder pads these are used to configure the i2c address of the module on the bottom of the module there are 16 connections these are for servo motors or LEDs so now that you've seen how versatile this little pwm controller module is let's use it in an experiment I've hooked the PC a 96 85 up to an Arduino and I've attacked four servo motors to it in order to make it a bit of fun I've taken the four servo motors that are in my knee arm so what I'm going to do in this experiment is use these four slide pots to control the me arm so let's take a look at how we're going to hook that up and then how we're going to code for it for this experiment we'll be using an Arduino Uno and a PC a 96 85 pwm controller module will also be using four potentiometers any value of 10k or higher will suffice I'll be using four servo motors in this experiment and finally a power supply capable of powering all four servo motors anything from five to six bolts can do we'll begin by connecting the 5 volts from the Arduino to the VCC connection on the PC a 96 85 module the ground from the arduino is connected to the ground connection on the module the SCL connector on the arduino is connected to the SCL input on the module if you do not have an SC L connector on your Arduino use analog pin a5 instead the SDA connector on the Arduino is connected to the SDA connection on the module if you do not have an SD a connection on your Arduino use analog pin a4 next we'll plug in the forest' servo motors I plugged mine in as follows servo 1 was connected to PWM output number 0 servo 2 was connected to output number 4 servo 3 to output number 8 and servo 4 to output number 12 if you decide to use different connections you can modify the code accordingly now connect the five volts in the Arduino to one side of each potentiometer connect the ground from the Arduino to the other side of each pot now connect the wiper of potentiometer number one to the analog a zero input pot number two has its wiper connected to the analog a1 input number three is connected to a2 and potentiometer number four has its wiper connected to analog input a3 finally connect the power from the servo power supply to the connector on the PC a ninety six eighty five module so here's the sketch that I'm using in order to drive the PCA 96 85 module with my Arduino now this sketch makes use of a library from Adafruit the Adafruit PWM servo driver library now there are a number of other libraries that can also be used with the PCA 96 85 and I've got links to those libraries in the article that accompanies this video if you want to take a look at some alternate ways of doing this but at any rate I need the Adafruit PWM servo driver library for this sketch and you'll need to install that into your Arduino IDE and you can do it just with the library manager so go up the sketch go to include library and then go to manage libraries when your library manager opens search for Adafruit PWM and the Adafruit PWM servo driver library should be the first thing that you see now I've already got mine installed so you can see it's installed over here but in your case if you don't just click the more info link and you will get an install button that will allow you to install the library into your arduino ide once you've done that you can load the code and we'll start taking a look at it now the first thing we do is we include the wire library this is essential for i2c communications on the arduino next we will include the library that we just installed the Adafruit PWM servo driver library now we define a number of constants the first two are the minimum pulse width and the maximum pulse width this refers to the size of the pulses that we are going to be sending the servo motor in order to drive it to one end of its travel or the other end now I didn't come up with these numbers out of the blue this came with the spec sheets of the PWM servo driver library and you can modify these numbers if you find that your servo isn't going from one end of travel to the other or if you have a 270 degree servo instead of a 180 degree one the third constant is the frequency that will be sending the PWM signal out at 50 Hertz is the standard for servo motors so you probably won't need to modify that next we create an object called pwm using the Adafruit PWM servo driver library and then we'll define a number of inputs and outputs there's four integers defining the input the four potentiometers that we've got connected to the board to control the servo so that's a 0 through a 3 and then we'll define the outputs that we're using on the PC a 90 685 board itself so these are not Arduino pin notes these are actually on the PC a 90 685 board which has 16 output connectors and so I've got my motors connected to 0 4 8 and 12 if you're using different connectors just modify the sketch accordingly to represent the connectors that you're using so now we'll go into the setup and the setup we just initialize the PWM object that we created and so we use a begin and then we set the frequency of the oscillator that will be doing the PWM with on the board and we set this to free to 50 Hertz in our case now here is a function that actually is really the meat and potatoes of this sketch this is a function that I called move motor and it has two inputs to control in and control in is which of these controls a 0 through a 3 are we taking as an input and also the motor out and which of these motors are we using so basically give me the control that we're using the motor we're using and we'll set the motor to the value that the control happens to be now the way the function works it first of all defines a number of integers to use internally and then it takes an analogue read of the control that we've passed to it and assigns it to an integer called pot Val now this will be a number from 0 to 1023 so we're going to change it and create an integer called pulse-width and we'll use a map command to map to 0 to 20 10:23 value to a value between the minimum and the maximum pulse width so this will represent the size of the pulse width now when we're using the module one thing you need to know is the way that it generates a PWM signal it is a 12 bit PWM encoder and so what it does is it defy it divides the signal into 12 bits was just 4096 parts and it allows you to specify which of those parts you want to start your pulse at and which one you want to finish that so if you start at 0 and go to 2048 you'll create a perfect square wave it'll be half on half off it's for example and so we need to compute what the actual value we're going to give it the value we're going to tell it between 0 and 4096 that represents how wide we want our pulse to be and that's what this pulse width does over here it calculates it from the value that we were given before so we take pulse wide and we calculate the pulse width value which will be an integer and we use that to control the motor so we set PWM as the command we use and we take the motor out that just comes from the motor that we specified which one of the outputs on the module we start at position zero and we send it the pulse width the value that we've calculated over here and then in the loop we simply call the move motor command four times one for each motor so on motor a we've assigned control age of motor a motor B control B to motor B etc etc and we just go through that loop over and over read the values of the four different controls and assigned position of the four different sir Motors accordingly so now let's take a look at this in action controlling the me arm with the four potentiometers so now let's control our me arm each one of these pops is controlling one of the four motors and so as you can see this one moves the base motor and this one moves the left motor this does the right motor and this does the motor on the gripper over here and so now I can manipulate my me arm using the slide pots on the breadboard over here now here's my PC a ninety six eighty five module connected to my Arduino just with two wires with the i2c bus so as you can see this is a very effective method of controlling multiple servo motors with a single Arduino all right that about wraps it up for today I hope you've learned something about servo motors and that you've got some ideas for incorporating servos into your own projects and if you do have some projects you're working on with servo motors I would love to hear about them please leave a comment below the video and let us know what you're working on now I've got three things to take care of before I go today the first thing is if you haven't already please subscribe to the youtube channel just hit the little subscribe button below this video and that way you'll get to know every time a new video has been released he'll also make me very happy and of course a happy workshop is a great place to be so please subscribe if you haven't done so already the second thing you can do it will make me even happier is to subscribe to the workshop newsletter you'll find a link below the video to the drone bot workshop newsletter and this is my way of keeping it tact with you to let you know what I'm working on in the workshop and to also find out from you what it is you'd like me to create what a kind of videos and articles you would find the most useful and so please subscribe to that all it takes is an email address and the third thing I would like you to do is to visit the article that's accompanying this video you'll find a link to that as well below the video and you'll find all the code from everything that we've done today waiting for you on the article and easy to download zip file details about servomotors and about all kinds of other things on the drone bot workshop comm website so please visit that as well until next time take care of yourself I hope to see you again soon in the workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 649,334
Rating: 4.9431796 out of 5
Keywords: servo motor tutorial, arduino, arduino servo motor code, pca9685, pca9685 tutorial, Arduino Servo Library, adafruit pwm servo driver tutorial
Id: kUHmYKWwuWs
Channel Id: undefined
Length: 43min 6sec (2586 seconds)
Published: Sun May 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.