TL;DR:
A hobby servo has three wires: power, ground and one signal wire that carries a pulse whose width sets the angle.
Give the servo a supply that can handle its current, and join its ground to the board's ground.
A sweep from 0° to 180° is a loop that writes one angle at a time.
Servos are the joints of beginner robots: arms, grippers, pan-tilt heads. They are also where many first builds fail, not because of the code, but because the servo shares a weak power supply with the board and the board keeps resetting.
What you will build
You will connect one hobby servo to a microcontroller board and make its horn sweep from 0° to 180° and back, smoothly, in about three seconds each way.
Along the way you will meet three ideas that every robot arm, gripper and camera mount depends on: a control signal that is a pulse width, a power supply that must cope with sudden current, and angle limits that protect the mechanism.
Parts and tools
A microcontroller board (an Arduino-style board is used here) and a USB cable.
One small hobby servo.
A separate battery pack or regulated supply that matches your servo's rated voltage, found on its label or data sheet.
Jumper wires and a breadboard.
A small screwdriver for the horn screw.
Wiring: power, ground and signal
The three servo wires are usually brown or black for ground, red for power and orange or yellow or white for signal. Check your servo's data sheet, because colors vary.
Signal: Connect the signal wire to a PWM-capable pin on the board, for example pin 9.
Power: Connect the red wire to the servo supply, not to the board's regulator, when the servo is larger than a tiny one. A moving servo draws far more current than an idle one, and a stalled one draws the most.
Common ground: Connect the servo supply's ground and the board's ground together. Without this shared reference the signal has no meaning to the servo.
Free the horn: Before the first run, make sure nothing blocks the horn's travel.
How the signal sets the angle
A common hobby servo expects a pulse every 20 ms, which is 1 / 0.020 s = 50 Hz. The pulse width sets the angle. A typical mapping is 1.0 ms for one end of travel, 1.5 ms for the middle, 2.0 ms for the other end. Over 180° that is 1000 µs / 180° ≈ 5.6 µs per degree, so 90° is 1000 + 90 × 5.56 = 1500 µs, matching the middle. Real servos differ in range, so your servo may stop short of 180° or try to go beyond it.
The code, step by step
The Servo library creates the 50 Hz pulses for you. You only call write with an angle in degrees.
#include <Servo.h>
Servo s;
void setup() {
s.attach(9); // signal pin
}
void loop() {
for (int a = 0; a <= 180; a++) { // sweep up
s.write(a);
delay(15);
}
for (int a = 180; a >= 0; a--) { // sweep back
s.write(a);
delay(15);
}
}Check the timing: 181 steps of 15 ms each is about 180 × 0.015 s = 2.7 s per direction, close to the three seconds promised. Change the delay to change the speed.
Test, measure and fix
Nothing moves: check the ground join and the signal pin number first.
Board resets or the servo twitches: a brownout. The supply sagged when the servo drew current. Use a separate supply and add a capacitor across the servo's power if your kit suggests one.
The servo buzzes at the ends: it is pushing against its limit. Narrow the range, for example 10° to 170°.
It sweeps jerkily: the supply is weak or the delay is too short.
To measure the real range, write 0, then 90, then 180 and mark the horn position each time on a sheet of paper under it. Use the angles that the horn really reaches as your limits.
Work in small steps. First write one fixed angle such as 90 and confirm the horn holds still. Then sweep a narrow range, say 60 to 120. Only then use the full range. If something is wrong, the small tests show whether the fault is wiring, power or code.
Make it better
Replace the loop with a function that moves toward a target angle by one degree per step. Then two servos can move together without the delay freezing everything else. You can read a knob with the analog input and map its value to an angle so the servo follows your hand.
Think about what happens at power-up. A servo may jump to its last commanded position or swing to a default angle, so mount the arm or gripper only after you know where the servo starts, and keep the first moves slow.
Stay safe
If you ever see smoke or smell something hot, disconnect the supply at once and let everything cool before you look for the fault.
Fingers, wires and batteries: Servos can pinch skin and trap hair or loose wires. Keep clear of the horn when power is on. Use battery packs designed for your servo, never mains power, and switch off before changing wiring. A stalled servo gets warm, so do not hold the horn to stop it.
Frequently asked questions
Can I power the servo from the board's 5 V pin?
A tiny servo with no load may work, but larger ones can pull more current than the board can give, causing resets. A separate supply with a shared ground is safer.
Why does my servo not reach exactly 180°?
The pulse range differs between models. Measure your servo's real range and set limits to match.
