One-sentence summary
A servo motor is a motor we can turn to any angle we want with Arduino's Servo library, telling the arm exactly where to point.
Why does it matter?
So far we have lit LEDs, read buttons, and cleaned up button bounce (debounce). All of that was about *reading* information or *switching* a light on and off. But real robots also move: an arm lifts, a gate opens, a wheel changes direction.
A servo motor is the easiest way to control movement. An ordinary motor just says "spin"; you don't know how far it has turned. A servo is different: you tell it "go to 90 degrees," and it moves exactly there and stops. In other words, you control the angle directly.
That is why servos show up so often in robotics projects:
- Setting the elbow of a robot arm to a specific angle.
- Opening and closing a barrier (like a parking gate).
- Turning a camera or sensor left and right to scan the surroundings.
Short definition: A servo is a motor that moves its arm to a specific position based on the angle command you send, and holds it there.
How does a servo motor work?
A motor that speaks in angles
Small hobby servos (for example the SG90) have three wires:
- Brown (or black): GND, the ground.
- Red: Power (5V).
- Orange (or yellow): Signal. Arduino sends the angle command through this wire.
Most small servos turn between about 0 and 180 degrees. So you can move the arm to any point along a half-circle: 0 degrees is one end, 180 degrees is the other end, and 90 degrees is the middle.
Arduino sends very fast pulses (signals) through the signal wire. A tiny circuit inside the servo reads these pulses and turns the arm to the correct angle. The good news: we do not have to calculate those pulses ourselves. The Servo library handles all the hard work for us.
The Servo library: attach and write
In Arduino, ready-made packages of code are called libraries. The Servo library comes with the Arduino software from the start, so it needs no extra installation. To use it, you only need to learn three things:
#include <Servo.h> // Include the Servo library in the program
Servo arm; // Create a servo object named "arm"
void setup() {
arm.attach(9); // The servo signal wire is on pin 9
arm.write(90); // Move the arm to 90 degrees (the middle)
}
void loop() {
// Nothing repeats in this example
}
Three key commands:
Servo arm;— We give the servo a name. You could call itgateorcamerainstead.arm.attach(9);— We tell it which pin the servo is connected to. This usually goes insetup, once.arm.write(90);— You send the arm to the angle you want. The number must be between 0 and 180.
Wiring summary (for a small SG90):
- Servo GND → Arduino GND
- Servo 5V → Arduino 5V
- Servo signal → Arduino pin 9
Let's move the servo
Example: going back and forth between two angles
Let's move the arm to 0 degrees, then to 180 degrees, with a pause each time. This is like a barrier opening and closing.
#include <Servo.h>
Servo arm;
void setup() {
arm.attach(9);
}
void loop() {
arm.write(0); // Send the arm to one end
delay(1000); // Wait 1 second
arm.write(180); // Send the arm to the other end
delay(1000); // Wait 1 second
}
The delay(1000) matters here. Turning takes time; if you send a new command right away, the arm turns back before it reaches the target. A short wait gives the arm time to get there.
Example: sweeping slowly
Let's move the arm from 0 to 180 gradually instead of in one jump. This motion is like turning a sensor left and right to scan an area.
#include <Servo.h>
Servo arm;
int angle = 0;
void setup() {
arm.attach(9);
}
void loop() {
for (angle = 0; angle <= 180; angle++) {
arm.write(angle); // Increase the angle by one degree
delay(15); // Short wait after each step
}
for (angle = 180; angle >= 0; angle--) {
arm.write(angle); // Decrease the angle by one degree
delay(15);
}
}
The first loop increases the angle from 0 to 180 one step at a time, and the second loop brings it back. delay(15) slows each step. A larger number makes the motion slower; a smaller number makes it faster.
Example: open a gate when a button is pressed
Remember the button from the last lesson. Let's move the servo to an open position when the button is pressed and a closed position when it is released. Like a simple parking barrier.
#include <Servo.h>
Servo gate;
const int button = 2;
void setup() {
gate.attach(9);
pinMode(button, INPUT_PULLUP); // Enable the built-in resistor
}
void loop() {
if (digitalRead(button) == LOW) {
gate.write(90); // Pressed: open
} else {
gate.write(0); // Not pressed: closed
}
}
Because we use INPUT_PULLUP, the pin reads LOW while the button is pressed. Pressing turns the arm to 90 degrees; releasing turns it back to 0.
Mini practice
Build your own "camera scanner." Goal: move the servo arm to three fixed positions — left (0°), middle (90°), and right (180°) — and wait one second at each.
Steps:
- Wire the servo: GND → GND, 5V → 5V, signal → pin 9.
- Add
#include <Servo.h>and create a servo object. - Write
attach(9)insidesetup. - Inside
loop, callwrite(0),write(90), andwrite(180)in order, with adelay(1000)between each. - Upload the code and watch whether the arm stops at all three points.
Challenge: Add Serial.begin(9600) and Serial.println() so each move prints its name (for example "Left", "Middle", "Right") to the Serial Monitor. That way you can see which line the code is on with your own eyes.
Common mistakes
Writing an angle above 180 or below 0
A value like write(200) does not work on most servos. Keep the angle between 0 and 180. If you are unsure, start with small values.
Forgetting the attach command
If you call write() without writing attach(9), the servo does not move, because Arduino does not know which pin the arm is on. attach must always come inside setup, before write.
Skipping the delay
If you send commands to the servo too quickly, one after another, the arm may jitter or never reach the target. Give the arm a little time to get into place after each move.
Straining the servo without separate power
If you power a large servo, or several servos, straight from Arduino's 5V pin, Arduino draws too much current and may reset. Large servos need a separate power source (next section).
Safety note
A servo is a moving part. Be careful not to catch a finger, your hair, or a wire while the arm is turning. Do these experiments together with an adult.
Be careful with power:
- Small servo (like the SG90): The 5V pin of a USB-powered Arduino is usually enough for a single small servo.
- Large servo or several servos: Do not power these from an Arduino pin. Use a separate power source (for example a battery pack), and connect that source's GND to Arduino's GND so they share a common ground and the signal is understood correctly. Set this up together with an adult.
- If the servo gets hot, behaves strangely, or the arm gets stuck and strains, cut the power right away. Forcing a motor to turn against a jam can damage both the servo and the Arduino.
- Never use mains (wall socket) electricity for motors. Work only with USB or suitable battery packs.
Lesson summary
- A servo motor moves to any angle you want (0–180°) with the
write(angle)command and holds there. - The
Servolibrary is added with#include <Servo.h>and does the hard pulse calculation for us. - Three basic steps: create the object,
attach(pin)insidesetup, thenwrite(angle). - Turning takes time, so you need a
delaybetween moves. - A small servo can run from USB; large or multiple servos need a separate power source with a shared ground.
Review questions
- Which line do we write to include the
Servolibrary in the program? - What does the command
arm.write(90)do? - In which part of the program does
attach(9)usually go, and what is it for? - About how many degrees does a small hobby servo turn?
- Why does powering a large servo from Arduino's 5V pin cause trouble, and what is the correct solution?
Answers
- We write
#include <Servo.h>at the very top of the program. - It moves the servo arm to 90 degrees, the exact middle of its range, and holds it there.
- It goes in the
setupsection and tells Arduino which pin the servo's signal wire is connected to. It must be written beforewriteis called. - It turns between about 0 and 180 degrees, that is, across a half-circle.
- A large servo draws a lot of current; Arduino cannot supply it and may reset or be damaged. The correct solution is to use a separate power source and connect that source's GND to Arduino's GND for a common ground.
Source and verification note
For “Servo Control”, verification focuses on whether the relationship between How does a servo motor work? and The Servo library: attach and write remains consistent across examples. Pin, voltage and current limits can differ between Arduino-compatible boards. Compiling code does not guarantee a safe circuit; loads such as motors and servos require a suitable driver and external power where appropriate.
Next lesson
Ultrasonic Distance Sensor: We will connect and read a sensor that measures how far away an obstacle is using sound waves.