Sub topic 5
Moving a Servo with the Servo Library
Assume you can upload an Arduino sketch, use setup() and loop(), and wire basic circuits. You should also understand that an Arduino pin provides a control signal, not enough power for a motor.
A hobby servo is a motor, gears, and a control circuit in one case. Its three wires usually connect as follows:
- Power: External 5–6 V supply
- Ground: External supply ground and Arduino
GND - Signal: An Arduino digital pin
The shared ground gives the Arduino and the servo the same voltage reference.
Including the Servo library
Arduino IDE includes the Servo library. The library generates the repeated timing signal that tells the servo where to move.
Create a Servo object, then attach it to the signal pin:
#include <Servo.h>
Servo armServo;
const int SERVO_PIN = 9;
void setup() {
armServo.attach(SERVO_PIN);
}
void loop() {
armServo.write(90);
delay(1000);
armServo.write(20);
delay(1000);
armServo.write(160);
delay(1000);
}attach() connects the library object to a pin. write() requests an angle in degrees. In this example, the servo moves to its center, then to two positions near its ends.
The requested range is normally 0 to 180 degrees, but the mechanical range depends on the servo. Some servos cannot safely reach either endpoint. Start with smaller values, such as 20 and 160, and expand the range only if the servo moves freely.
Understanding the control pulse
A servo does not receive a variable voltage that directly represents its angle. Instead, it receives a repeating series of pulses. The pulse width tells the internal controller the requested position.
The library's write() method converts an angle into a suitable pulse width. For more direct control, use writeMicroseconds():
#include <Servo.h>
Servo servo;
const int SERVO_PIN = 9;
void setup() {
servo.attach(SERVO_PIN);
}
void loop() {
servo.writeMicroseconds(1500);
delay(1000);
servo.writeMicroseconds(1100);
delay(1000);
servo.writeMicroseconds(1900);
delay(1000);
}The values are pulse widths in microseconds. A common starting range is about 1000 to 2000 microseconds, with 1500 near the center. These values are not universal. Check the servo's documentation and approach the limits gradually.
writeMicroseconds() is useful when you need calibration or when a servo's real range does not match 0–180 degrees. If the servo buzzes loudly, pushes against a stop, or becomes hot, reduce the range immediately.
Power and library details
The signal wire carries very little current, but the servo power wire may carry a large startup or stall current. Use an appropriate external supply and connect its ground to Arduino GND.
When the servo is detached, the library stops sending its control signal:
servo.detach();A detached servo usually stops actively holding its position. The arm may then move if an external force acts on it.
The Servo library can use a hardware timer. On some Arduino boards, this affects particular PWM functions on other pins. If another device's PWM output changes after you include Servo.h, check the board's Servo library documentation and choose a compatible pin or timer arrangement.
Exercise
A servo moves to an endpoint, buzzes, and becomes warm. What should you change first?
Show answer
Reduce the commanded angle or pulse-width range. The servo is probably being driven against a mechanical limit.