Bench NotesUno handbook

Servo Basics

How hobby servos are controlled, their wire colours, and a smooth sweep with the Servo library.

BeginnerServo

Overview

A hobby servo contains a motor, a gearbox and a position sensor. It holds the angle set by a pulse repeated every 20 ms: about 1 ms means one end, 1.5 ms the middle and 2 ms the other end. The Servo library generates these pulses for you; you just call write(angle).

You will need

  • Arduino Uno R3
  • Micro servo such as SG90
  • Jumper wires

Wiring

Uno pinConnects toNotes
9Signal (orange, yellow or white)
5VV+ (red)Small servo, no load only
GNDGND (brown or black)
Wiring for Servo BasicsArduino Uno D9 to Servo signal; Arduino Uno 5V to Servo V+ (red); Arduino Uno GND to Servo GND.Connections (each line is one wire)Arduino UnoServoD9signal5VV+ (red)GNDGND
Original wiring diagram. Each line is one jumper wire; boxes on a line are series resistors.

Diagram

Sweep end points and centreSemicircle from 0 to 180 degrees showing servo arm positions in order: 0 degrees, 90 degrees, 180 degrees. 0°90°180°123Order1. 0°2. 90°3. 180°
write(0), write(90), write(180).

The sketch

servo_sweep.ino
#include <Servo.h>

Servo arm;
const int SERVO_PIN = 9;

void setup() {
  arm.attach(SERVO_PIN);
}

void loop() {
  for (int angle = 0; angle <= 180; angle++) {
    arm.write(angle);
    delay(15);
  }
  for (int angle = 180; angle >= 0; angle--) {
    arm.write(angle);
    delay(15);
  }
}

How the code works

Moving 1° every 15 ms gives a sweep of about 2.7 seconds. The library's default pulse range (544–2400 µs) can push some servos against their internal stops at 0° or 180°; if yours buzzes at the ends, limit the range, for example arm.attach(SERVO_PIN, 1000, 2000). Use arm.writeMicroseconds() for finer control.

On the Uno, attaching any servo disables analogWrite() on pins 9 and 10.

What you should observe

  • The horn sweeps slowly from one end to the other and back.
  • If the board resets or USB disconnects mid-sweep, the servo is drawing too much current: see the next article.