Servo PWM pulse-mapping helper

Calibrated endpoints

Enter the pulse widths your servo reaches at its two ends and the commands they mean, such as 0° and 180°. Endpoints may be given in either order.

No valid result to draw.

Show the steps

Interactive calculation is unavailable until JavaScript loads. The formulas, worked examples and references below remain available.

Map a servo command, such as an angle, to a pulse width between calibrated endpoints, and back, with the timer compare value for the PWM period.

How to use

Choose Pulse for a command, or Command for a pulse.

Enter the two pulse endpoints in microseconds and the command values they correspond to. Measured endpoints from your own servo give the most accurate mapping.

Enter the command, or the pulse width, or drag the slider between the endpoints.

Enter the PWM frequency and choose the timer resolution to see the period, the timer compare value and the pulse the timer actually produces.

The graph shows the mapping line between your endpoints and marks your value. Copy result copies the pulse and compare value. Reset restores the starting example: 0 to 180 between 1000 and 2000 μs, command 90, 50 Hz and 16 bits.

Science

A hobby servo reads the length of a repeating high pulse, not its duty cycle as such. About 1500 μs is centre; shorter and longer pulses move the horn towards each end.

Between the endpoints the position is close to proportional to the pulse width, so the command is mapped with a straight line through the two calibrated endpoints. A command outside the endpoints is refused rather than clamped.

Nominal endpoints such as 1000 and 2000 μs are a convention, not a guarantee. Many servos travel further with 500 to 2500 μs, and the Arduino Servo library defaults to 544 and 2400 μs. Driving past the mechanical stops makes a servo stall and draw high current.

The pulse repeats every period. 50 Hz (a 20 ms period) is the classic rate; many digital servos accept faster rates. The pulse must always be shorter than the period.

A hardware timer can only produce whole counts. With 2^b counts per period, each count is the period divided by 2^b, so low resolutions at low frequencies give coarse pulse steps. When the count cannot hit the pulse exactly, the tool says so and shows the error.

Assumptions and limitations: servo position is linear in pulse width between the endpoints, and the timer has 2^b counts per PWM period. Dead band, non-linearity and load are not modelled. For timers whose period is a different number of counts (for example ARR + 1 on STM32), replace 2^b with that count. General PWM duty-cycle, frequency and period calculations belong to the PWM duty-cycle calculator.

Formulas

Pulse for a command: P = Pmin + (c − cmin) × (Pmax − Pmin) ÷ (cmax − cmin)

Command for a pulse: c = cmin + (P − Pmin) × (cmax − cmin) ÷ (Pmax − Pmin)

Period: T = 1,000,000 ÷ f, in μs

Compare value: n = round(P ÷ T × 2^b) for a timer with 2^b counts per period

Produced pulse: Pq = n × T ÷ 2^b

Examples

Endpoints 1000–2000 μs for 0–180°, command 90°, 50 Hz, 16 bits: 1500 μs; 7.5 % of the period; compare value 4915; produced pulse 1499.94 μs. 90° is halfway, so the pulse is halfway between the endpoints. 1500 ÷ 20000 × 65536 = 4915.2, rounded to 4915, so there is no exact setting: the timer gives 0.06 μs less.

Arduino defaults 544–2400 μs for 0–180°, command 90°: 1472 μs. Halfway between 544 and 2400 is 1472, not 1500. Using the 1500 μs convention with these endpoints puts the horn slightly off centre.

Endpoints 1000–2000 μs for 0–180°, pulse 1200 μs: Command 36°. 1200 μs is one fifth of the way from 1000 to 2000, and one fifth of 180° is 36°.

References

Arduino Servo library source (Servo.h)

ESP-IDF LED Control (LEDC) programming guide

Arduino AVR core source, WMath.cpp: map()