Hobby Servo Tutorial

Contributors: MikeGrusin, Byron J.
Favorited Favorite 24

Controlling a Servo with Arduino

Because they move on command, servo motors are an easy way to add motion to any project. If you're working with an Arduino-compatible platform, the Arduino servo library provides a ready-to-go servo pulse generation solution.

Suggested Materials

To build this example, you'll need the following materials.

SparkFun RedBoard - Programmed with Arduino

SparkFun RedBoard - Programmed with Arduino

Wall Adapter Power Supply - 5VDC, 2A (USB Micro-B)

Wall Adapter Power Supply - 5VDC, 2A (USB Micro-B)

Servo - Generic (Sub-Micro Size)

Servo - Generic (Sub-Micro Size)

USB Micro-B Cable - 6 Foot

USB Micro-B Cable - 6 Foot

Jumper Wires Premium 12" M/F Pack of 10

Jumper Wires Premium 12" M/F Pack of 10


Hardware Hookup

Hooking a servo to a RedBoard is pretty straightforward. It only requires 3 connections.

Servo Circuit Diagram Updated

Connecting a Servo to a RedBoard

In particular, notice that power to the servo motor is supplied from the VIN pin, which bypasses the onboard regulator. The onboard regulator is insufficient to drive anything but the smallest of servos. You'll also notice that the project is powered with a 5V wall adapter. On the author's workbench, performance was marginal when the board was powered by the USB port.


With the circuit hooked up, load the following sketch.

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

If you have not previously installed an Arduino library, please check out our installation guide.

Example sketch for connecting a hobby servo to a sparkfun redboard
Byron Jacquot@ SparkFun Electronics
May 17, 2016

**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).**

Development environment specifics:
Arduino 1.6.5

#include <Servo.h>

Servo testservo;

uint32_t next;

void setup()
  // the 1000 & 2000 set the pulse width 
  // mix & max limits, in microseconds.
  // Be careful with shorter or longer pulses.
  testservo.attach(9, 1000, 2000);

  next = millis() + 500;

void loop()
  static bool rising = true;

  if(millis() > next)
      rising = false;
      rising = true;

    // repeat again in 3 seconds.
    next += 3000;


This sketch drives the servo back and forth.

Pay particular attention to the attach() call on line 26. It is using the optional min and max parameters, to constrain the pulses to the 1000 to 2000 microsecond (1 to 2 millisecond) range. As noted in the Range Constraints section above, driving a servo outside that range may damage the servo.

The Servo object can be a little bit confusing about the ranges and positions, particularly if the minimum and maximum pulses are defined in the attach() call. With the minimum and maximum defined, the parameters to the write() method (ideally expressed in degrees) get retranslated to the constrained range. In the example, write(0) will result in a 1 millisecond pulse, and write(180) yields a 2 millisecond pulse. On an average servo, this translates to approximately 90° of motion, not the 180° that the call parameters would indicate.

The servo library also has a few other limitations. Most notably, it overrides analogWrite()on pins 9 and 10. For more information about the library, check the Arduino reference pages.

If things don't seem to be quite right, please look through the troubleshooting section.