ZachLabs Arduino

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

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.