How to Control a Servo with Arduino – An Introduction
Free Arduino Course
Controlling a Servo with Arduino
Want to get started with Arduino? Are you new to Arduino? This may help: How to Get Started with Arduino in 1 Day or Less
Figure 1: I know I said in the video I wasn’t going to post a schematic, but I just couldn’t help myself. Here it is for clarity.
Using Arduino to Control a Servo Video Transcription
Hey Everybody. Hope you’re doing awesome. It’s Brian Jenkins with circuit crush TV. And today I have another Arduino tutorial and we’re going to talk about using a Servo motor with Arduino. We can see we have this this little servo motor here connected directly to the Arduino. Now, that may or may not be a good idea depending on the motor and what you plan on doing with it. So, we’re going to talk more about that in a minute, but I just want to demonstrate real quick what this does. So I’m going to plug in the Arduino.
Give it a second…
And we see it sitting around, 180 and reversing and doing another 180. Okay, so that is what this program does.
I’m not going to go into a lot of detail about how Servo Motors work or anything like that. But I do want to say a few things on Motors in general.
Motors, Current, and Arduino
Motors can draw a fair amount of current and the more load of motor has the more current its going to draw and when a motor stalls — as in it too much load to can’t handle it, It’s trying and trying but just can’t do it — That’s when it’s going to draw the most current.
And we see this little thing here. This is the horn it’s — called a horn, spinning around and I took some current measurements earlier of this setup right here and right now with no load it draws anywhere from sixty to a hundred eighty five milliamps, depending on where it is in that, you know, the position of where it is in the program. Now if I take this and grab it with my fingers and stop it and actually stall the motor that shoots up to about 430 milliamps.
The Arduino Uno, the power supply or regulator is capable of about a maximum of 800 milliamps, no more and when it’s plugged into USB, of course, most USB ports are only good for 500.
But you also have to factor in the chip is going to draw some current — that microcontroller that powers the Arduino and you know, the little LEDs and stuff so you could get yourself in trouble real quick.
If, say, you have a couple of these hooked up directly to the board and for some reason they stopped — say your robot runs into something or something like that. They draw a lot of current, especially if it’s a bigger motor like this here. This Servo we could see is considerably larger than that one and is capable of drawing a lot more current than this little tiny one here.
And actually, even this little one can draw more than 430 milliamps and I’m going to show you right now how that is. I’m going to connect this little Servo motor directly to my bench top power supply. I’ve got these Nifty little cables here…
To our 5 volts here and our ground here and make sure they don’t touch we don’t want them touching.
And I’m going to plug the Arduino back in.
Then I’m going to kick on the power supply. We see this thing spinning round and round in circles this time. With no load it draws anywhere from a 110 to 140 milliamps and if I stop this thing with my fingers it shoots up to about 640.
So this could draw some significant amount of current, even a little motor like this. So just a word to the wise, a fair warning, when you’re connecting things like this directly to your Arduino be careful.
Make sure you have a good understanding of the board’s capabilities and how much power or current it’s going to draw.
Now, let’s jump on over and talk about some code.
Use Arduino for Servo Control: the Code
Okay guys, so here we are at the code and this is actually just an example program that comes with the Arduino IDE. I didn’t write this — full disclosure — this guy here did and we can find it by going to file examples….whoops…. and then down here to Servo and sweep.
So anyway, I just wanted to talk about this code real quick and go over some of it.
Of course, we have to include the servo a library and that’s what this line of code does.
Next we create a Servo object and this is a tutorial for beginners, So I’m not going to get into objects and classes, but just for now know that you need to use this to control the servo. It says here 12 Servo objects can be created on those boards. I believe with the mega 2560 that number is higher. Anyway, we have the position. We want to start off at zero.
And we have an integer here and then we jump right into setup and it’s just one line of code. And all we’re doing is attaching the object myServo which we created here to pin 9 which is the one of the pins on the Arduino with pulse width modulation capabilities. You can find information about that in the Arduino reference. Let’s take a quick look at the Arduino reference because it’s a pretty good indispensable tool.
Okay. So here we are at the Arduino reference and we’re looking at the servo library and this tells you a little bit about how server Motors work and all that and here we have the functions used with the servo Library. We see right here the .attach function — if we click on that it’ll give us some more information and like I said I attach the servo variable to a pin.
I’m gonna let you guys take a look at this on your own if you want to know more about the various functions within the servo Library.
Let’s jump back over to the IDE.
So here we are back at the IDE and we jump into the main Loop and we see the good old familiar for Loop. We see we want to sweep from 0 to 180. So the start position was Zero.
And then we increment by one in steps of 1 degree and this plus equals is a shorthand notation and is the same thing as writing — and I talked about this in another tutorial — but plus equals is same as writing…typing skills just are not the best, So bear with me here. It would be the same thing as writing position equals position plus 1.
Okay. So it’s just a quicker shorthand way when coding to write stuff and then here we write the position. We wait 15 milliseconds and then here we jump into this for Loop which reverses it and Spins it back the other way same basic concept here. We’re counting backwards instead of forwards and we see here it says minus equals instead of plus equals. You could do the this kind of shorthand notation with your addition, subtraction, multiplication operations.
Anyway, this was just an introduction to using servos with Arduino. There’s a lot of other things you could do with servos and Arduino, of course, and we will talk about some of those later. Meanwhile go out there and make something!