The Arduino Inventor's Guide (29 page)

BOOK: The Arduino Inventor's Guide
8.12Mb size Format: txt, pdf, ePub

Inside a servo are three main parts: the motor, gear train, and control circuit. When voltage is applied to the motor, it turns the gear train, which turns the hub of the servo motor. The rotational position of the hub is controlled by the control circuit. Part of the gear train is a potentiometer that rotates as the motor rotates. Remember that a potentiometer is a simple sensor that changes resistance based on how much it rotates, and when it’s connected up as an adjustable voltage divider, the voltage varies as the potentiometer rotates. The control circuit reads both the value in the input signal coming into the servo (from the Arduino, in this case) and the potentiometer value and compares them. When the two values are equal, the motor stops and holds its position.

A servo motor relies on PWM, a concept we introduced in “
Create More Colors with
analogWrite()
” on page
138
. To control the position of a servo, the Arduino sends out a PWM signal that pulses every 20 ms. The width of the pulse corresponds to a specific rotational position for the servo motor.
Figure 6-10
illustrates this by showing the minimum PWM pulse widths for 0 degrees of a servo, the midpoint of 90 degrees, and the maximum of 180 degrees. Similar to blinking an LED, you can use the Arduino to create a very short pulse that is on for 1 ms and off for 19 ms to move the servo to an angle of 0 degrees.

FIGURE 6-10:
The PWM duty cycles for the standard range of a servo

To set the angle of a servo motor to 0 degrees, you could use code like the following:

void
setup
()
{
  
pinMode
(9,
OUTPUT
);
}
void
loop
()
{
  
digitalWrite
(9,
HIGH
);
  
delay
(1);
  
digitalWrite
(9,
LOW
)
  
delay
(19);
}

This code drives pin 9
HIGH
for 1 ms, and then immediately sets the pin
LOW
for 19 ms. As soon as the 19 ms are over, it has to drive the pin
HIGH
again for 1 ms to maintain the timing cycle. If your code is busy managing timing like this, you can’t add anything else to it without affecting the timing of the pulses and control of the servo. Thankfully, the Arduino has a trick to simplify the way you control the servo motor: using a
library
. A library is a file containing extra code that you can use with your sketch to perform specific tasks or make it easier to use particular parts. The Servo library handles all of the pulse timing needed to drive the servo motor to a specific angle.

In this project, you’ll be using the Arduino to move a balance beam based on the voltage output of a sensor—your potentiometer. The code will use the voltage reading of the sensor to set the appropriate pulse width length for a given rotation for the servo, which will determine the angle of the beam.

The good news is that the Arduino, and more specifically the Servo library, does all of the hard work for you! It is great to understand how the pulse width controls the position of the servo, but, in the end, the software takes care of it for you.

BUILD THE BALANCE BEAM PROTOTYPE

Now that you know the theory, you’ll build the circuit for the Balance Beam. You’ll start by connecting the servo, and then you’ll add a potentiometer;
Figure 6-11
shows the full circuit.

Notice that the servo has a single three-pin female header. To connect this to your circuit, you’ll need to use male-to-male jumper wires. Take three short male-to-male jumper wires and connect these to the female pins, as shown in
Figure 6-12
. It’s good practice to use the colors that correspond to the servo wires to make it easier to see which is which: black, red, and white represent the ground, power, and signal lines, respectively. Now, hook the servo up to the Arduino.

FIGURE 6-11:
Balance Beam prototype circuit

FIGURE 6-12:
Adding male-to-male jumper wire extensions to the servo motor

The circuit connection is pretty simple: connect 5 V and GND from the Arduino to the power rails on the left side of the breadboard. Connect the servo’s ground (black) wire to the ground rail on the breadboard and the power (red) wire to the 5 V rail. Connect the signal wire directly to pin 9 on the Arduino. A complete diagram is shown in
Figure 6-13
.

FIGURE 6-13:
Servo hooked up to signal, power, and ground

Finally, add the servo
horn
onto the hub of the servo. Horns are different-shaped arms for a servo that rotate with the hub to make it easier to use and attach things to the servo. At this point, select any one of the horns that come with the servo, and press-fit it onto the hub of the servo, as shown in
Figure 6-14
. You will add a specific horn later, but for now we just want to make it easier to see rotation.

FIGURE 6-14:
Press-fitting a servo horn from the included options

If the servo starts moving or acting erratically, simply disconnect its black wire from the ground rail to stop it. It’s good safety practice to keep the black wire disconnected until you upload code.

Now, wire up the potentiometer. The breadboard has plenty of room, so place the potentiometer anywhere you like, making sure each leg is in its own row. Connect the two outside pins to the 5 V and ground rails, with the center pin connected to analog input pin A0 directly on the Arduino, as shown in
Figure 6-15
.

FIGURE 6-15:
The full Balance Beam circuit

At the moment, you just have a servo connected to a potentiometer. To give the project its balancing powers, you need to program it.

PROGRAM THE BALANCE BEAM

To use the servo with an Arduino, you need to use the Servo library— which, as mentioned earlier, is a collection of prewritten code that expands the commands and capabilities of the code in your sketch. It gives you more features and functions to work with and simplifies using external hardware with your Arduino. For example, the Servo library includes code that attaches the servo to a specific Arduino pin, moves the servo to specific angles, and even detaches the servo from a pin.

Before you program the full project, you’ll upload a quick test sketch to check that your servo is working correctly.

Test the Servo

Here’s a simple example sketch for controlling your servo. Start a new sketch by selecting
File

New
, and then enter the sketch in
Listing 6-1
:

LISTING 6-1:
A servo “Hello world”


#include
<
Servo
.h>

Servo
myServo;
  
void
setup
()
  {

   myServo.
attach
(9);
  }
  
void
loop
()
  {

   myServo.
write
(90);
  }

To use the Servo library, call
#include

, which tells the Arduino to include the
Servo.h
file containing the Servo library code. This adds the functions and definitions of the library to the sketch. Notice that this is one of the rare instances where there isn’t a semicolon at the end of the line. In Arduino programming, the
#
symbol indicates that the following code is a
preprocessor directive
, a special piece of code that should be executed before the rest of the sketch. When you compile a sketch, the first thing that runs is the
preprocessor
, which searches for any lines that start with a
#
symbol and don’t end with the semicolon and runs those lines first. The
#include
directive tells the preprocessor to include all of the code in the named file before compiling the code in your sketch.

You can also use the drop-down menu to add a library by selecting
Sketch

Include Library…
and then selecting the library you want to use (in this case,
Servo
). This will automatically add the
#include
statement to your sketch. This option is great if you can’t remember the precise syntax of the
#include
command or the library name—for example, when you use a library for the first time.

Other books

The Right Time by Susan X Meagher
Chasing the Night by Iris Johansen
Yesterday's Papers by Martin Edwards
The Archon's Assassin by D. P. Prior
Kisser by Stuart Woods
Terrible Swift Sword by William R. Forstchen