Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Sensors and Actuators

Servo Motors

Control hobby servos with Arduino: how pulse width sets the angle, the Servo library, smooth sweeps without delay, power requirements, and common problems.

A hobby servo is a small motor with a gearbox, a position sensor and a control circuit in one case. You do not tell it how fast to spin; you tell it where to go, and it moves its output shaft to that angle and holds it there. That makes servos perfect for robot arms, steering, pan-and-tilt camera mounts, door latches and gauges.

The real board and servo cannot run here. The sketches are read-only Arduino code. The runnable C programs are labelled stand-ins that model the pulse math and a smooth movement.

How a servo is controlled

A standard servo has three wires: ground (brown or black), power (red, usually 4.8 to 6 V) and signal (orange or yellow). The signal is a repeating pulse, one every 20 milliseconds (50 Hz). The width of that pulse sets the target angle:

Pulse width Typical position
about 1000 us one end (0 degrees)
about 1500 us center (90 degrees)
about 2000 us other end (180 degrees)

Different servos want slightly different ranges, which is why a servo sometimes buzzes at the ends of its travel: it is being asked to go past what it can physically do.

The Servo library

The Servo library ships with the IDE. It generates the pulses with hardware timers, so it keeps working in the background while your sketch does other things.

C++
#include <Servo.h>

Servo gate;

void setup() {
  gate.attach(9);        // signal wire on pin 9
}

void loop() {
  gate.write(0);         // go to 0 degrees
  delay(1000);
  gate.write(90);
  delay(1000);
  gate.write(180);
  delay(1000);
}

attach(pin) assumes pulses from 544 to 2400 microseconds. If your servo covers a different range, pass your own limits: gate.attach(9, 1000, 2000). write(angle) takes degrees; writeMicroseconds(us) sets the pulse width directly, giving finer control than whole degrees.

Two details worth knowing:

  • On many AVR boards (such as the Uno) the library uses Timer1 and therefore disables analogWrite() on pins 9 and 10 while it is in use.
  • write() returns immediately. The servo needs time (typically 100 to 300 ms for a large move) to arrive, so wait before assuming it is there.
C Stand-in: angle to pulse width (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Power: the usual surprise

A servo that works with nothing attached may reset your Arduino as soon as it moves under load. A small micro servo can draw several hundred milliamps; a standard servo under load can pull an ampere. The Uno's 5 V pin, fed from USB, cannot supply that.

Rules that save hours of debugging:

  • Power servos from a separate supply (4 x AA cells, a 5 V regulated supply or a BEC), not from the board's 5 V pin when you have more than one small servo or any load.
  • Join the grounds of the supply and the Arduino, or the signal has no reference and the servo jitters.
  • Add a capacitor of 100 to 470 microfarads across the servo supply to absorb current spikes.
  • Do not drive a servo against a hard stop; it stalls and draws heavy current.

Smooth movement without delay()

Jumping straight to an angle is jerky. A better approach moves one degree at a time on a schedule, using millis() so the sketch stays responsive:

C++
#include <Servo.h>

Servo arm;
int current = 0;
int target = 150;
unsigned long lastStep = 0;
const unsigned long STEP_MS = 15;

void setup() {
  arm.attach(9);
  arm.write(current);
}

void loop() {
  unsigned long now = millis();
  if (now - lastStep >= STEP_MS && current != target) {
    lastStep = now;
    current += (target > current) ? 1 : -1;
    arm.write(current);
  }
  // other work (reading buttons, sensors...) happens here without being blocked
}

The expression now - lastStep stays correct even when millis() wraps around after about 49.7 days, because unsigned subtraction wraps too (more in debugging sketches).

C Stand-in: stepping toward a target on a timer (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Continuous-rotation and other servo types

A continuous-rotation servo has no position feedback limit: the pulse sets speed and direction instead of angle (about 1500 us stops, less turns one way, more the other). write(90) means stop on many of them; trim the small screw on the case if it creeps. Digital and metal-gear servos are stronger and more precise but draw more current. For a 6 or more servo robot arm, use a servo driver board (PCA9685 over I2C) rather than burning Arduino pins.

Common problems

  • Jitter or twitching: weak power, no common ground, or timer conflicts with other libraries.
  • Buzzing at the ends: you ask for an angle outside its range; narrow the limits.
  • Arduino resets when the servo moves: the supply is too weak.
  • Wrong angle: servos differ; calibrate with writeMicroseconds().

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the pulse width on a standard servo's signal wire set?

    A standard servo reads a pulse every 20 ms; about 1000 us to 2000 us maps to one end through the other end of its travel.
  2. 2. Why should a servo with a real load use its own power supply?

    Servos draw hundreds of milliamps to an ampere under load, which can brown out the board. Use a separate supply and join the grounds.
  3. 3. What is true about Servo.write()?

    write() sets the target and returns. The servo physically takes time, so plan delays or timed steps.
  4. 4. What does a continuous-rotation servo do with the pulse width?

    Without a position stop, the pulse controls rotation speed and direction; about 1500 us means stop.

Summary

  • A hobby servo moves to an angle set by a 1 to 2 ms pulse repeated every 20 ms.
  • The Servo library generates the pulses; write() takes degrees, writeMicroseconds() takes pulse width.
  • Give servos a separate supply with common ground, and use a capacitor for current spikes.
  • Move gradually with millis() timing to keep the rest of the sketch responsive.

Next: DC Motors and the H-Bridge