Robotics and Embedded Systems

Lesson 15: Servo Motors and Precise PositioningNew-age

A DC motor spins continuously; a servo motor moves to a position and stays there. That difference makes servos the right choice whenever a robot needs to aim, point, lift or grip — a sensor that sweeps, an arm that picks up, a steering mechanism that turns a precise angle. This lesson explains how a servo works, how to control it with a single signal wire, and how to build the sweeping sensor mount that the later navigation lessons depend on.

A servo is also a good lesson in feedback. Inside every servo is a small DC motor, a set of gears, a potentiometer that measures the output angle, and a control circuit that drives the motor until the measured angle matches the commanded one. The servo is a complete control loop in a small box, and understanding it will help you understand the bigger control loops in Lesson 16 and beyond.

Key Terms.
  • Servo motor — a motor that moves to a commanded angle and holds it.
  • Pulse width — the length of the control pulse, usually 1–2 ms, that sets a servo’s angle.
  • Feedback — measuring the actual result and comparing it with the desired result.
  • Gear train — the gears inside a servo that trade speed for torque.
  • Torque — the turning force a servo can produce, such as 1.8 kg·cm.
  • Range — the span of angles a servo can reach, usually 0° to 180°.
  • Jitter — small unwanted movements of a servo, often caused by a noisy power supply or signal.
  • Continuous rotation servo — a modified servo that spins continuously, with the signal controlling speed, not angle.

How a servo works

A servo has three wires: power (usually red, 5 V), ground (brown or black), and signal (orange or yellow). The signal is a pulse sent 50 times a second: a 1 ms pulse commands 0°, a 1.5 ms pulse commands 90°, and a 2 ms pulse commands 180°. The servo’s control circuit compares the commanded angle with the angle measured by its internal potentiometer and drives the motor until they match — a feedback loop that holds the position even if something pushes against it.

The gears inside trade speed for torque: a small, fast motor drives a set of gears that turn slowly but strongly. A typical small servo, the SG90, produces about 1.8 kg·cm of torque — enough to lift a small sensor or turn a light arm, but not to drive a wheel or lift a heavy load.

Controlling a servo from code

The Arduino Servo library hides the pulse timing: attach the servo to a pin with servo.attach(9), then command an angle with servo.write(90). The library generates the pulses in the background, so your program stays free to do other work. Command angles in whole degrees, and move in steps rather than jumps: sweeping from 0° to 180° in steps of 1° with a 15 ms pause between steps gives a smooth motion, while a single jump from 0° to 180° jerks the mechanism and can strip the gears.

Never power a servo from the microcontroller’s 5 V pin if it must move a load: a servo can draw 500 mA or more when it starts moving, which will brown out the board. Power servos from a separate 5 V supply — a battery pack or a regulator — with the grounds joined. A servo that twitches or resets the board is almost always a power problem, not a code problem.

A sweeping sensor mount

The classic servo project in robotics is a sensor mount: an ultrasonic sensor on top of a servo, sweeping left and right to measure the distance in each direction. The robot can then compare the distances and choose the clearest path — the basis of the obstacle avoidance in Lesson 16. Build the mount so the sensor is balanced over the servo’s shaft, and so the wires have enough slack to turn without pulling.

Limits and care

A standard servo turns about 180°, not 360°: commanding 200° will drive it against its internal stop, drawing a large current and possibly stripping the gears. Stay within 0° to 180°, and preferably within 10° to 170°, so the servo never hits its stops. A continuous rotation servo is different: it has no stops, and the signal controls speed and direction instead of angle — useful for wheels, useless for aiming.

Worked example — sweeping a sensor

The code below sweeps a servo from 0° to 180° in 30° steps, taking an ultrasonic reading at each step and printing the angle and distance. It uses the Servo library and the distance function from Lesson 13.

#include <Servo.h>
Servo s;
void setup() { s.attach(9); }
void loop() {
  for (int a = 0; a <= 180; a += 30) {
    s.write(a);
    delay(200);              // let the servo settle
    int d = readDistance();  // ultrasonic, in cm
    Serial.print(a); Serial.print(": "); Serial.println(d);
  }
}

Common mistakes

  • Powering a servo from the microcontroller’s 5 V pin. It draws too much current and resets the board.
  • Commanding an angle outside 0–180°, driving the servo against its internal stop.
  • Moving a servo in one big jump instead of steps, jerking the mechanism.
  • Reading a sensor while the servo is still moving, so the reading belongs to the wrong direction.

Discussion questions

  1. A servo holds its position when pushed. What is doing the holding — the motor, the gears, or the control circuit? How would you test it?
  2. A sweeping sensor gives a robot a map of its surroundings. What does the map leave out, and when would that matter?
  3. Why is a servo better than a DC motor for aiming a sensor, and a DC motor better than a servo for driving a wheel?

Practice questions

  1. What does a 1.5 ms pulse command a standard servo to do? Answer: Move to 90°, the centre of its range. 1 ms is 0° and 2 ms is 180°. Explanation: Pulse width = angle.
  2. Why should a servo be powered separately from the microcontroller? Answer: It can draw 500 mA or more when it starts moving, which browns out the board. Separate power with a shared ground is the safe arrangement. Explanation: Servos are hungry.
  3. What is feedback, and where is it inside a servo? Answer: Measuring the actual result and comparing it with the desired one. Inside a servo, a potentiometer measures the output angle and the control circuit drives the motor until it matches the command. Explanation: Measure, compare, correct.
  4. Why sweep a sensor in steps with a pause, rather than in one jump? Answer: The servo needs time to reach each angle, and the sensor needs a settled position to measure the right direction. A pause of about 200 ms per step is typical. Explanation: Let it settle before measuring.
  5. Your servo buzzes and does not move. What is the most likely cause? Answer: It is stalled — the load is too heavy or it is driven against its stop. Remove the load before it burns out. Explanation: Buzzing means stalled.

✅ Check your understanding

Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.

Keep reading to complete this lesson