SparkFun Inventor's Kit for MicroView

Pages
Contributors: Joel_E_B
Favorited Favorite 7

Experiment 7: Servo Motors

Servos are ideal for embedded electronics applications because they do one thing very well that motors cannot – they can move to a position accurately. By varying the pulse width of the output voltage to a servo, you can move a servo to a specific position. For example, a pulse of 1.5 milliseconds will move the servo 90 degrees. In this circuit, you’ll learn how to use PWM (pulse width modulation) to control and rotate a servo. To learn more about PWM, visit our tutorial.

Parts Needed

You will need the following parts:

  • 1x Servo Motor
  • 3x Jumper Wire

Breadboard Setup

Connect 3x jumper wires to the female 3 pin header on the servo. This will make it easier to breadboard the servo.

Servo Jumpers

Hook up your circuit as pictured below:

Circuit07

MicroView Arduino Code

language:c
#include <MicroView.h>      // include MicroView library
#include <Servo.h>          // include Servo library

Servo servo;                // declare servo object

void setup()
{
    uView.begin();          // start MicroView 
    uView.clear(PAGE);      // clear page
    servo.attach(6);        // servo control pin at D6  
}

void loop()
{
    uView.setCursor(0,0);   // set cursor to 0,0
    uView.print("Mid  ");   // display Mid
    uView.display();
    servo.write(90);    // about 90 degree
    delay(2000);        // delay 2 seconds

    uView.setCursor(0,0);
    uView.print("Left ");
    uView.display();
    servo.write(20);    //  about 20 degree
    delay(2000);

    uView.setCursor(0,0);
    uView.print("Mid  ");
    uView.display();
    servo.write(90);    // about 90 degree
    delay(2000);

    uView.setCursor(0,0);
    uView.print("Right");
    uView.display();
    servo.write(160);   // about 160 degree
    delay(2000);
}

What You Should See

You should see your servo motor move to various locations at several speeds. If the motor doesn't move, check your connections and make sure you have verified and uploaded the code, or see the troubleshooting tips below.

Code to Note

#include is a special "preprocessor" command that inserts a library (or any other file) into your sketch. You can type this command yourself, or choose an installed library from the "sketch / import library" menu.

#include <Servo.h>

The servo library adds new commands that let you control a servo. To prepare the Arduino to control a servo, you must first create a Servo "object" for each servo (here we've named it "servo"), and then "attach" it to a digital pin (here we're using digital pin 6).

Servo servo;
servo.attach(6);

The servo in this kit doesn't spin all the way around, but they can be commanded to move to a specific position. We use the servo library's write() command to move a servo to a specified number of degrees(0 to 160). Remember that the servo requires time to move, so give it a short delay() if necessary.

servo.write(160);

Troubleshooting

Servo Not Twisting

Even with colored wires it is still shockingly easy to plug a servo in backward. This might be the case.

Still Not Working

A mistake we made a time or two was simply forgetting to connect the power (red and brown wires) to +5 volts and ground.

Fits and Starts

If the servo begins moving then twitches, and there's a flashing light on your MicroView, the power supply you are using is not quite up to the challenge. Try using a USB adapter that provides more current.

Still No Success?

A broken circuit is no fun, send us an e-mail and we will get back to you as soon as we can: TechSupport@sparkfun.com