Component: Servo Motor

Objectives and Overview

This lesson introduces the servo motor. Servo motors are fun and powerful and open up a lot of project possibilities. This lesson includes an introduction to using the Servo Library that provides additional functionality.

Lesson Objectives

  • Become familiar with the servo motor component.
  • Understand and explain how to include a library in an Arduino project.

Working with Servo Motors

Servo motors are small RC hobby motors that can be controlled by an Arduino through sending signals to the motor shaft. Servos have plastic arms that connect to the top of this shaft. You’ll come across servos that have different tops attached. The ones in your kit have some options for you to try out. You’re able to switch out these tops rather easily- sometimes there is a tiny screw that needs to be detached, but often you can just use your fingers to switch out the arms.

There are many sizes of servos, but you’ll be using a micro servo. Here’s a photo of a servo you’ll be using for this project:

Servo motor sitting on a table

Controlling a Servo Motor

Servo motors have three wires:

  • Red – Power (5v)
  • Brown – Ground
  • Orange – Signal / Data

These wires then connect to the Arduino board. The Arduino sends electric pulse signals to the servo, which causes it to rotate. When you begin working with the code, you’ll see how this process of sending a message from the Arduino to a component is quite similar to what you’ve done so far with LEDs.

Servo motor connected to jumper wires

Servos and Arduino

Once the servo motor is connected to an Arduino board, you’re able to control the angle of the shaft position, typically between a range of 0 to 180 degrees. There are certain types of servos that provide continuous rotation that can move at different speeds, but the more common types work within a range of 0 to 180 degrees and move one step (degree) at a time.

In order to work with servos, you’ll need to import a code library specifically for the servo. There are several Arduino libraries that you’re able to include in projects. Often these libraries are written to provide additional support or functionality for working with certain components. Practicing importing and including libraries will open up lots of projects and ideas!

The Servo Library can support up to 12 motors on the board and uses a special servo.write() command to send position data to the motor. The servo.write() is from the Servo Library and the Arduino IDE wouldn’t understand what it means without including the library.

You’ll have the entirety of the code to experiment with, but let’s cover some key parts:

Including a Library: to include a library in your code, you add #include at the top of your code, before anything else:

#include  //This is necessary when working with servos. It includes the servo library.

The name of the library goes in the < >. In this example, it’s called Servo.h

Many times when working with libraries there are code examples that you can reference if you need help knowing what to include.

Creating a servo object: the next key piece of code creates a servo object. This is what the Arduino uses to work with the servo component:

Servo myservo;  // create servo object instance to control a servo

After you’ve done this step, you’re then able to interact with the servo that you’ve attached to your board.

Connecting the servo: Just like when working with any of the other components, such as the LED, you need to tell the Arduino the pin number where the component is attached. For the servos, this is the pin that the orange (signal) wire connects with. Here’s the code that goes into the void setup():

myservo.attach(9);  // attaches the servo on pin 9 to the servo object- needs to match the pin servo is on board

This covers the basic introduction to using servo motors with Arduino!