Sub topic 4
How a Hobby Servo Reads Its Position Command
This chapter assumes you have read the earlier chapters on why motors need external power and a shared ground, and on PWM with analogWrite. You should be comfortable with pinMode, digitalWrite, delay, and the idea of a microsecond (µs), which is one millionth of a second.
A hobby servo takes a single signal wire and moves its shaft to a position. The signal looks like PWM, but the servo reads it differently from an LED or a motor driver. Knowing what the servo measures explains why the Servo library works and why servos jitter, buzz, or ignore commands.
What is inside a hobby servo
A hobby servo is a small closed-loop position system in one case. It contains four parts:
- A small DC motor.
- A gearbox that trades the motor's speed for torque.
- A potentiometer (a variable resistor) attached to the output shaft, which reports the shaft's angle.
- A control circuit that compares the commanded position with the measured one and drives the motor to reduce the difference.
The loop runs inside the servo, not on your Arduino. The Arduino only says where the shaft should be. The servo does the rest.
The command is a pulse width
The servo does not care about the average voltage on the signal wire, which is what analogWrite changes on an LED. It measures how long each pulse stays HIGH. The pulse width is the command.
A typical hobby servo follows this convention:
| Pulse width | Typical shaft position |
|---|---|
| 1000 µs (1.0 ms) | One end of travel |
| 1500 µs (1.5 ms) | Center |
| 2000 µs (2.0 ms) | Other end of travel |
Many servos accept a wider range, often about 500 to 2500 µs, and travel further over it. The exact limits differ between models and even between units of the same model. The 1000 to 2000 µs range is the nominal standard, not a guarantee.
The pulse width maps linearly to angle, so a 1 ms change produces roughly 90 degrees of movement on a servo with 180 degrees of travel. That is about 11 µs per degree. Every degree you want to resolve needs about 11 µs of timing accuracy, which is why the timing must come from hardware timers and not from sloppy software delays.
Frame rate: the pulse must repeat
The servo needs a fresh pulse about every 20 ms, which is 50 times per second (50 Hz). Each pulse and its idle gap make up one frame.
|<------------- 20 ms frame ------------->|
____ ____
| | | |
__| |_____________________________________| |____
1.5 ms HIGH ~18.5 ms LOWThe idle time is not important in itself. The servo only measures the HIGH time. But the repetition matters for two reasons:
- The servo's controller updates its target each time a pulse arrives. If pulses stop, many analog servos lose their command. Depending on the model, the shaft may go limp, drift, or keep holding the last position.
- Many servos work only within a limited frame rate. Most tolerate somewhere between 40 and 60 Hz. Some digital servos accept 300 Hz or more, but sending that rate to an analog servo can overheat it. At 50 Hz you are safe with nearly everything.
The Servo library handles the repetition for you. After servo.write(...), the library keeps sending the pulse every 20 ms with a hardware timer, even while your loop() does other work or blocks in delay(). You never need to resend the same angle.
Generating the signal by hand
The sketch below creates the pulses without the Servo library. It shows exactly what the library automates. It sends 50 frames of each pulse width, which is about one second at each position.
const int SERVO_PIN = 9;
// Sends one frame: a HIGH pulse of pulseUs microseconds, then idle.
void sendFrame(unsigned int pulseUs) {
digitalWrite(SERVO_PIN, HIGH);
delayMicroseconds(pulseUs);
digitalWrite(SERVO_PIN, LOW);
delay(20); // frame is about 20 ms plus the pulse, close to 48 Hz
}
void holdFor50Frames(unsigned int pulseUs) {
for (int i = 0; i < 50; i++) {
sendFrame(pulseUs);
}
}
void setup() {
pinMode(SERVO_PIN, OUTPUT);
digitalWrite(SERVO_PIN, LOW);
}
void loop() {
holdFor50Frames(1000); // one end
holdFor50Frames(1500); // center
holdFor50Frames(2000); // other end
holdFor50Frames(1500); // back to center
}Power the servo from an external 5 V supply and connect its ground to the Arduino ground, as in the earlier chapter. Do not take servo power from the Arduino 5 V pin unless it is a very small servo that is unloaded.
This manual approach works, but it has costs:
- The Arduino can do nothing else while it generates pulses, because the timing relies on blocking calls.
- Interrupts such as the
millis()timer tick can stretch a pulse by a few microseconds. That is a small shift in angle, which the servo turns into visible twitching. - Each additional servo needs its own timing, which gets messy quickly.
The Servo library avoids these problems by using a hardware timer and interrupts to produce pulses in the background. Chapter material on attach, write, and writeMicroseconds builds on this.
How the servo decides to move
The control circuit measures the pulse width and converts it into a target position. It then reads the potentiometer, subtracts, and drives the motor in proportion to the error. This is proportional control. A large error gives a strong, fast push, and a small error gives a weak one.
Three behaviors follow from this design:
- Deadband. The controller ignores errors smaller than a small threshold, typically a few microseconds of pulse width. Pulses that differ by 1 to 5 µs might produce no motion at all. A deadband prevents constant hunting around the target. It also means the finest steps you can command depend on the servo.
- Active holding. A servo that has reached its target keeps working. If you push the shaft away, the controller drives the motor back. That is why a loaded servo draws current and feels warm even while stationary, and why a servo pushed against a stop can buzz or stall.
- Speed is not commanded. The servo moves to the new target as fast as it can. A jump from 1000 to 2000 µs sends the shaft at full speed, with a current spike. If you want slow motion, you must send many small steps over time. That is the subject of the smooth-motion chapter.
What the servo does not tell you
The signal wire carries commands in one direction only. A standard hobby servo gives the Arduino no information about its real position. When you call write(90), you know what you asked for. You do not know whether the horn arrived. If the load is too heavy, if the supply sags, or if the gear is blocked, the Arduino has no way to find out.
If you need real feedback, you can use a servo with an extra feedback wire, or add your own sensor, or switch to a different actuator. Do not assume the angle you wrote is the angle you have.
Variations and edge cases
- Continuous-rotation servos. These have the potentiometer feedback removed or disconnected. The pulse width then sets speed and direction, not position. About 1500 µs stops the motor, less than that turns one way, and more turns the other. The exact stop point varies, and a small trimmer on the servo usually lets you adjust it.
- Digital servos. The pulse command is the same. The difference is that a microcontroller inside updates the motor drive much faster. They typically hold position more firmly and draw more current, and they can be more sensitive to supply sag.
- Limits of travel. If you command a pulse beyond the mechanical range, the servo pushes against its end stop. The motor stalls, draws high current, and can strip plastic gears. Find the safe limits by testing slowly with
writeMicrosecondsand leave a margin, as the chapter's sweeps do with 10 and 170 degrees. - Signal voltage. A 5 V Arduino drives the signal line with ordinary 5 V logic, which every common servo accepts. Servos powered at 6 V generally still read 5 V as HIGH. With a 3.3 V board, most servos still work, but marginal ones may not. Check the datasheet when in doubt.
- Power-up behavior. When a servo first receives power and a pulse, it moves immediately to the commanded position, which can be a fast, large swing. Set a safe starting position early in
setup(), and consider the pose the servo takes before any signal arrives. - Floating signal line. If the signal pin is not yet set as an output, or the wire is disconnected, the line can pick up noise. The servo may then twitch randomly. A weak pull-down resistor of about 10 kΩ from the signal line to ground can keep it quiet during reset.
Common mistakes
- Using
analogWriteon a servo pin. Its frequency is about 490 Hz or 980 Hz, and its pulse widths are not in the servo's range. The servo will behave erratically. - Expecting identical angles from different servos. Two servos given the same pulse can sit several degrees apart.
- Treating 1000 to 2000 µs as exact. Check for stalling at the ends, and measure.
- Powering servos from the board 5 V pin and then blaming the code for resets.
Exercises
Exercise
A servo is receiving a 1500 µs pulse every 20 ms. You change the pulse to 1750 µs and keep the same frame rate. What changes at the servo, and does the average voltage on the signal wire matter?
Show answer
The target moves from center to roughly a quarter of the full range beyond center (about 45 degrees on a servo with 180 degrees of travel over 1000 to 2000 µs), and the shaft turns there. The average voltage on the signal wire does not matter. The servo measures only the width of the HIGH pulse.
Exercise
Why can a servo run from analogWrite on a PWM pin not be made to behave correctly, even though both are PWM signals?
Show answer
analogWrite produces a fixed frequency (about 490 Hz or 980 Hz on common boards) and varies the duty cycle, so the HIGH time is only a few milliseconds at most and the period is far shorter than 20 ms. A servo expects a pulse of 1 to 2 ms every 20 ms, so the timing is wrong and the servo will not hold a stable position.
Exercise
You write a hand-timed servo sketch and call delay(2000) in the middle of loop() without sending frames. Predict what happens to the servo, and explain why the Servo library behaves differently.
Show answer
During the delay no pulses reach the servo. Depending on the model, it may go limp, drift, or hold, and it can jump when pulses resume. The Servo library avoids this because its hardware timer keeps sending the last commanded pulse every 20 ms in the background, even while loop() is blocked.