Servo sweep on pin 3, on an Arduino Uno
A servo on pin 3 sweeps from 0 to 180 degrees one degree at a time and back again, with the Servo library. 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.
What you'll learn
- Driving a servo with the Servo library
- How a servo turns to an angle with write
- Sweeping a value with a for loop
- Powering a servo from 5V and sharing ground
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A hobby servo turns to the angle set by the pulses it receives. The Servo library makes those pulses on pin 3. The servo is powered from 5V and shares ground with the Uno.
The loop sweeps from 0 to 180 degrees, one degree every 15 milliseconds, then sweeps back down to 0 the same way. The pause gives the horn time to reach each angle.
Parts list
3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- SunFounder Universal Maker Sensor Kit
- Adafruit MetroX Classic Kit
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 pin | Connects to |
|---|---|
| Hobby servo pin GND | Arduino Uno R3 pin GND |
| Hobby servo pin PWM | Arduino Uno R3 pin 3 |
| Hobby servo pin VCC | Arduino 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.
// A servo sweep on pin 3.
//
// servo signal (orange) -> pin 3 red -> 5V brown -> GND
//
// A servo turns to the angle in its pulse: Servo.write(angle) sends it. One
// degree every 15 ms gives the horn time to get there.
#include <Servo.h>
Servo horn;
void setup() {
horn.attach(3);
Serial.begin(9600);
Serial.println("Mokxi Uno: servo sweep on pin 3");
}
void loop() {
for (int pos = 0; pos <= 180; pos++) {
horn.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos--) {
horn.write(pos);
delay(15);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. horn.attach(3) connects the Servo object horn to pin 3 so it can send pulses there. Serial.begin(9600) opens serial output and prints a title line.
void setup() {
horn.attach(3);
Serial.begin(9600);
Serial.println("Mokxi Uno: servo sweep on pin 3");
}loop sketch.ino
Two for loops do the work. The first counts pos from 0 up to 180, the second from 180 back to 0. Each step calls horn.write(pos) and then delay(15). A smaller delay would sweep faster.
void loop() {
for (int pos = 0; pos <= 180; pos++) {
horn.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos--) {
horn.write(pos);
delay(15);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The servo twitches or does nothing This project
Check that the signal wire goes to pin 3, red to 5V and brown to GND. - 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.