Lab 8: Servo Motors
Electric motors convert electrical power into angular motion. Most motors spin continuously, like a cordless drill or the wheels on an electric vehicle, with a speed dependent on how much power is applied. Servo motors are a special type of motor that, instead of spinning continuously, rotate to a specific angular position based on an electrical signal. They are particularly useful in robotics, where they are used for range-limited movements like opening and closing a claw or steering a car.
Example Program
Assemble the following circuit and upload the program to your Arduino:
#include <Servo.h>
Servo myServo;
void setup()
{
myServo.attach(2);
}
void loop()
{
// Move the servo all the way to one end:
myServo.write(0);
delay(1000);
// Move the servo all the way to the other end:
myServo.write(180);
delay(1000);
}
You should see the servo moving back and forth over an arc of about 180 degrees, which happens to be its full range of motion. It can be helpful to attach one of the plastic accessories that came with the servo motor to better visualize its motion; just be sure to do this while it is not turned on and moving!
Learn More
-
If you want to interface your Arduino with a servo motor the first thing you will need to do is add
#include <Servo.h>
to the top of your program like in the example above. This tells the C compiler to include the Servo library, which contains a bunch of code that you can use to control a servo motor without having to know the nitty-gritty details of how they work. One of the biggest advantages of using an Arduino is the huge number of libraries available for interfacing with other components. -
Then, for each servo motor that you want to control, you must create a
Servo
object at the start of your program (likeServo myServo;
) and call itsattach()
method insetup()
with the pin that the servo is connected to (here, pin 2). The specifics of objects and methods are beyond the scope of this lab and AP CSP, but know that objects are a lot like variables in that you have to declare them ahead of time and each one needs a unique name, likemyServo
in the example above. -
Once we have declared and attached our
Servo
object we can call thewrite()
method to set its position to an angle between 0 and 180, the full range of the servo. The motion of a servo is fast but not instantaneous, so if you want to wait until a servo reaches its destination you will need to calldelay()
afterwrite()
, like in our example above. - When a servo motor is powered but sitting idle it will use about 10 mA of current. When the motor is moving from one position to another the current use dramatically increases to something closer to 250 mA. If a force is applied to resist the motor’s movement it will stall and consume even more current, as high as 350 mA. Why does this matter? Any device hooked up to a PC using a USB port (like your Arduino) is only allowed to use up to 500 mA of current. If it exceeds this limit it will be disconnected. Be mindful of this limit when powering servo motor circuits over USB.
Assignment
Build a “servo motor tester” using a servo motor and a potentiometer. As you turn the potentiometer knob over its full range of motion (0 to 1023) it should also move the servo motor over its full range of motion (0 to 180). While this is happening, your program should also print the servo motor position value in the serial monitor. This program will be useful if you decide to go on and do the “build an automaton” capstone project, as you can use it to directly control a servo motor and calibrate any servo motor position values you need to hard-code into the program.
Do you remember the map()
function from Lab 5? It will be useful here too.