Skip to content
FlintMindsMiddle School
Middle 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

0 of 5 build milestones complete

What are we building?

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.

The mechanism

How it works

  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.

What you'll pick up

  • Understand how a servo motor is different from a spinning motor
  • Learn how servo angles work, from 0 to 180 degrees
  • Use a motor-control library in your code
  • Sequence multiple movements in order

What you need

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 it

  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.

Does it work?

  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.

Make it yours

What can you change?

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.

If something's off

The usual causes, in the order they're worth checking.

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.

Do you know why it works?

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?

What to build next

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.