Datasheet
Shield, Lights, Servo Motors • Chapter 2
Robotics with the BOE Shield-Bot • 57
How to Use the Arduino Servo Library
A better way to generate servo control signals is to include the Arduino Servo library in your
sketch, one of the standard libraries of pre-written code bundled with the Arduino software.
To see a list of Arduino libraries, click the Arduino software’s Help menu and select
Reference. Or, if using codebender : edu, go to the Arduino Library reference
page: https://www.arduino.cc/en/Reference/Libraries
Find and follow the Libraries link.
Find and follow the link to the Servo library.
Follow and read the links for these functions on the Servo library page:
attach()
writeMicroseconds()
detach()
Servos have to receive high-pulse control signals at regular intervals to keep turning. If the
signal stops, so does the servo. Once your sketch uses the Servo library to set up the signal, it
can move on to other code, like delays, checking sensors, etc. Meanwhile, the servo keeps
turning because the Servo library keeps running in the background. It regularly interrupts
the execution of other code to initiate those high pulses, doing it so quickly that it’s
practically unnoticeable.
Using the Servo library to send servo control signals takes four steps:
1. Tell the Arduino editor that you want access to the Servo library functions with the
#include declaration at the start of your sketch, before the
setup function.
#include <Servo.h> // Include servo library
2. Declare and name an instance of the Servo library for each signal you want to send,
between
#include and the setup function.
Servo servoLeft; // Declare left servo
3. In the setup function, use the name you gave the servo signal followed by a dot,
and then the attach function call to attach the signal pin. This example is telling
the system that the servo signal named
servoLeft should be transmitted by
digital pin 13.
servoLeft.attach(13); // Attach left signal to pin 13