Home / Learn Embedded C / PWM for Servo Control

PWM for Servo Control

Using specially-timed PWM pulses to command a hobby servo motor's exact angle.

The gistUsing specially-timed PWM pulses to command a hobby servo motor's exact angle.

The idea, explained simply

LED/motor PWM vs servo PWMLED/motor PWMDuty cycle percentage is whatmattersFrequency can often varywithout issueServo PWMAbsolute pulse width(microseconds) is what mattersRepetition period matters too(~20ms typical)
LED/motor PWM vs servo PWM

What it is

A hobby servo is controlled not by varying duty cycle in the usual sense, but by the exact WIDTH of a pulse repeated roughly every 20ms — typically ~1ms for one extreme angle, ~1.5ms for center, ~2ms for the other extreme.

Why it matters

Servos are used throughout robotics and remote-control projects for precise angular positioning — understanding that servo PWM timing differs from LED/motor PWM is essential to driving one correctly.

Mechanically speaking

Unlike LED/motor PWM (where only the percentage matters), servo control is sensitive to the ABSOLUTE pulse width in time (microseconds), not a 0-255 style duty value — a subtle but important distinction.

Shows up in

  • Robotics joints/actuators
  • RC vehicle steering
  • Camera gimbals

Try it

Commanding three servo angles via pulse width (simulated)C: edit and run

A real servo driver uses a hardware timer's output-compare feature (covered next) to generate these exact pulse widths repeatedly, without CPU intervention each cycle.

Careful:Servo timing is in absolute microseconds, not a 0-255 duty percentage — mixing up the two PWM models is a common beginner mistake when moving from LED dimming to servo control.

Try it: mapping an angle in degrees to a pulse width

Converting a 0-180 degree angle into a servo pulse widthC: edit and run

Real servo libraries perform exactly this kind of linear mapping from a requested angle to the microsecond pulse width the servo expects.

Check yourself

1. What determines a hobby servo's commanded angle?