Skip to main content

Subsection 13.1.4 How a Servomotor Is Commanded

In order to control the position of a servomotor, we first need to understand the protocol we use to communicate to the servo the position we want it to move to. The servo expects a pulse on its signal lead, between 1 ms and 2 ms wide, repeated regularly, roughly every 20 ms (so at 50 Hz). The width of the pulse is conveying information to the controller on how far to turn the motor (Figure 13.1.4): the servo’s controller electronics convert the pulse width into the control voltage that its feedback loop works from. A pulse train like this is a PWM (pulse-width modulation) signal, so we can produce it with the same timer we used for the motor, but it is a different use of PWM. For the motor, the fraction of each period spent HIGH set an average voltage; here the width of the pulse itself is the message, and the period only has to be roughly right.
Figure 13.1.4. The command pulse. Each row is one pulse width, 1 ms, 1.5 ms and 2 ms, repeated about every 20 ms. The angles and the turn directions are the convention described below, and the drawing is not to scale.
Servos do not have an I2C interface. The technology is fairly old, and this pulse is the historic way to control them. By convention, a 1.5 ms pulse brings the servo to the middle, a zero angle; 1 ms turns it to −90 degrees and 2 ms turns it to +90 degrees. Those endpoints are hypothetical: the SG90 datasheet says the servo rotates approximately 180 degrees, but in reality our servos do not go that far, and how far depends on the individual servo.