Skip to content
Tactiverge

School/△ Builder/Week 24

Talking to a servo in milliseconds

A servo has just one signal wire. How do you tell it 'turn to 135°' with a single wire that is only ever on or off?

About 30 min · 3 goals · Checked by automated tests

1.Why should I care?

Servo signals are the friendliest introduction to encoding information in time: the message is not the voltage but how long the voltage stays high. The same idea underlies PWM power control next week and many sensor outputs.

  • ◇ Explain how a hobby servo reads an angle from the width of a pulse.
  • ◇ Convert between pulse width and angle with a linear mapping.
  • ◇ Calculate the duty cycle of a 50 Hz servo signal.

2.Predict

A servo treats a 1.0 ms pulse as 0° and a 2.0 ms pulse as 180°. What pulse width means 90°?

Guess first. Being wrong here is how the surprise works.

3.Discover

Set the PWM frequency to 50 Hz. Find the duty cycles that give a high time of 1.0 ms, 1.5 ms and 2.0 ms per period. (Ignore the motor readout here: a servo listens to pulse width, not average voltage.)

Loading the interactive…

Can't use the interactive? Read a description instead

The PWM visualiser shows a square wave whose high time is the duty cycle times the period. At 50 Hz the period is 20 ms, so 5% duty gives a 1.0 ms pulse, 7.5% gives 1.5 ms and 10% gives 2.0 ms: the pulse widths a hobby servo reads as 0°, 90° and 180° in the common mapping.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A hobby servo is told 1.0 ms means 0° and 2.0 ms means 180°, in a straight line between. How long a pulse sets it to 0°, in milliseconds?

ms

5.Watch

→ Look for the parts inside the servo, especially the position sensor, and how the control circuit compares the pulse with the measured angle.

An animated explainer of what is inside a hobby servo and how it turns to, and holds, the angle it is told.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

6.Understand

A pulse every 20 ms

A hobby servo expects a pulse about every 20 ms (50 times a second). The width of each pulse carries the command. A common mapping is 1.0 ms → 0°, 1.5 ms → 90°, 2.0 ms → 180°. The voltage of the pulse is just 'high'; the information is in the time.

A straight-line mapping

Between the ends, angle and pulse width are linked by a straight line: angle = (pulse − 1.0 ms) × 180° per ms, and pulse = 1.0 ms + angle ÷ 180° × 1 ms. So each 0.1 ms is worth 18°. In Python:

def pulse_ms(angle):
    return 1.0 + angle / 180

Duty cycle of a servo signal

The duty cycle is the fraction of each period spent high. A 1.5 ms pulse every 20 ms is 1.5 ÷ 20 = 7.5%. The whole servo range lives between 5% and 10% duty. Microcontrollers make this signal with a timer-driven PWM peripheral, which you meet in week 25.

Explain it another way

Morse code with one long beep: the longer the beep, the further round the servo turns.

Worked example 1

What pulse width sets 45°, and what duty cycle is that at 50 Hz?

Pulse = 1.0 + 45 ÷ 180 = 1.25 ms. Period = 1 ÷ 50 = 20 ms. Duty = 1.25 ÷ 20 = 6.25%.

Common mix-ups

  • A servo's angle is set by the voltage on its signal wire.
    ✓ It is set by the pulse width. The signal is always either high or low.
  • All servos use exactly 1–2 ms.
    ✓ That is a common convention; real models vary, so check the datasheet and test the ends carefully.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A hobby servo is told 1.0 ms means 0° and 2.0 ms means 180°, in a straight line between. It receives a pulse 1.85 ms long. What angle does it turn to, in degrees?

°

Practice 3

A servo signal has a 20 ms period and a 1.8 ms pulse. What is the duty cycle, in percent?

%

Practice 4

What carries the angle command in a hobby servo signal?

Practice 5

You send a 2.6 ms pulse to a servo whose range is 1.0–2.0 ms. What is the risk?

8.Apply it

Write a Python function servo_pulse(angle) that clamps the angle to 0–180°, returns the pulse width in ms, and a second function that returns the duty cycle in % at 50 Hz. Test both at 0°, 90°, 180° and at −20° and 200°.

9.Reflect

Concepts: Motors, servos and steppers · Timers and pulse-width modulation