TactivergeLite

Lite / Building Machines That Respond / 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 · Full interactive version

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.

Predict

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

  1. 0.5 ms
  2. 1.5 ms
  3. 9 ms
Guess first, then reveal

90° is halfway through the range, so the pulse is halfway between 1.0 and 2.0 ms: **1.5 ms**. Many servos use roughly this mapping, but the exact ends vary by model, so check the datasheet and test gently.

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.)

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Servo Motors, how do they work?” by The Engineering Mindset (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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

Technical

The servo's control circuit converts pulse width into a target angle and runs a proportional position loop using a potentiometer on the output shaft. End-point pulse widths vary between models (some use about 0.5–2.5 ms); driving beyond the mechanical stops makes the servo strain and heat up.

Mathematical

angle = 180 × (p − 1)/(2 − 1) for p in ms; duty = p ÷ T × 100% with T = 20 ms.

Applied

Radio-control planes steer their flaps, rudders and throttles with exactly these pulses. Small robot arms and pan-tilt camera mounts often do too.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

ms

°

%

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?

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°.

Reflect

Why might engineers prefer to send information as a pulse width rather than as a voltage level?

Write your answer on paper or in your own notes. Lite never stores what you write.

← DC motors, servos and steppersChoosing an actuator, and why pins need help →