Skip to content

Servo Motors

Overview

Use the Servo library to move a positional servo through a sequence of angles.

Builds on

Before You Start

Test this servo separately from the motor-driver circuit. Confirm the servo model, usable angle range, and suitable supply with Mr. Willis.

Materials

  • Arduino Uno R3, USB cable, and Arduino IDE with Servo library
  • Positional servo, male-to-male jumper wires, and an approved servo supply

Safety

Disconnect power before rewiring

Unplug Arduino USB and disconnect or switch off every other supply before changing wires. Check polarity and connections before restoring power.

You've got it when…

  • The servo moves through the requested positions without binding.
  • Your notebook contains the actual wiring/supply table and complete final code.

Collaboration & AI

Compare observations and troubleshoot with classmates. Each student must understand the wiring or design and prepare their own submission.

AI — AIAS Level 1, No AI: Complete the code or design yourself without AI assistance. AI Assessment Scale


Servo Motor Overview

A positional servo motor moves to an angle and holds it, rather than spinning continuously like a DC gearmotor. Many classroom servos cover roughly 0° to 180°, but the usable range depends on the model.

Inside are a DC motor, gears, a position sensor, and a control circuit. This feedback system checks the shaft position and adjusts the motor to reach the requested angle. The Arduino sends timed pulses along the signal wire; pulse width specifies the position. This is different from controlling motor speed with analogWrite().

Wiring

  1. Disconnect all power and wire the servo using male-to-male jumper wires and the table below. Use the Arduino Pinout to locate pin 3 and GND.

    Servo wire Connection
    Orange (signal) Arduino pin 3
    Red (power) Teacher-approved servo supply within the servo’s voltage rating
    Brown (ground) Supply ground, shared with Arduino GND

    Choose the servo supply with Mr. Willis

    Use an external supply with voltage and current capacity suitable for the actual servo. Connect its ground to Arduino GND, but do not connect its positive output to the Arduino 5V pin while USB is connected.

    A small, unloaded servo may be tested from Arduino 5V only with teacher approval. Servo startup or stall current can exceed what the board can supply. Do not connect a servo directly to an unspecified battery pack: excess voltage can damage it. A suitable separate supply avoids overloading the Arduino; it does not justify exceeding the servo's rated voltage.

Code

The Arduino Servo library handles the pulse timing. A library is reusable code that provides functions for a particular task. There are many Arduino libraries. If Servo.h is unavailable in your installation, install the official Servo library through the IDE's Library Manager.

  1. Start a new sketch and put this include directive at the very top:

    #include <Servo.h>
    
  2. Below it, declare the signal pin and create a Servo object. The name myservo identifies this servo; names such as leftServo or flagServo can describe its purpose in a robot.

    const int pinServo = 3;
    Servo myservo;
    
  3. Attach the object to the signal pin in setup():

    void setup() {
      myservo.attach(pinServo);
    }
    
  4. Add this loop() to request positions in 45-degree increments, pausing half a second at each position.

    void loop() {
      myservo.write(0);
      delay(500);
      myservo.write(45);
      delay(500);
      myservo.write(90);
      delay(500);
      myservo.write(135);
      delay(500);
      myservo.write(180);
      delay(500);
    }
    
  5. Have the supply and wiring checked, keep the horn free of obstructions, then power and upload. Verify the position sequence. If the servo binds, buzzes continuously at an endpoint, or becomes hot, disconnect power and ask Mr. Willis to check the usable angle range.

    Combining this with the motor lab

    On an Uno R3, the Servo library uses a timer that disables PWM from analogWrite() on pins 9 and 10. The motor-driver lab uses pin 10, so those examples cannot be combined unchanged. This servo lab is a separate circuit. Servo library documentation

  6. Copy the wiring table into your notebook, identifying the actual approved supply, and paste your complete final code.

Turn It In

  1. Submit the following notebook entry:
  • Notebook entry: wiring table identifying the approved supply, and complete final Arduino code

How It's Graded

One score from 0–4 covers the required work and evidence. Verification may be shown during the lab; the required uploads are listed above.

Score Evidence
4 Excellent The complete code and actual wiring/supply table agree; the servo position sequence is verified without binding or power problems.
3 Above Average The servo operation, final code, and wiring table are complete and working, with only minor errors or omissions in clarity or verification.
2 Average The servo operation, final code, and wiring table have significant gaps, errors, or missing evidence.
1 Below Average The required work is largely incomplete or nonfunctional, with little usable evidence.
0 Failing No submission or no evidence of the required work.

Converted from the original lab.