Home · Academy · Robotics & Coding · Arduino · Servo Control

Servo Control

Learn to turn a servo to a specific angle with the Servo library.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Ultrasonic Distance Sensor: We will connect and read a sensor that measures how far away an obstacle is using sound waves.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteButton Debounce Logic
ContentStandard lesson · 1,586 words
Last updated

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:

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:

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:

Wiring summary (for a small SG90):

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:

  1. Wire the servo: GND → GND, 5V → 5V, signal → pin 9.
  2. Add #include <Servo.h> and create a servo object.
  3. Write attach(9) inside setup.
  4. Inside loop, call write(0), write(90), and write(180) in order, with a delay(1000) between each.
  5. 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:

Lesson summary

Review questions

  1. Which line do we write to include the Servo library in the program?
  2. What does the command arm.write(90) do?
  3. In which part of the program does attach(9) usually go, and what is it for?
  4. About how many degrees does a small hobby servo turn?
  5. Why does powering a large servo from Arduino's 5V pin cause trouble, and what is the correct solution?

Answers

  1. We write #include <Servo.h> at the very top of the program.
  2. It moves the servo arm to 90 degrees, the exact middle of its range, and holds it there.
  3. It goes in the setup section and tells Arduino which pin the servo's signal wire is connected to. It must be written before write is called.
  4. It turns between about 0 and 180 degrees, that is, across a half-circle.
  5. 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.

Start QuizBack to Arduino
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.