Skip to content
FlintMindsElementary

Move a Servo

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

About 25 min

or read the story first

0 of 5 steps done

What are we making?

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.

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.

Let's 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.

How it all connects

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.

The 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.

Try it!

  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.

Not working yet?

Nothing works the first time for everyone. Here's what to check.

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.

Try changing it

Sweep back and forth

Make the servo continuously sweep between two angles.

Point like a clock hand

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

Respond to a button press

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

Do you know how 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 should you make next?

You just learned how to:

  • Controlling a motor with code
  • Commanding precise angles
  • Using a code library
  • Distance Alarm

    Use an ultrasonic sensor to measure distance and trigger an alarm when something gets too close.