Hardware Reference
In-Depth Information
Servoshaveatendencytoconsumecurrentinburstsastheyturn.They
generallyconsumelittlecurrentwhilestationary,andtheyconsumeseveral
hundredmilliampsforafewmillisecondswhentheyareactuated.These
currentspikescanrippleonthe5Vline,andcanevenbeseeninothercom-
ponents,likeLEDs.Bykeepingthesupplyfortheservoonaseparaterail,
youensurethatthisdoesnothappen.
Insuficientcurrentforaservomightalsocauseittobehaveerratically.
Whenyouinishtheinalprojectforthischapter,tryhookingtheservosup-
plypinuptothebuilt-in5Vrail.(Don'tworry;thiswon'tdamageanything.)
WhentheservoispoweredoverUSB,youmayseetheservodoingallkinds
ofunexpectedmotionsduetoaninsuficientcurrentsupply.Naturally,the
degreeofthisbehaviordependsonthespeciicationofyourparticularservo.
ControllingaServo
The Arduino IDE includes a built-in library that makes controlling servos a
breeze. A software library is a collection of code that is useful, but not always
needed in sketches. The Arduino IDE contains a number of libraries for com-
mon tasks. The servo library abstracts the timing routines you would need to
write out on your own for pulsing the servo pin. All you have to do is attach a
servo “object” to a particular pin and give it an angle to rotate to. The library
takes care of the rest, even setting the pin as an output. The simplest way to
test out the functionality of your servo is to map the potentiometer directly to
servo positions. Turning the potentiometer to 0 moves the servo to 0 degrees,
and moving it to 1023 moves the servo to 180 degrees. Create a new sketch with
the code from Listing 4-4 and load it onto your Arduino to see this functional-
ity in action.
Listing 4-4: Servo Potentiometer Control—servo.ino
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int POT=0; //POT on Analog Pin 0
Servo myServo;
int val = 0; //for storing the reading from the POT
void setup()
{
myServo.attach(SERVO);
 
Search WWH ::




Custom Search