The Arduino can control two servos with the same ease as one. All it takes is creating a second instance (copy) of the Servo object, giving it a unique name. For example, in a two-wheeled differentially-steered robot you might call one servo object servoLeft, and the other servoRight.
The Arduino lacks direct connections for attaching the servo motors. Instead, the mini breadboard provides prototyping space for connecting both servos, as well as the AA battery holder that powers the servos.
Refer to Figure 1 (schematic) and Figure 2 (pictorial) for wiring the solderless breadboard. Using a strip of 0.100" double-sided (long) male header pins, break off two sets of three pins and one set of pins for the AA battery connection.
[Figure 1]
[Figure 2]
Note that you want the version of male header pins that are “double-sided”—they’re long on both sides. If you use standard header pins, the length of the pins on one side is shorter. These don’t make good contact when used with solderless breadboard designs. In a pinch, you can instead use right-angle header pins and straighten them out so that all the pins are flat.
The reference design uses an AA battery holder with a four-pin female connector. The + and – leads are on the two outside positions of the connector. I’ve broken off the pin right next to the + connection of the male header, then used a short piece of solid conductor hookup wire to fill in its corresponding hole in the connector. This prevents the connector from being reversed when it is plugged in.
When wiring the solderless breadboard, be especially careful not to mix positive and negative leads to the servo. Reversing the power will permanently damage it.
Here’s an important note: the ArdBot uses separate battery supplies for the Arduino and its two servos. In order for everything to function properly, the ground connections for the Arduino and the servo battery supply must be connected together. This is shown in both the schematic and pictorial circuit views.
Make sure to also properly orient the connectors for the servos when you plug them into the board. Servo power leads are color-coded, but the colors aren’t universal.
When in doubt, check the spec sheet that comes with your servos. Don’t guess!
With the ArdBot constructed and the breadboard wired, you’re ready to test the robot and put it through its paces. Refer to the sketch below.
Start the Arduino IDE, connect a USB cable between your computer and the Arduino (as noted on the Getting Started pages of the Arduino Web site), and type the program as shown. When done, Verify (compile) the sketch, and look for any syntax errors. If there are none, download the sketch to your Arduino.
Once downloaded, put a small book under your ArdBot to lift its wheels off the ground. Disconnect the USB cable, and, in this order, plug the AA battery connector into the breadboard, then plug in the 9-volt power to the Arduino power jack. (If you are using an Arduino Diecimila, be sure to switch over the power selection jumper from USB to EXTernal.)
If everything is connected properly, the servo motors should go through a test pattern.
Assuming the motors are working as they should, depress the Reset switch on the Arduino board and place the ArdBot on the ground. Release the switch, and the robot should perform its self-test maneuvers.
If the motors aren’t moving, double-check your wiring, making sure the servo connectors are properly oriented. They won’t work if the connectors are reversed on the breadboard.
#include <Servo.h> Servo servoLeft; // Define left servo Servo servoRight; // Define right servo void setup() { servoLeft.attach(10); // Set left servo to digital pin 10 servoRight.attach(9); // Set right servo to digital pin 9 } void loop() { // Loop through motion tests forward(); // Example: move forward delay(2000); // Wait 2000 milliseconds (2 seconds) reverse(); delay(2000); turnRight(); delay(2000); turnLeft(); delay(2000); stopRobot(); delay(2000); } // Motion routines for forward, reverse, turns, and stop void forward() { servoLeft.write(0); servoRight.write(180); } void reverse() { servoLeft.write(180); servoRight.write(0); } void turnRight() { servoLeft.write(180); servoRight.write(180); } void turnLeft() { servoLeft.write(0); servoRight.write(0); } void stopRobot() { servoLeft.write(90); servoRight.write(90); }
Let’s review how the test sketch works. First off is an include statement to the Servo.h library header file, which is provided with the Arduino IDE installation. This file, and its corresponding C-language program, provide all the actual coding to make the servos function.
Next come two statements that create, or instantiate, two Servo objects for use in the remainder of the sketch. Each object represents a physical servo attached to the Arduino. Methods of these objects include things like specifying which digital pin is used to connect from the Arduino to the servo, and the position of the servo. Note that I’ve given the two Servo objects descriptive names: servoLeft and servoRight. It’s easier to keep track of things this way.
In the setup function the servoLeft and servoRight objects are “wired” to their respective pins on the Arduino—in this case, pin 10 for servoLeft and pin 9 for servoRight.
Now comes the main body of the program, provided in the loop function. It contains a series of user-defined functions for forward, backward, and so on, plus a delay of 2000 milliseconds (2 seconds) between functions. You can see that the robot repeats the same demonstration steps over and over:
And finally, each user-defined function specifies the specific motion to apply to the servos. With the Servo object, servos are commanded to move in one direction or another by (among other ways) specifying an angle of between 0 and 180. The servo then moves to that angle in response.
When using servos that have been modified for continuous rotation, 0 makes the servo rotate in one direction, 180 makes the servo rotate in the opposite direction, and 90 makes it stop. Pretty easy, isn’t it?!