Built-in project · Arduino Uno R3

Servo sweep on an Arduino Uno

A micro servo on pin 9 jumps to 0, 90 and 180 degrees, then sweeps slowly back, with every angle on the serial monitor. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How pulse width sets a servo's angle
  • Using the Servo library
  • Why a servo needs time to move
  • Jumping versus sweeping
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A hobby servo turns to an angle and holds it. It reads the angle from the width of a pulse sent fifty times a second: about 0.5 ms for one end, about 2.4 ms for the other. The Servo library makes those pulses for you from pin 9.

The sketch jumps to 0, 90 and 180 degrees, waiting a second at each so the horn has time to get there. Then it sweeps back to 0 one degree at a time. Every angle on the jumps is printed to the serial monitor.

Parts list

3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Hobby servo pin GNDArduino Uno R3 pin GND
Hobby servo pin PWMArduino Uno R3 pin 9
Hobby servo pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Servo sweep: the horn swings to 0, 90 and 180 degrees, then sweeps back.
//
//   servo brown (GND)   -> GND
//   servo red (VCC)     -> 5V
//   servo orange (PWM)  -> pin 9
//
// A hobby servo turns to an angle and holds it. It reads the angle from the
// width of a pulse sent fifty times a second: about 0.5 ms is one end, about
// 2.4 ms the other. The Servo library sends that pulse for you; write(angle)
// only changes how wide it is.
//
// The horn needs time to get there. This one moves about 60 degrees in a
// tenth of a second, so each jump waits a second before the next.

#include <Servo.h>

Servo horn;

void moveTo(int angle) {
  horn.write(angle);
  Serial.print("angle ");
  Serial.println(angle);
}

void setup() {
  horn.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: servo sweep");
}

void loop() {
  moveTo(0);
  delay(1000);
  moveTo(90);
  delay(1000);
  moveTo(180);
  delay(1000);
  // A slow sweep back, one degree at a time.
  for (int angle = 180; angle >= 0; angle--) {
    horn.write(angle);
    delay(10);
  }
  Serial.println("swept back to 0");
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

moveTo sketch.ino

A small helper. It tells the servo to go to an angle with horn.write(angle) and prints the angle to the serial monitor. It does not wait, so the caller adds the delay.

void moveTo(int angle) {
  horn.write(angle);
  Serial.print("angle ");
  Serial.println(angle);
}

setup sketch.ino

Connects the horn servo to pin 9 with attach, then starts serial at 9600 and prints a title.

void setup() {
  horn.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: servo sweep");
}

loop sketch.ino

Calls moveTo for 0, 90 and 180, with a one second delay after each. Then a loop steps the angle from 180 down to 0, one degree every 10 ms, for a slow sweep. It prints "swept back to 0" and waits a second.

void loop() {
  moveTo(0);
  delay(1000);
  moveTo(90);
  delay(1000);
  moveTo(180);
  delay(1000);
  // A slow sweep back, one degree at a time.
  for (int angle = 180; angle >= 0; angle--) {
    horn.write(angle);
    delay(10);
  }
  Serial.println("swept back to 0");
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The servo jitters or the board resets This project
    Make sure the servo has VCC on 5V and a shared GND with the Uno.
  • The servo does not move This project
    The PWM wire must go to pin 9, because the code attaches the servo there.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.
  • The servo hums at the end of its travel or will not reach full angle. Hobby servo
    Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.