Built-in project · Arduino Uno R3

Servo sweep on pin 10, on an Arduino Uno

A micro servo on pin 10 sweeps from 0 to 180 degrees and back, one degree at a time, with the angle on the serial monitor every ten degrees. 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

  • Servos work on any digital pin
  • Using the Servo library
  • Sweeping smoothly in one-degree steps
  • Reusing one function for both directions
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A hobby servo turns to the angle in its pulse and holds it. The Servo library makes that pulse in software, so it works on any digital pin, not just the ones marked with a tilde. Here the signal wire is on pin 10, with power on 5V and ground shared.

The sketch sweeps the horn from 0 to 180 degrees and back, one degree every 15 ms. It prints the angle on every tenth step, so the serial monitor stays readable.

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 10
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 on pin 10: the horn sweeps from one end to the other and back.
//
//   servo brown (GND)   -> GND
//   servo red (VCC)     -> 5V
//   servo orange (PWM)  -> pin 10
//
// The Servo library can drive a servo from any digital pin, not only the
// ones marked with a ~. It keeps its own timer and makes the 50 Hz pulses in
// software, so pin 10 works the same as pin 9.
//
// A servo turns to the angle in its pulse and holds it there. write() sets
// that angle; the sketch steps it one degree at a time so the horn moves
// smoothly, and prints every tenth step.

#include <Servo.h>

Servo horn;

void sweep(int from, int to) {
  int step = from < to ? 1 : -1;
  for (int angle = from; angle != to + step; angle += step) {
    horn.write(angle);
    if (angle % 10 == 0) {
      Serial.print("angle ");
      Serial.println(angle);
    }
    delay(15);
  }
}

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

void loop() {
  sweep(0, 180);
  sweep(180, 0);
}

Code walkthrough

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

sweep sketch.ino

Moves the horn from one angle to another. It picks a step of 1 or -1, depending on which end is bigger, then loops, calling horn.write(angle) and waiting 15 ms. Every time the angle divides by 10, it prints it.

void sweep(int from, int to) {
  int step = from < to ? 1 : -1;
  for (int angle = from; angle != to + step; angle += step) {
    horn.write(angle);
    if (angle % 10 == 0) {
      Serial.print("angle ");
      Serial.println(angle);
    }
    delay(15);
  }
}

setup sketch.ino

Attaches the horn servo to pin 10, starts serial at 9600 and prints a title.

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

loop sketch.ino

Calls sweep(0, 180) and then sweep(180, 0), so the horn goes up and back again forever.

void loop() {
  sweep(0, 180);
  sweep(180, 0);
}

Troubleshooting

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

  • Nothing moves This project
    The PWM wire must go to pin 10, because the code calls horn.attach(10).
  • The sweep is jerky This project
    Check the servo has 5V and a common ground with the Uno, and keep the 15 ms delay in sweep.
  • 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.