From the angle to the pulse width and from the pulse to the timer registers
Pulse widths of 1000 to 2000 µs are the standard, but many servos accept 500 to 2500 µs for a wider travel; check it gradually so as not to force the mechanical stops. Indicative currents for a small SG90-type servo; an MG996R can exceed 2 A when stalled.
A hobby servo combines in one housing a DC motor, a gearbox, a potentiometer attached to the output shaft and a small control circuit. The circuit compares the position measured by the potentiometer with the one requested by the signal and drives the motor until they match: it is a closed position loop. That is why a servo holds its angle even when pushed, up to the limit of its torque.
The position is set with a pulse repeated every 20 ms (50 Hz). What matters is its width, not the duty cycle: 1 ms takes the shaft to one end, 1.5 ms to the centre and 2 ms to the other end. The period can vary somewhat without the servo noticing.
In a microcontroller, the pulse is generated with a timer in PWM mode: the period sets the maximum count (TOP) and the width, the compare value. A 16-bit timer gives resolutions of hundredths of a degree. An 8-bit PWM at 50 Hz, on the other hand, has only about 13 useful steps between 1 and 2 ms: the servo moves in jumps of more than 10°. That is why servo libraries use 16-bit timers or generate the pulse with interrupts.
| Type | Characteristics |
|---|---|
| Analog | Refreshes the position with each pulse, at 50 Hz; cheaper, with some buzz and play |
| Digital | Internal microcontroller at several hundred Hz: more holding torque and accuracy, more consumption |
| Continuous rotation | No stop and no position feedback: the pulse sets speed and direction; 1.5 ms is stopped |
| Industrial servo motor | AC or brushless motor with an encoder and a servo drive; position, speed and torque control for machines |
If the servo vibrates or jitters at rest, the usual causes are supply noise, an unstable pulse (generated in software with interrupts) or a load that forces it to correct continuously.
Related: PWM calculator, stepper motors, DC motors with an H-bridge and state machines.