Sub topic 4
How a Hobby Servo Reads Its Position Command
Prerequisites and the control signal
This chapter assumes that you can upload an Arduino sketch, use setup() and loop(), and understand voltage, current, ground, and digital pins. You should also know that a servo needs a suitable power supply and that the Arduino ground and servo ground must be connected.
A hobby servo is a motor, gearbox, position sensor, and controller in one package. The Arduino does not directly set the motor voltage. Instead, it sends a repeating timing signal. The servo measures each signal pulse and turns its internal motor until its output shaft reaches the requested position.
The signal has two important parts:
- The pulse width is the length of time the signal stays HIGH.
- The period is the time from the start of one pulse to the start of the next pulse.
Many hobby servos expect a period close to 20 milliseconds, or about 50 pulses per second. A pulse near 1,000 microseconds commonly requests one end of the travel. A pulse near 1,500 microseconds requests the middle. A pulse near 2,000 microseconds requests the other end.
These values are conventions, not universal limits. Some servos accept a wider range, while others may be damaged or strained if commanded beyond their mechanical stops.
| Pulse width | Typical request |
|---|---|
| 1,000 microseconds | Near one end |
| 1,500 microseconds | Near the center |
| 2,000 microseconds | Near the other end |
The signal is not ordinary PWM speed control. With analogWrite(), PWM usually means changing the duty cycle to control average power. A servo instead interprets the width of each individual pulse as a position command. The repetition rate also matters because the servo uses repeated commands to maintain its position.
What happens inside the servo
Inside the servo, a position sensor reports the shaft angle to a small controller. The controller compares the measured angle with the requested angle. If the shaft is too far in one direction, it drives the motor in that direction. If it is too far in the other direction, it reverses the motor. When the error becomes small, it reduces or removes motor power.
This is a feedback loop. Feedback means that a system measures its result and adjusts its action based on the difference between the requested result and the measured result.
The servo may continue making small corrections even when it appears still. A load, loose gearbox, noisy signal, or poorly regulated power supply can make those corrections visible as jitter.
A centered test
The Servo library creates the correctly timed pulses for you. This sketch commands the center position and leaves the servo holding it:
#include <Servo.h>
Servo servo;
const int SERVO_PIN = 9;
void setup() {
servo.attach(SERVO_PIN);
servo.writeMicroseconds(1500);
}
void loop() {
// The Servo library continues generating the signal.
}The servo signal wire connects to pin 9 in this example. Connect the servo power wire to a suitable supply, not automatically to an Arduino I/O pin. Connect the supply ground, servo ground, and Arduino GND together. The shared ground gives the pulse signal a reference that the servo can measure.
A servo may move to a slightly different angle than another servo receives from the same pulse width. Manufacturing tolerances, calibration, gear backlash, and the available supply voltage all affect the result. Treat the pulse width as a request, not a guaranteed physical angle.
Exercise
Why can a 1,500-microsecond pulse center one servo but produce a slightly different angle on another?
Show answer
The pulse is interpreted through each servo's calibration, sensor, gearbox, and mechanical limits. The timing requests a nominal position, but the physical angle is not guaranteed to be identical.