TL;DR:
A hobby servo holds an angle because it contains a motor, a gearbox, a position sensor and a small controller.
You command it with a repeating pulse, commonly every 20 ms, whose width (often 1 to 2 ms) sets the target angle.
A servo pushed past its limits or overloaded stalls, draws high current and can strip its gears, so check the range and the supply.
You push the lever of a toy robot arm and feel the joint push back. Let go, and it returns to where it was. A servo does that: you tell it where to be, and it keeps working, quietly and continuously, to stay there. A regular motor has no idea where its shaft is. A servo does.
What a servo is made of
A hobby servo is four things in one case. A small DC motor does the work. A gear train slows the motor and multiplies its torque. A potentiometer (a variable resistor) or another angle sensor is attached to the output shaft, so the servo can measure its own angle. A small circuit compares the angle you asked for with the angle it measures and drives the motor toward the target.
That comparison is feedback. The difference between the target and the measured angle is the error. When the error is large the circuit drives the motor hard, and as the shaft approaches the target the drive shrinks. At zero error the motor stops. If something pushes the shaft away, the error comes back and the motor pushes back, which is why a servo seems to hold its position.
How you command it: pulse width
The command is a repeating pulse on a signal wire. A very common convention is a pulse every 20 ms, which is a frequency of 1 / 0.020 s = 50 Hz. The pulse width carries the angle. Typical hobby servos treat about 1.5 ms as the center, about 1.0 ms as one end and about 2.0 ms as the other end. These values vary between models, so read your servo's datasheet and test before you trust them.
This is PWM, pulse width modulation, but you are using it differently from a motor driver. A motor driver uses the average voltage of the pulses. A servo ignores the average and measures the width of each pulse with its own circuit.
Worked example: a hobby servo commanded by pulse width
Assume a servo whose datasheet says 1.0 ms means 0° and 2.0 ms means 180°, a straight-line relationship, with a 20 ms period. The angle is angle = (pulse − 1.0 ms) / 1.0 ms × 180°.
Command 90°. Pulse
= 1.0 + 90 / 180 = 1.5 ms. As a duty cycle that is1.5 / 20 = 0.075.Command 45°. Pulse
= 1.0 + 45 / 180 = 1.25 ms. Duty cycle1.25 / 20 = 0.0625.Command 120°. Pulse
= 1.0 + 120 / 180 = 1.667 ms.On a microcontroller timer ticking every 1 µs, the 45° command is
1250ticks high out of20000per period. Check:1250 / 20000 = 0.0625, matching step 2.
To check the formula backwards, a 1.667 ms pulse gives (1.667 − 1.0) × 180 = 120°. Good.
Notice how small the numbers are: the whole 180° swing is only 1 ms of pulse width, so a timing error of 10 µs is 10 / 1000 × 180° = 1.8°. That is why servo signals come from hardware timers, not from loops that wait.
The limits you have to respect
Mechanical stops. Many servos have a physical limit, or gears that will strip if you push beyond the end. If you send a pulse that asks for more than the range, the shaft jams against the stop and strains. Command only inside the range you tested.
Speed. The servo moves at its own maximum rate. If you jump from 0° to 180° in one step it does not teleport. If you need smooth motion, step the target gradually.
Torque and stall. Hold a load that is too heavy and the motor stalls, the current climbs and the servo gets hot. Check the stall torque on the datasheet and leave margin, as in the torque-speed discussion in how DC motors turn electricity into motion.
Power. A servo can draw an amp or more when it starts moving. If it shares a supply with the microcontroller, the voltage dips and the board resets. Give the servo its own supply, and connect the grounds together.
Jitter and buzzing. Servos can buzz or twitch when the signal is noisy or the load is near the limit. Better power and a cleaner signal usually fix it.
Continuous-rotation servos. These look the same but have no angle sensor feedback. The pulse width sets speed and direction, not position.
Safety: A servo arm can swing instantly when power comes on or when your code starts, and the gears can pinch fingers. Keep your hands clear, power up with the arm in a safe position, and never force a powered servo by hand to the ends of its travel. If a servo hums and will not move, switch it off rather than letting it stall.
How real robots use servos
Servos are the standard joint in small robot arms, grippers, pan-and-tilt camera mounts, legs on hobby walkers and steering on model cars. Bigger robots use the same idea with stronger motors and better angle sensors, often with a digital bus instead of a pulse. If you want to know what angles to send so a gripper reaches a point, that is the job of inverse kinematics. If you need exact steps without any feedback sensor at all, look at stepper motors.
Try it yourself
Wire one hobby servo to a separate battery pack, with the grounds joined to your microcontroller. Send 1.0 ms, 1.5 ms and 2.0 ms pulses in turn, with a pause between, and mark the shaft angle each time. Then calculate your own line of angle against pulse width from what you measure, and compare with the datasheet. Move in small steps near the ends, not all at once.
Frequently asked questions
Can a servo hold a position with the power on forever?
It can, as long as the load is within its torque rating. Holding costs current, though, and a heavy load makes it hot and noisy, so size the servo with margin.
Why do I need a signal every 20 ms?
The servo's circuit expects a fresh pulse regularly. If the pulses stop, many servos go limp or stop holding, so keep sending the command while you want it to hold.
