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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | servo motor | PWM to a pin, V+ to 5V, GND to GND. The Servo library turns write(angle) into a pulse width. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 9 | servo motor PWM |
| 2 | Arduino Uno 5V | servo motor V+ |
| 3 | Arduino Uno GND | servo motor GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a servo motor from the parts palette.
- Wire 1. Connect Arduino Uno pin 9 to servo motor PWM.
- Wire 2. Connect Arduino Uno 5V to servo motor V+.
- Wire 3. Connect Arduino Uno GND to servo motor GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator