Arduino Servo Motor Sweep: Wiring, Code and Simulator

To build this Arduino Servo Motor Sweep project you need an Arduino Uno and a servo motor. The Servo library on pin 9 sweeping a hobby servo from 0° to 180° and back. Make the 3 connections in the wiring table, upload the Arduino C++ code, and watch the horn sweep. Each write(angle) becomes a pulse between 544 and 2400 µs.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1servo motorPWM to a pin, V+ to 5V, GND to GND. The Servo library turns write(angle) into a pulse width.

Wiring

#FromTo
1Arduino Uno pin 9servo motor PWM
2Arduino Uno 5Vservo motor V+
3Arduino Uno GNDservo motor GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a servo motor from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 9 to servo motor PWM.
  4. Wire 2. Connect Arduino Uno 5V to servo motor V+.
  5. Wire 3. Connect Arduino Uno GND to servo motor GND.
  6. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  7. Run it. Press Run. Watch the horn sweep. Each write(angle) becomes a pulse between 544 and 2400 µs.

Arduino C++ code

// Servo sweep: turn a hobby servo from 0 to 180 degrees and back.
// The Servo library turns write(angle) into a 544-2400 microsecond pulse.
#include <Servo.h>

Servo arm;

void setup() {
  arm.attach(9);
  Serial.begin(9600);
}

void loop() {
  for (int a = 0; a <= 180; a += 10) { arm.write(a); delay(60); }
  Serial.println("swept to 180");
  for (int a = 180; a >= 0; a -= 10) { arm.write(a); delay(60); }
  Serial.println("back to 0");
}

What to try

Watch the horn sweep. Each write(angle) becomes a pulse between 544 and 2400 µs.

Frequently asked questions

How do I wire the Servo Motor Sweep project on an Arduino Uno?

Make 3 connections: Arduino Uno pin 9 to servo motor PWM; Arduino Uno 5V to servo motor V+; Arduino Uno GND to servo motor GND.

What parts do I need for this Arduino project?

An Arduino Uno and a servo motor.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

The Servo library on pin 9 sweeping a hobby servo from 0° to 180° and back.

More Arduino Uno projects