A servo motor is the easiest way to make something move with an Arduino. You tell it an angle — it goes there. No fancy wiring, no motor driver, no math. This tutorial walks you through wiring up an SG90 servo (the tiny blue one in every starter kit), writing the 12 lines of code that make it sweep back and forth, and the three mistakes that cause 90% of "my servo isn't working" frustration.
What you'll build
A servo that smoothly sweeps from 0° to 180° and back, like a tiny windshield wiper. Once this works, you can turn it into a robot arm joint, a camera mount, a treat dispenser for your cat, or any of a hundred other projects.
What is a servo motor (in one paragraph)
Most motors just spin. A servo motor is a small geared motor with a built-in position sensor and a tiny circuit board, all crammed into one package. When you tell it "go to 90°", it turns and staysthere, holding against pressure. That makes it perfect for things that need to point — robot arms, RC car steering, door latches, animatronic eyes.
The cheap blue servo in every Arduino starter kit is called the SG90. It rotates from 0° to 180°, runs on 5V, and costs about $2. It is plastic-geared and not very strong, but it is more than enough to learn on.
Parts you need
- 1 × Arduino Uno (or any clone — Nano works fine too)
- 1 × SG90 servo motor
- 3 × jumper wires (male-to-male is fine for the SG90)
- USB cable
That is it. No breadboard, no resistor, no external power. The SG90 plugs straight into the Arduino.
Step 1: Wire it up (3 wires, 2 minutes)
Servo motors have three wires. They are always the same colours and they always do the same thing:
| Servo wire | What it does | Plug into Arduino |
|---|---|---|
| Brown (or black) | Ground (–) | GND |
| Red | Power (+5V) | 5V |
| Orange (or yellow) | Signal | Pin 9 |
Pin 9 is not magic — any digital pin that supports PWM works (3, 5, 6, 9, 10, 11 on the Uno). Pin 9 is the convention in tutorials.
Step 2: Upload this code
Open the Arduino IDE, paste this, and click Upload. That's it.
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // signal wire is on pin 9
}
void loop() {
// sweep from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(15);
}
// sweep back from 180 to 0
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
delay(15);
}
}12 lines. The Servo.h library is built into the Arduino IDE — no installation needed. Thedelay(15) controls speed; smaller numbers = faster sweep, larger numbers = slower. Try changing it to 5 and 50 to see the difference.
Step 3: Understand what just happened
Behind the scenes, Arduino is sending a PWM signal — a pulse of voltage that repeats every 20 milliseconds. The width of each pulse tells the servo what angle to hold:
- 1ms pulse → servo goes to 0°
- 1.5ms pulse → servo goes to 90°
- 2ms pulse → servo goes to 180°
You will never have to think about pulse widths again — the Servo.h library does the math. But knowing this is why your servo only goes 180° and not 360° (the pulse-width range maps to a half rotation), and why "continuous rotation" servos exist (different internals, same library).
Bonus: Control the servo with a knob
Once the sweep works, the next step every maker takes is wiring a potentiometer (knob) to control the angle in real time. It is a 2-component upgrade and feels magical the first time it works:
#include <Servo.h>
Servo myServo;
const int POT_PIN = A0;
void setup() {
myServo.attach(9);
}
void loop() {
int raw = analogRead(POT_PIN); // 0–1023
int angle = map(raw, 0, 1023, 0, 180); // remap to 0–180
myServo.write(angle);
delay(15);
}Wire the potentiometer's outer pins to 5V and GND, and the middle pin to A0. Turn the knob — the servo follows. You just built a one-axis robot arm controller. Add three more servos and you have a real robot arm.
Troubleshooting: 3 mistakes that cause 90% of problems
1. The servo jitters or resets the Arduino
You are powering the servo straight from the Arduino's 5V pin. That is fine for one small SG90, but two or more servos draw enough current to brown out the Arduino. The fix: power the servos from a separate 5V battery pack or a 5V adapter, and connect the grounds of the Arduino and the battery together. Common ground is non-negotiable.
2. The servo buzzes and gets hot
You are commanding it past its physical end-stop (less than 0 or more than 180). The motor keeps trying to push, draws current, and overheats. Clamp the angle in software withconstrain(angle, 0, 180) before calling myServo.write().
3. The servo does nothing
The signal wire is in the wrong pin. The Arduino IDE will happily compile myServo.attach(9)even if you have the orange wire in pin 8 or pin 10. Triple-check the pin number in code matches the physical pin the orange wire is in.
What to build next
Once you have one servo working, the world opens up. Here is the natural progression most makers take:
- Two-axis camera mount — pan and tilt, controlled by two knobs
- Robot arm — 4 servos: base, shoulder, elbow, gripper. Joystick or app control.
- Animatronic eyes — 2 servos, an Arduino, a 3D-printed mask. Halloween hit.
- Cat feeder — 1 servo opens a flap at a set time using
millis() - RC car steering — replace the steering mechanism with a servo + radio receiver
Want a live simulator for this?
CircuitPath has a built-in Arduino simulator. You can run this servo sweep in the browser before touching real hardware.
Disclosure: Amazon affiliate links — we may earn a small commission at no extra cost to you.