SERVOMOTORS
makezine.com/19/primer
0 degrees
45 degrees
180 degrees
High
High
High
Low
1,000 microseconds
1,250 microseconds
Low
2,000 microseconds
Low
F
Arduino microcontroller.
The HIGH part of the pulse lasts between 1 and 2
milliseconds (ms), equal to 1,000 to 2,000 microseconds (μs). At 1,000μs, the servo will rotate to its
full anti-clockwise position. At 2,000μs, it will rotate
to its full clockwise position. Some servos accept
shorter or longer pulses and have a correspondingly
larger rotational range.
The LOW part of the control pulse lasts for 20
milliseconds. Every 20ms ( 50 times per second),
the HIGH pulse must be received again or the servo
will de-energize and not hold its position. This is
useful if you want your project to “go limp.”
Below is a complete Arduino sketch that will
continually position the servo at its midpoint.
Controlling a servo is pretty easy.
G
int servoPin = 9;
int servoPosition = 1500; // position in microseconds
void setup() {
pinMode(servoPin, OUTPUT);
}
void loop() {
digital Write(servoPin, HIGH);
delayMicroseconds(servoPosition);
digital Write(servoPin, LOW);
delay( 20); // wait 20 milliseconds
}
The problem with this Arduino sketch is that it
spends most of its time stuck in the delay commands. Fortunately, the Arduino’s built-in Servo
library lets you control 2 servos (on pins 9 and 10)
using its built-in timers, and frees up your code to
do other things. The same sketch using the library
would be:
Wire up the servo as in Figure G. Red and black
wires go to the Arduino’s 5V power and Gnd pins.
The control wire goes to digital input/output pin 9.
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach( 9); // servo is on pin 9 like before
myservo.write( 90); // set servo to 90 degree position
}
void loop() {
// free to do anything, our servo is still being driven for us
}
144 Make: Volume 19