Lab: Servo Motor Control with an Arduino

Last edited 28 August 2014 by Tom Igoe

Introduction

In this lab, you’ll control a servomotor’s position using the value returned from an analog sensor. Servos are the easiest way to start making motion with a microcontroller. Even though they don’t turn 360 degrees but only 180, you can use them to create all sorts of periodic or reciprocating motions. Check out some of the mechanisms at Rob Ive’s site for ideas on how to make levers, cams, and other simple machines for making motion.

What You’ll Need to Know

To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below:

Things You’ll Need

For this lab you will need the following parts:
Solderless breadboard Hook-up wire Arduino
Solderless Breadboard 22-AWG hook-up wire Arduino microcontroller module
Resistors Flex sensor Servo Motor
10Kohm resistors Flex sensors (or a different form of variable resistor) RC Servomotor

Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:

LabTemplate_bb
Made with Fritzing

Connect an analog input sensor and a servo

Pick any analog input and connect it to Analog pin 0 as you did in the Analog Input Lab covered previously. Then connect an RC servomotor to digital pin 2. The yellow wire of the servo goes to the pin, and the red and black wires go to +5V and ground, respectively.

Safety Warning! Not all servos have the same wiring colors. For example, the Hextronik servos that come with Adafruit’s ARDX kit use red for +5V,brown for ground, and mustrard yellow for control.

LabServo_bb

When you attach the servo, you’ll need a row of three male headers to attach it to a breadboard. You may find that the pins don’t stay in the servo’s connector holes. Put the pins in the servo’s connector, then push them down on a table gently. They will slide up inside their plastic sheaths, and fit better in your servo’s connector.

Header pins don't always stay in a servo connector Put them in and push gently on a firm surface Voila – your headers fit better!

Program the Microcontroller

First, find out the range of your sensor by using analogRead() to read the sensor and printing out the results.

void setup() {
  Serial.begin(9600);       // initialize serial communications
} 

void loop()
{
  int analogValue = analogRead(A0); // read the analog input
  Serial.println(analogValue);      // print it
}

Now, map the result of the analog reading to a range from 0 to 179, which is the range of the sensor in degrees. Store the mapped value in a local variable called servoAngle.

void setup() {
  Serial.begin(9600);       // initialize serial communications
} 

void loop()
{
  int analogValue = analogRead(A0); // read the analog input
  Serial.println(analogValue);      // print it

  // if your sensor's range is less than 0 to 1023, you'll need to
  // modify the map() function to use the values you discovered:
  int servoAngle = map(analogValue, 0, 1023, 0, 179);
}

Finally, add the servo library at the beginning of your code, then make a variable to hold an instance of the library, and a variable for the servo’s output pin. In the setup(), initialize your servo using servo.attach(). Then in your main loop, use servoAngle to set the servo’s position.

#include <Servo.h>      // include the servo library

Servo servoMotor;       // creates an instance of the servo object to control a servo
int servoPin = 2;       // Control pin for servo motor

void setup() {
  Serial.begin(9600);       // initialize serial communications
  servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
} 

void loop()
{
  int analogValue = analogRead(A0); // read the analog input
  Serial.println(analogValue);      // print it

  // if your sensor's range is less than 0 to 1023, you'll need to
  // modify the map() function to use the values you discovered:
  int servoAngle = map(analogValue, 0, 1023, 0, 179);

  // move the servo using the angle from the sensor:
  servoMotor.write(servoAngle);
}

Get Creative

This is just a suggestion for a short project. It’s not a requirement for the class homework.

Servo motors give you the power to do all kinds of things.

They can be used to push a remote control button, in a pinch:

Project by Tom Igoe

You can play drums like in this Project by Nick Yulman.
You can build a frisking machine like in this Project by Sam Lavigne and Fletcher Bach.

If you’ve got 800 or so of them and a lot of time, you can build a wooden mirror like this Project by Daniel Rozin

Come up with a project of your own that needs a little movement, and see if you can solve the problem with a servomotor.