Wiring and Controlling Servos with Motor Shield

Before we start we start wiring up our servo and motor shield it may be worth reviewing two related sparks;

The first, Steppers, Servos and DC Motors, tells us what the different types of electric motors are and what they are used for.

The second ,Controlling little motors: Relay Shield, introduces us to the  relay shield which is pace on the arduino to control motors and to manage their power requirements.

If you’ve already been there then the next step is to plug your servo into the Motor Shield – The adafruit motor shield can control two servos at once – although we can also control servos using the digital ports of our arduino without the overhead of the AFMotor library. Today we are using these servos with other motorised elements so we will use the ports available on the shield

The servo ports have three pins each and are clearly labelled -they can be found in the top corner of the shield just above the USB port.

Connecting our servo is simply a matter of plugging them in – unfortunately its not entirely clear which way the pins should be plugged in – on my servo the negative terminal is brown.


Here are the specs for the included micro-servo 

To control the servo once it is plugged in we require one library –  the servo library that is included as part of Arduino’s standard install. The shield is really only being used as a port here.

Once you’ve installed the AFMotor library you should be ready to copy and paste the commented code below and start exploring the limits and potential of this powerful little micro-servo.

/* Sweep
 by BARRAGAN <http://barraganstudio.com> 
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald

modified by Mat Wall-Smith for Polygon Door.

#include <Servo.h>   //include the standard arduino servo library
#define SERVO2_PWM 9  // define the port that we are plugged into
//the other option is SERVO1_PWM 10
Servo servo_2;  // create servo object to control a servo - call it servo_2
                // twelve servo objects can be created on most arduino boards using the PWM ports
int pos = 0;    // variable to store the servo position 
void setup() 
  servo_2.attach(SERVO2_PWM);     // attach the object to the port.
  servo_2.write(0);               // write a position to the servo_2 object
  delay(15);  //hang about while we get there
void loop() 
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    servo_2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
    servo_2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position