Skip to content
FlintMindsHigh School
High School

Elementary

Move a Servo

Control a small motor with code and turn it to exact angles, just like a robot arm.

Level
Explorer
Time
25 min
Parts
3
Hardware-checked
Not yet

What this build assumes

  • Building a basic circuit
  • Controlling a digital output
  • Using timing and loops in code

0 of 5 build milestones complete

What this build is

A servo motor turns to a precise angle instead of spinning forever, which makes it perfect for arms, gates, and steering.

In this project you'll connect a servo to your board and write code that points it wherever you want.

How it works

System architecture

  1. Program

    Your code calls write() with an angle.

  2. Signal

    A PWM signal tells the servo which angle to hold.

  3. Servo

    The motor physically rotates to that angle.

Parts

Microcontroller board × 1

Runs the code that tells the servo which angle to move to.

Micro servo motor × 1

A small motor that rotates to a precise angle instead of spinning continuously.

Jumper wires × a few

Connect the servo's power, ground, and signal pins to the board.

Build

  1. 1

    Attach the servo horn

    Snap or screw the small plastic arm (the "horn") onto the servo's spinning shaft.

    Servo horn attachment diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  2. 2

    Connect power and ground

    The servo's power and ground wires connect to your board's 5V and GND pins.

    Servo power wiring diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  3. 3

    Connect the signal wire

    Run the servo's signal wire to a digital pin on your board that supports PWM.

    Servo signal wiring diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  4. 4

    Upload the sweep code

    Connect your board to your computer and upload the code below.

Wiring

Move a Servo wiring diagram

Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

Servos have three wires: power, ground, and signal. Power and ground connect to your board's 5V and GND; the signal wire connects to a PWM-capable digital pin — match the pin in your code below.

Code

arduino
1#include <Servo.h>2 3Servo myServo;4const int servoPin = 9; // update to match the pin you wired5 6void setup() {7  myServo.attach(servoPin);8}9 10void loop() {11  myServo.write(0);12  delay(1000);13  myServo.write(180);14  delay(1000);15}16 
  • Line 3: myServo is a Servo object from the library — it's what you'll command to move.
  • Line 11: write() tells the servo which angle, from 0 to 180, to rotate to.

Expected behaviour

  1. 1Upload the code and watch the servo horn.
  2. 2It should rotate to one side, pause, then rotate to the other side, repeating.
  3. 3Try changing the angle values to see the servo stop at different positions.

Diagnosis

Symptoms and their usual causes. Work down the list — each check rules out a subsystem.

The servo doesn't move.
Confirm servoPin matches the pin you wired the signal wire to.
The servo jitters or moves erratically.
Servos can draw more current than a board's pin can supply — try powering it from a separate 5V source if it keeps jittering.
The servo moves the wrong direction.
This is normal — some servo horns are mounted differently. Adjust the angle values to match what you want to see.

Open problems

Extensions

Small change

Sweep back and forth

Make the servo continuously sweep between two angles.

Add something

Point like a clock hand

Move the servo to five different angles in sequence, pausing at each.

Design it yourself

Respond to a button press

Add a push button that moves the servo to a new angle each time it's pressed.

You decide how it works. No steps for this one.

Check your understanding

1. What makes a servo motor different from a regular spinning motor?

2. What is the typical range of motion for the servo in this project?

3. Why would a robot arm use a servo instead of a simple motor?

Where this leads

You now know:

  • Controlling a motor with code
  • Commanding precise angles
  • Using a code library
  • What this leads to

    Distance Alarm

    Move a Servo leads straight into this one.

The engineering brief for this project has not been written yet.

The problem statement, the constraints, the design decisions and the validation procedure are the parts of a project that make it worth an engineering student’s time, and this one does not have them yet. Writing them means building it with real components and measuring what it does — so rather than approximate, we are telling you they are missing. Why we do this.