LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)

Home > Robotics with the Board of Education Shield for Arduino > Chapter 2. Shield, Lights, Servo Motors

Chapter 2. Shield, Lights, Servo Motors

In this chapter, you will use the Board of Education Shield for building and testing circuits with Parallax continuous rotation servos, resistors, and light-emitting diodes. Along the way, you’ll start learning the basics of building circuits and making the Arduino interact with them. By the end of the chapter, you’ll have a pair of servos connected, each with its own signal indicator light, and you’ll be writing sketches to control servo speed and direction.

Board of Education Shield (for Arduino), Parallax continuous rotation servo, LED, and resistor

  • Download Chapter 2 Arduino code [1]
  • Save the file to your desktop, and un-zip it.  The file contains example sketches, and a PDF of a table that you will fill out.
  • Follow the links below to get started!

Activity 1: Board of Education Shield Setup

The Board of Education Shield makes it easy to build circuits and connect servos to the Arduino module.  In this chapter, you will use it to test servos and indicator lights. Next chapter, you’ll mount the BOE Shield and servos on a robot chassis to build a robot we’ll call the BOE Shield-Bot.

Parts List:

(1)  Arduino module
(1)  Board of Education Shield
(4)  1″ round aluminum standoffs
(4)  pan head screws, 1/4″ 4-40
(3)  1/2″ round nylon spacers*
(3)  nylon nuts, 4-40*
(3)  pan head screws, 7/8″, 4-40*

(*Items also included in the Boe-Bot to Shield-Bot Retrofit Kit [2])

Parts required for Board of Education Shield setup

Instructions:

The four groups of pins under the Board of Education Shield plug into the four Arduino socket headers.  There are also three board-connection holes in the shield that line up with holes in the Arduino module, designed to connect the two boards together with screws and nylon spacers. 

If you have a revision 3 Arduino, it will be labeled UNO R3 or MEGA R3 on the back.  R3 boards will have two empty pairs of sockets, closest to the USB and power connectors, after socketing the shield.  Earlier versions, such as 2, 1, and Duemilanove, have the same number of sockets as the shield has pins, so there will be no empty sockets left over.  If you have an Arduino Mega, the four pin groups will fit into the four headers closest to the USB and power connectors, as shown in the box below.

How the BOE Shield plugs into the Arduino Uno or Mega

  • Disconnect the programming cable from your Arduino module.
  • Look closely at your Arduino module and the pins on the Board of Education Shield to see how the sockets and pins will line up for your particular boards.  Note that If you have an Arduino Mega,  its USB port and power jack will be close to the edge of the shield, like the image on the right.

The Arduino Mega will not protrude far out from under the Board of Education Shield

Component placement varies a little bit for the different Arduino models; some can only fit one or two nylon standoffs for holding the boards together. This is okay, but you need to find out which holes you can use before socketing the Board of Education Shield.

Board-connection hole placement on the Board of Education Shield

  • Hold a nylon spacer over each mounting hole on your Arduino module, and look through it to see if the spacer can line up with the hole completely.
  • For each hole that works on your Arduino module, insert a 7/8″ screw through the corresponding board-connection hole in your Board of Education Shield.

Plug the Board of Education Shield into the Arduino module and secure with nylon spacers, screws, and nylon nuts.

  • Slide a nylon spacer over each screw you used.
  • Line up the Arduino module’s sockets with the Board of Education Shield’s pins.
  • Also line up the 7/8″ screws with the mounting holes in the Arduino board.
  • Gently press the two boards together until the pins are firmly seated in their sockets.  The sockets will not cover the pins completely; there will be about 3/8″ (~5 mm) of the pins still exposed between the bottom of the shield and the top of the sockets.
  • Check to make ABSOLUTELY SURE your pins are seated in the sockets correctly. It is possible to misalign the pins, which can damage your board when it is powered.

Check pin alignment between Board of Education Shield and Arduino module - no pins should be visible in the gap between socket headers

  • Thread a nylon nut over each screw, and tighten gently.

To keep the connected boards up off of the table, we’ll mount tabletop standoffs to each corner of the Board of Education Shield.

  • Thread a 1/4″ screw through a corner hole on the Board of Education Shield from the top side. 
  • Thread a 1″ aluminum standoff onto the screw and tighten gently.
  • Repeat until all four standoffs are installed.

Board of Education Shield with Arduino module plugged in and tabletop standoffs installed

Activity 2: Build and Test LED Indicator Lights

Indicator lights give people a way to see a representation of what’s going on inside a device, or patterns of communication between two devices.  Next, you will build indicator lights to display the communication signals that the Arduino will send to the servos.  If you haven’t ever built a circuit before, don’t worry, this activity shows you how.

Board of Education Shield with two red LED circuits built onto its breadboard

Introducing the Resistor

Introducing the Resistor

A resistor is a component that resists the flow of electricity.  This flow of electricity is called current.  Each resistor has a value that tells how strongly it resists current flow.  This resistance value is called the ohm, and the sign for the ohm is the Greek letter omega: Ω.  (Later on you will see the symbol kΩ, meaning kilo-ohm, which is one thousand ohms.)

This resistor has two wires (called leads and pronounced “leeds”), one coming out of each end.  The ceramic case between the two leads is the part that resists current flow.  Most circuit diagrams use the jagged line symbol with a number label to indicate a resistor of a certain value, a 470 Ω resistor in this case.  This is called a schematic symbol.  The part drawing on the right is used in some beginner-level texts to help you identify the resistors in your kit, and where to place them when you build circuits. 

A resistor schematic symbol, labeled 470 ohms, and a wirig diagram showing color-code bands

The resistors in your parts kit have colored stripes that indicate what their resistance values are.  There is a different color combination for each resistance value.  For example, the color code for the 470 Ω resistor is yellow-violet-brown. 

There may be a fourth stripe that indicates the resistor’s tolerance.  Tolerance is measured in percent, and it tells how far off the part’s true resistance might be from the labeled resistance.  The fourth stripe could be gold (5%), silver (10%) or no stripe (20%).  For the activities in this book, a resistor’s tolerance does not matter, but its value does.

Each color bar on the resistor's case corresponds to a digit, as listed in the table below.

Resistor Color Code Values
Digit0123456789
Colorblackbrownredorangeyellowgreenbluevioletgraywhite

 

resistor color code bands indicating digits, using a 470 ohm radial resistor (yellow violet brown)

Here’s how to find the resistor’s value, in this case proving that yellow-violet-brown is really 470 Ω:

  • The first stripe is yellow, which means the leftmost digit is a 4.
  • The second stripe is violet, which means the next digit is a 7.
  • The third stripe is brown.  Since brown is 1, it means add one zero to the right of the first two digits.

Yellow-Violet-Brown = 4-7-0 = 470 Ω.

Your Turn

  • Use the table and picture above to figure out the color code for the 220 Ω resistors you will need for the indicator lights. 

Introducing the LED

Introducing the LED

A diode is a one-way electric current valve, and a light-emitting diode (LED) emits light when current passes through it.  Since an LED is a one-way current valve, you have to make sure to connect it the right way for it to work as intended. 

An LED has two terminals: the anode and the cathode. The anode lead is labeled with the plus-sign (+) in the part drawing, and it is the wide part of the triangle in the schematic symbol.  The cathode lead is the pin labeled with a minus-sign (-), and it is the line across the point of the triangle in the schematic symbol.

LED (light-emitting diode) schematic symbol and part drawing

When you build an LED circuit, you will have to make sure the anode and cathode leads are connected to the circuit properly. You can tell them apart by the shape of the LED’s plastic case.  Look closely at the case—it’s mostly round, but there is a small flat spot right near one of the leads, and that tells you it’s the cathode. Also note that the LED’s leads are different lengths.  Usually, the shorter lead is connected to the cathode.

Always check the LED’s plastic case.
Usually, the longer lead is connected to the LED’s anode, and the shorter lead is connected to its cathode. But sometimes the leads have been clipped to the same length, or a manufacturer does not follow this convention.  So, it’s best to always look for the flat spot on the case.  If you plug an LED in backwards, it will not hurt it, but it won’t emit light until you plug it in the right way.

 

Introducing the Prototyping Area

The white board with lots of square sockets in it is called a solderless breadboard.  This breadboard has 17 rows of sockets. In each row, there are two five-socket groups separated by a trench in the middle. All the sockets in a 5-socket group are connected together underneath with a conductive metal clip. So, two wires plugged into the same 5‑socket group make electrical contact. This is how you will connect components, such as an LED and resistor, to build circuits. Two wires in the same row on opposite sides of the center trench will not be connected. 

Board of Education Shield protoyping area with solderless breadboard, power headers, and analog/digital pin headers to the Arduino

The prototyping area also has black sockets along the top, bottom, and left. 

  • Top: these sockets have three supply voltages for the breadboard: 3.3 V, Vin (input voltage from either battery pack or programming cable), and 5 V. 
  • Bottom-left: The first six sockets along the bottom-left are ground terminals, labeled GND; think of them as a supply voltage that’s 0 V.  Collectively, the 3.3V, Vin, 5V and GND are called the power terminals, and they will be used to supply your circuits with electricity. 
  • Bottom-right: The ANALOG IN sockets along the bottom-right are for measuring variable voltages; these connect to the Arduino module’s ANALOG IN sockets.
  • Left: The DIGITAL sockets on the left have labels from 0 to 13.  You will use these to connect your circuit to the Arduino module’s digital input/output pins.

Digital and analog pins are the small pins on the Arduino module’s Atmel microcontroller chip.  These pins electrically connect the microcontroller brain to the board.

Digital and analog pins on an Arduino Uno module, close-up

A sketch can make the digital pins send high or low signals to circuits. In this chapter, we’ll do that to turn lights on and off. A sketch can also make a digital pin monitor high or low signals coming from a circuit; We’ll do that in another chapter to detect whether a contact switch has been pressed or released.

A sketch can also measure the voltages applied to analog pins; we’ll do that to measure light with a phototransistor circuit in another chapter.

How To: The Basics of Breadboarding (A YouTube video from Parallax Inc.)

LED Test Circuit

LED Test Circuit Parts

(2) LEDs – Red
(2) Resistors,  220 Ω (red-red-brown)
(3) Jumper wires
 

Always disconnect power to your board before building or modifying circuits!  
1. Set the BOE Shield’s Power switch to 0.
2. Disconnect the programming cable and battery pack.

LED Test Circuits

The image below shows the indicator LED circuit schematic on the left, and a wiring diagram example of the circuit built on your BOE Shield’s prototyping area on the right.

  • Build the circuit shown below. If you are new to building circuits, try to follow the wiring diagram exactly.
  • Make sure your LED cathode leads are connected to GND.  Remember, the cathode leads are the shorter pins that are closer to the flat spot on the LED’s plastic case.  Each cathode lead should be plugged into the same 5-socket row as the wires that run to the GND sockets.
  • Make sure that each longer anode lead is connected to the same 5-socket row as a resistor lead.

Schematic and wiring diagram of two LED circuits built on the Board of Education Shield breadboard

The next picture will give you an idea of what is going on when you program the Arduino to control the LED circuit.  Imagine that you have a 5 volt (5 V) battery. The Board of Education Shield has a device called a voltage regulator that supplies 5 volts to the sockets labeled 5V.  When you connect the anode end of the LED circuit to 5 V, it’s like connecting it to the positive terminal of a 5 V battery.  When you connect the circuit to GND, it’s like connecting to the negative terminal of the 5 V battery.

Diagram showing electrical connection with LEDs and batteries

On the left side of the picture, one LED lead is connectd to 5 V and the other to GND. So, 5 V of electrical pressure causes electrons to flow through the circuit (electric current), and that current causes the LED to emit light.  The circuit on the right side has both ends of the LED circuit connected to GND.  This makes the voltage the same (0 V) at both ends of the circuit.  No electrical pressure = no current = no light. 

You can connect the LED to a digital I/O pin and program the Arduino to alternate the pin’s output voltage between 5 V and GND.  This will turn the LED light on/off, and that’s what we’ll do next.

Volts is abbreviated V. 
When you apply voltage to a circuit, it’s like applying electrical pressure. By convention, 5 V means “5 V higher than ground.” Ground, often abbreviated GND, is considered 0 V. 

Ground is abbreviated GND.
The term ground originated with electrical systems where this connection is actually a metal rod that has been driven into the ground.  In portable electronic devices, ground is commonly used to refer to connections that go to the battery supply’s negative terminal.

Current refers to the rate at which electrons pass through a circuit. 
You will often see measurements of current expressed in amps, which is abbreviated A.  The currents you will use here are measured in thousandths of an amp, or milliamps.  For example, 10.3 mA passes through the circuit shown above.

How a Sketch Makes the LED Turn On and Off

Let’s start with a sketch that makes the LED circuit connected to digital pin 13 turn on/off.  First, your sketch has to tell the Arduino to set the direction of pin 13 to output, using the pinMode function: pinMode(pin, mode).  The pin parameter is the number of a digital I/O pin, and mode must be either INPUT or OUTPUT.

void setup()                      // Built-in initialization block
{
  pinMode(13, OUTPUT);            // Set digital pin 13 -> output
}

Now that digital pin 13 is set to output, we can use digitalWrite to turn the LED light on and off.  Take a look at the picture below.  On the left, digitalWrite(13, HIGH) makes the Arduino’s microcontroller connect digital pin 13 to 5 V, which turns on the LED.  On the right, it shows how digitalWrite(13, LOW) makes it connect pin 13 to GND (0 V) to turn the LED off.

Diagram showing how a sketch causes the Arduino's microcontroller to connect an LED circuit to 5 V or to GND

Here’s the loop function from the next sketch.  First, digitalWrite(13, HIGH) turns the light on, delay(500) keeps it on for a half-second. Then  digitalWrite(13, LOW) turns it off, and that’s also followed by delay(500).  Since it’s inside the loop function’s block, the statements will repeat automatically.  The result?  The light will flash on/off once every second.

void loop()                        // Main loop auto-repeats
{                                         
  digitalWrite(13, HIGH);          // Pin 13 = 5 V, LED emits light
  delay(500);                      // ..for 0.5 seconds
  digitalWrite(13, LOW);           // Pin 13 = 0 V, LED no light
  delay(500);                      // ..for 0.5 seconds
}

Example Sketch: HighLowLed

  • Reconnect the programming cable to your board.
  • Enter, save, and upload HighLowLed to your Arduino.
  • Verify that the pin 13 LED turns on and off, once every second. (You may see the LED flicker a few times before it settles down into a steady blinking pattern. This happens when reprogramming the Arduino.)
/*
 Robotics with the BOE Shield - HighLowLed
 Turn LED connected to digital pin 13 on/off once every second.
 */

void setup()                                 // Built-in initialization block
{
  pinMode(13, OUTPUT);                       // Set digital pin 13 -> output
}  
 
void loop()                                  // Main loop auto-repeats
{                                         
  digitalWrite(13, HIGH);                    // Pin 13 = 5 V, LED emits light
  delay(500);                                // ..for 0.5 seconds
  digitalWrite(13, LOW);                     // Pin 13 = 0 V, LED no light
  delay(500);                                // ..for 0.5 seconds
}  

Introducing the Timing Diagram

A timing diagram is a graph that relates a signal's high and low stages to time.  This timing diagram shows you a 1000 ms slice of the HIGH (5 V) and LOW (0 V) signals from the sketch HighLowLed.  Can you see how delay(500) is controlling the blink rate? 

Picture showing a timing diagram of HIGH and LOW signals that turns an LED on or off every 500 ms

Your Turn – Experiment with the Blink Rates and Both LEDs

How would you make the LED blink twice as fast?   How about reducing the delay function’s ms parameters by half?

  • Try modifying your sketch to use delay(250). Don’t forget to change it in both places!
  • How far can you reduce the delay before it just looks like the LED is dim instead of blinking on/off?

Blinking the pin 12 LED is a simple matter of changing the pin parameter in the pinMode and two digitalWrite function calls. 

  • Modify the sketch so that pinMode in the setup function uses pin 12 instead of pin 13.
  • Also modify both digitalWrite statements in the loop function to use pin 12.
  • Run it, and make sure the pin 12 LED blinks.

You can also make both LEDs blink at the same time.

  • Add statements to the sketch so that it uses pinMode twice:
  pinMode(13, OUTPUT);         // Set digital pin 13 -> output
  pinMode(12, OUTPUT);         // Set digital pin 12 -> output
  • ….and uses digitalWrite four times:
  digitalWrite(13, HIGH);      // Pin 13 = 5 V, LED emits light
  digitalWrite(12, HIGH);      // Pin 12 = 5 V, LED emits light
  delay(500);                  // ..for 0.5 seconds
  digitalWrite(13, LOW);       // Pin 13 = 0 V, LED no light
  digitalWrite(12, LOW);       // Pin 12 = 0 V, LED no light
  delay(500);                  // ..for 0.5 seconds
  • Run the modified sketch. Do both LEDs blink on and off together?

How would you modify the sketch again to turn one LED on while the other turns off? One circuit will need to receive a HIGH signal while the other receives a LOW signal. 

  • Try it!

Activity 3: LED Servo Signal Monitors

The high and low signals that control servo motors must last for very precise periods of time.  That’s because a servo motor measures how long the signal stays high, and uses that as an instruction for how fast, and in which direction, to turn its motor. 

This timing diagram shows a servo signal that would make your Shield-Bot’s wheel turn full speed counterclockwise.  There’s one big difference though: all the signals in this timing diagram last 100 times longer than they would if they were controlling a servo.  This slows it down enough so that we can see what’s going on.

Servo control timing diagram, and 1/100th actual speed

Example Sketch: ServoSlowMoCcw

  • Enter, save, and upload ServoSlowMoCcw to the Arduino. 
  • Verify that the pin 13 LED circuit pulses briefly every two seconds.
/*
    Robotics with the BOE Shield - ServoSlowMoCcw
    Send 1/100th speed servo signals for viewing with an LED.
 */

void setup()                                 // Built in initialization block
{
  pinMode(13, OUTPUT);                       // Set digital pin 13 -> output
}  
 
void loop()                                  // Main loop auto-repeats
{                                         
  digitalWrite(13, HIGH);                    // Pin 13 = 5 V, LED emits light
  delay(170);                                // ..for 0.17 seconds
  digitalWrite(13, LOW);                     // Pin 13 = 0 V, LED no light
  delay(1830);                               // ..for 1.83 seconds
}

Your Turn – Two Steps to Servo Signal

Alright, how about 1/10th speed instead of 1/100th speed?

  • Reduce delay(170) to delay(17), and delay(1830) to delay(183), and re-upload the sketch.

Is the LED blinking 10 times faster now?  Divide by 10 again for a full speed servo signal—we’ll have to round the numbers a bit: 

  • Change delay(17) to delay(2), and delay(183) to delay(18), then upload the modified sketch.

Now you can see what the servo signal looks like with the indicator LED.  The LED is flickering so fast, it’s just a glow.  Since the high signal is 2 ms instead of 1.7 ms, it’ll be a little brighter than the actual servo control signal—the light is spending more time on.  We could use this signal and programming technique to control a servo, but there’s an easier, more precise way. Let’s try it with LEDs first.

How to Use the Arduino Servo Library

A better way to generate servo control signals is to include the Arduino Servo library in your sketch, one of the standard libraries of pre-written code bundled with the Arduino software. 

  • To see a list of Arduino libraries, click the Arduino software’s Help menu and select Reference.
  • Find and follow the Libraries link.

We want to take a closer look at the Servo library.

  • Find and follow the Servo link.
  • Follow and read the links for these functions on the Servo library page:

attach()

writeMicroseconds()

detach()

Servos have to receive high-pulse control signals at regular intervals to keep turning.  If the signal stops, so does the servo.  Once your sketch uses the Servo library to set up the signal, it can move on to other code, like delays, checking sensors, etc. Meanwhile, the servo keeps turning because the Servo library keeps running in the background.  It regularly interrupts the execution of other code to initiate those high pulses, doing it so quickly that it’s practically unnoticeable.

Using the Servo library to send servo control signals takes four steps: 

  1. Tell the Arduino editor that you want access to the Servo library functions with this declaration at the start of your sketch, before the setup function.
     #include <Servo.h>          // Include servo library
  1. Declare and name an instance of the Servo library for each signal you want to send, between the #include and the setup function.
     Servo servoLeft;            // Declare left servo
  1. In the setup function, use the name you gave the servo signal followed by a dot, and then the attach function call to attach the signal pin.   This example is telling the system that the servo signal named servoLeft should be transmitted by digital pin 13.
     servoLeft.attach(13);      // Attach left signal to pin 13
  1. Use the writeMicroseconds function to set the pulse time.  You can do this inside either the setup or loop function:
     servoLeft.writeMicroseconds(1500); // 1.5 ms stay-still signal

Seconds, Milliseconds, Microseconds
A millisecond is a one-thousandth of a second, abbreviated ms.
A microsecond is a one-millionth of a second, abbreviated μs.
There are 1000 microseconds (μs) in 1 millisecond (ms). 
There are 1,000,000 microseconds in 1 second (s).

Example Sketch: LeftServoStayStill

For calibrating servos, your sketch will need to send signals with 1.5 ms pulses.  Take a look at the timing diagram below.  This stay-still signal’s high pulses last 1.5 ms.  That’s halfway between the 1.7 ms full-speed-counterclockwise and 1.3 ms full-speed-clockwise pulses.

Timing diagram showing the stay-still servo control signal, 1.5 ms high, 20 ms low

  • Enter, save and upload LeftServoStayStill to your Arduino.  The pin 13 LED should glow, about halfway between the two brightness levels you observed earlier.
/*
 Robotics with the BOE Shield – LeftServoStayStill
 Generate signal to make the servo stay still for centering.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left servo

void setup()                                 // Built in initialization block
{
  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoLeft.writeMicroseconds(1500);         // 1.5 ms stay still signal
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Your Turn – Check a Second Control Signal with the Pin 12 LED

You’ll be using this code a lot, so it’s a good idea to practice declaring an instance of Servo, attaching the signal to a pin, and setting the pulse duration.

  • Save LeftServoStayStill as BothServosStayStill.
  • Add a second Servo declaration and name it servoRight.
     Servo servoRight;             // Declare right servo
  • Attach your servoRight signal to digital pin 12.
     servoRight.attach(12);        // Attach right signal to pin 12
  • Set the servoRight signal for 1.5 ms (1500 μs) pulses.
     servoRight.writeMicroseconds(1500); // 1.5 ms stay still signal
  • Your sketch should now look like BothServosStayStill.
  • Save the sketch and upload it to your Arduino.
  • Verify that both LEDs are at a similar brightness level.
/*
 Robotics with the BOE Shield – BothServosStayStill
 Generate signals to make the servos stay still for centering.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left servo signal
Servo servoRight;                            // Declare right servo signal

void setup()                                 // Built in initialization block
{
  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                      // Attach left signal to pin 12

  servoLeft.writeMicroseconds(1500);         // 1.5 ms stay still sig, pin 13
  servoRight.writeMicroseconds(1500);        // 1.5 ms stay still sig, pin 12
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Activity 4: Connect Servo Motors and Batteries

From the robot navigation standpoint, continuous rotation servos offer a great combination of simplicity, usefulness and low price.  The Parallax continuous rotation servos are the motors that will make the BOE Shield-Bot’s wheels turn, under Arduino control.

Parallax continuous rotation servos, with parts labeled

In this activity, you will connect your servos to the Board of Education Shield’s servo ports, which will connect them to supply voltage, ground, and a signal pin.  You will also connect a battery supply to your Arduino because, under certain conditions, servos can end up demanding more current than a USB supply is designed to deliver.

Standard Servos vs. Continuous Rotation Servos
Standard servos are designed to receive electronic signals that tell them what position to hold.  These servos control the positions of radio controlled airplane flaps, boat rudders, and car steering.  Continuous rotation servos receive the same electronic signals, but instead turn at certain speeds and directions.  Continuous rotation servos are handy for controlling wheels and pulleys.

Servo Control Horn, 4-point Star vs. Round
It doesn’t make a difference.  So long as it is labeled “continuous rotation” it’s the servo for your BOE Shield-Bot.  You’ll remove the control horn and replace it with a wheel.

Connect the Servos to the BOE Shield

Leave the LED circuits from the last activity on your board. They will be used later to monitor the signals the Arduino sends to the servos to control their motion.

Parts List

(2) Parallax continuous rotation servos
BOE Shield with built and tested LED indicator circuits from the previous activity

Instructions

  • Set your Shield’s power switch to position-0.

BOE Shield's 3-position switch set to 0

  • Disconnect all sources of power from the Arduino including the USB cable.

Between the servo headers on the BOE Shield is a jumper that connects the servo power supply to either Vin or 5V.  To move it, pull it upwards and off the pair of pins it covers, then push it onto the pair of pins you want it to rest on.  The BOE Shield-Bot’s battery pack will supply 7.5 V.  Since the servos are rated for 4–6 V, we want to make sure the jumper is set to 5V.  Also, a steady 5 V voltage supply will support a consistent servo speed, and more accurate navigation, than voltage that varies as batteries discharge.

  • Make sure your BOE Shield’s power jumper is set to 5V; if not, set it now.

Set the Board of Education Shield servo jumper to 5 V

The picture below shows the schematic of the circuit you create by plugging the servos into ports 13 and 12 on the BOE Shield. Pay careful attention to wire color as you plug in the cables: the black wire should be at the bottom, and the white one should be at the top.

  • Connect your servos to your BOE Shield as shown in the diagram below. The left servo connects to port 13 and the right servo connects to port 12.
  • Make sure that you follow the cable colors shown in the figure, with the black wire closer to the breadboard and the white wire closer to the board’s edge.

Schematic and wiring diagram for connecting Parallax continuous rotation servos to the Board of Education Shield

Connect the Battery Pack to the BOE Shield

To properly power the servos, you’ll need to switch to an external battery pack now.  When servos make sudden direction changes or push against resistance to rotation, they can draw more current than a USB port is designed to supply.  Also, it would be no fun for the BOE Shield-Bot to be tethered to the computer forever!  So, from here on out we’ll be using an external battery pack with five 1.5 V AA batteries.  This will supply your system with 7.5 V and plenty of current for the voltage regulators and servos. From here forward, remember two things:

  • ALWAYS unplug the battery pack when you are done experimenting for a while.  Even when the power switch on your BOE Shield is off (position-0), the Arduino module will still draw power from the batteries.
  • Unplug the programming cable too, whenever you unplug the battery pack. That way, you won’t accidentally try to run the servos off of USB power.

Which Battery Pack Do You Have?

  • Compare your parts to the picture below to see which battery pack you have. 
  • For the 5-cell Pack, keep going on the next page.
  • For the 4-cell pack, skip to the 4-Cell Pack + Boe-Boost Setup [3].

Battery pack options for the Parallax Robotics Shield Kit

Rechargeable Options
The thrifty Boe-Boost (#30078) allows you to add another cell in series with a 4-cell or 5-cell pack.  Adding a 6th 1.2 V AA rechargeable cell to a 5-cell pack will supply 6 x 1.2 = 7.2 V. 
The Li-ion Boe-Bot Power Pack-Charger (#28988) combines a lithium-ion battery pack and recharger in one board that you can mount under your Shield-Bot.

CAUTION: AC powered DC supplies are not recommended for the BOE Shield-Bot.
Some DC supplies provide much higher voltage than their rating.  The BOE Shield-bot is designed for use with a 7.2–7.5 V battery supply.  It will work with higher supply voltages at low loads, but the servo loads can heat up the regulator until it shuts off to protect itself.

 

5-cell Pack Setup

Parts List

(5) AA alkaline batteries
(1) 5-cell battery pack

  • Load the batteries into the battery pack. 
  • Plug the battery pack into the Arduino’s power jack. When you are done, it should resemble the picture below.
  • Skip to Centering the Servos [4].

5-cell AA battery pack setup for the BOE Shield-Bot

4-cell Pack + Boe-Boost Setup

Parts List

(1) Boe-Boost
(1) 4-cell battery pack
(5) AA alkaline batteries

4-cell AA battery pack, Boe-Boost module, and 5 AA batteries

  • Load the batteries into the battery pack and Boe-Boost. Align the batteries’ +/- markings with the +/- markings in the battery cases. (Negative (-) terminals connect to springs.) 

AA batteries in the 4-cell battery holder and Boe-Boost module, showing proper orientation

  • Use the figure below as a guide and lift two of the positive battery ends upward.  Lift each one just far enough to expose its terminal end.

Lift two batteries from the holder, getting ready to insert the Boe-Boost module

  • Push the Boe-Boost up against the positive terminals, then push down to insert it into the battery holder.

Inserting the Boe-Boost module into the 4-cell battery pack

  • Plug the battery pack into the Arduino module’s power jack.
  • Check your work; when you are done assembling the system it should resemble the picture below.

4-cell battery pack setup with Boe-Boost assembled and connected

Activity 5: Centering the Servos

In this activity, you will run a sketch that sends the “stay-still” signal to the servos.    You will then use a screwdriver to adjust the servos so that they actually stay still. This is called centering the servos. After the adjustment, you will run test sketches that will  turn the servos clockwise and counterclockwise at various speeds.

Tool Required

You’ll need a Phillips #1 point screwdriver with a 1/8″ (3.18 mm) or smaller shaft.

Parallax screwdriver, #1 dual Phillips and flathead

Sending the Center Signals

If a servo has not yet been centered, it may turn, vibrate, or make a humming noise when it receives the “stay-still” signal.  

  • Reconnect your programming cable, and re-run LeftServoStayStill.
  • Set the BOE Shield’s Power switch to 2, to provide power to the servos.

3-position switch on the BOE Shield set to 2, to power the servo ports

  • Use a screwdriver to gently adjust the potentiometer in the servo as shown in Figure 2‑26.  Don’t push too hard!  Adjust the potentiometer slightly until you find the setting that makes the servo stop turning, humming or vibrating.

Insert a Phillips screwdriver into the access port on the continous rotation servo's case to adjust the potentiometer inside

  • Verify that the pin 13 LED signal monitor circuit is showing activity.  It should glow like it did when you ran LeftServoStayStill the first time.

What’s a Potentiometer?
A potentiometer is kind of like an adjustable resistor with a moving part, such as a knob or a sliding bar, for setting the resistance. The Parallax continuous rotation servo’s potentiometer is a recessed knob that can be adjusted with a small Phillips screwdriver tip.  Learn more about potentiometers in What’s a Microcontroller? and Basic Analog and Digital at www.parallax.com. 

Your Turn – Center the Servo Connected to Pin 12

  • Repeat the process for the pin 12 servo using the sketch RightServoStayStill.
/*
 Robotics with the BOE Shield – RightServoStayStill
 Transmit the center or stay still signal on pin 12 for center adjustment.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoRight;                            // Declare right servo

void setup()                                 // Built-in initialization block
{
  servoRight.attach(12);                     // Attach right signal to pin 12
  servoRight.writeMicroseconds(1500);        // 1.5 ms stay still signal
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Activity 6: Testing the Servos

There’s one last thing to do before assembling your BOE Shield-Bot, and that’s testing the servos.  In this activity, you will run sketches that make the servos turn at different speeds and directions.  This is an example of subsystem testing—a good habit to develop.

Subsystem testing is the practice of testing the individual components before they go into the larger device.  It’s a valuable strategy that can help you win robotics contests.  It’s also an essential skill used by engineers to develop everything from toys, cars, and video games to space shuttles and Mars roving robots.  Especially in more complex devices, it can become nearly impossible to figure out a problem if the individual components haven’t been tested beforehand.  In aerospace projects, for example, disassembling a prototype to fix a problem can cost hundreds of thousands, or even millions, of dollars.  In those kinds of projects, subsystem testing is rigorous and thorough.

Pulse Width Controls Speed and Direction

This timing diagram shows how a Parallax continuous rotation servo turns full speed clockwise when you send it 1.3 ms pulses.  Full speed typically falls in the 50 to 60 RPM range.

Timing diagram for signal to turn a Parallax continuous rotation servo full-speed clockwise, 1.3 ms high, 20 ms low

What’s RPM?  Revolutions Per Minute—the number of full rotations turned in one minute.

What’s a pulse train?  Just as a railroad train is a series of cars, a pulse train is a series of pulses (brief high signals).

Example Sketch: LeftServoClockwise

  • Enter, save, and upload LeftServoClockwise.
  • Verify that the servo’s horn is rotating between 50 and 60 RPM clockwise.
/*
 Robotics with the BOE Shield – LeftServoClockwise
 Generate a servo full speed clockwise signal on digital pin 13.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left servo

void setup()                                 // Built in initialization block
{
  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoLeft.writeMicroseconds(1300);         // 1.3 ms full speed clockwise
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Your Turn: Left Servo Counterclockwise

  • Save LeftServoClockwise as LeftServoCounterclockwise.
  • In servoLeft.writeMicroseconds, change (1300) to (1700).
  • Save the modified sketch and upload it to the Arduino.
  • Verify that the servo connected to pin 13 now rotates the other direction, which should be counterclockwise, at about 50 to 60 RPM.

Timing diagram for signal to turn a Parallax continuous rotation servo full speed counterclockwise, 1.7 ms high, 20 ms low

Example Sketch: RightServoClockwise

  • Save LeftServoClockwise as RightServoClockwise.
  • Replace all instances of servoLeft with servoRight.
  • Replace all instance of 13 with 12.
  • Run the sketch and verify that the pin 12 servo is rotating between 50 and 60 RPM clockwise.
/*
 Robotics with the BOE Shield – RightServoClockwise
 Generate a servo full speed clockwise signal on digital pin 12.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoRight;                            // Declare left servo

void setup()                                 // Built in initialization block
{
  servoRight.attach(12);                     // Attach left signal to pin 12
  servoRight.writeMicroseconds(1300);        // 1.3 ms full speed clockwise
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Your Turn – Right Servo Counterclockwise

  • In servoRight.writeMicroseconds change (1300) to (1700).
  • Save the sketch and upload it to your Arduino.
  • Verify that the pin 12 servo turns full-speed counterclockwise, about 50 to 60 RPM.

Controlling Servo Speed and Direction

For BOE Shield-Bot navigation, we need to control both servos at once.

  • Enter, save, and upload ServosOppositeDirections to the Arduino.
  • Verify that the servo connected to pin 13 turns counterclockwise and the one connected to pin 12 turns clockwise.

Example Sketch: ServosOppositeDirections

 /*
 Robotics with the BOE Shield – ServosOppositeDirections
 Generate a servo full speed counterclockwise signal with pin 13 and
 full speed clockwise signal with pin 12.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left servo signal
Servo servoRight;                            // Declare right servo signal

void setup()                                 // Built in initialization block
{
  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                     // Attach right signal to pin 12

  servoLeft.writeMicroseconds(1700);         // 1.7 ms -> counterclockwise
  servoRight.writeMicroseconds(1300);        // 1.3 ms -> clockwise
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

This opposite-direction control will be important soon.  Think about it: when the servos are mounted on either side of a chassis, one will have to rotate clockwise while the other rotates counterclockwise to make the BOE Shield-Bot roll in a straight line. Does that seem odd?  If you can’t picture it, try this:

  • Hold your servos together back-to-back while the sketch is running.

Pulse Width Modulation 
Adjusting the property of a signal to carry information is called modulation.  We’ve discovered that servo control signals are a series of high pulses separated by low resting states. How long the high pulse lasts—how wide the high pulse looks in a timing diagram—determines the speed and direction that the servo turns.  That adjustable pulse width carries the servo setting information. Therefore, we can say that servos are controlled with pulse width modulation.

Different combinations of writeMicroseconds us parameters will be used repeatedly for programming your BOE Shield-Bot’s motion.   By testing several possible combinations and filling in the Description column of Table 2‑2, you will become familiar with them and build a reference for yourself.  You’ll fill in the Behavior column later on, when you see how the combinations make your assembled BOE Shield-Bot move.

  • Save a copy of the ServosOppositeDirections sketch as TestServosTable2_2.
  • Test each combination of values in servoLeft.WriteMicroseconds(us) and servoRight.writeMicroseconds(us) and record your results in Table 2‑2—a PDF of this table is included with this chapter's code download zip archive.

Table for recording servo speed and direction with different pulse widht combinations

How To Control Servo Run Time

It’s easy to control how long the servos run when using the Servo library. Once set, a servo will maintain its motion until it receives a new setting. So, to make a servo run for a certain length of time, all you have to do is insert a delay after each setting. 

Example Sketch: ServoRunTimes

  • Enter, save, and upload ServoRunTimes to your Arduino.
  • Verify that both servos turn full speed clockwise for 3 seconds, then counterclockwise for 3 seconds, then stop.
/*
 Robotics with the BOE Shield – ServoRunTimes
 Generate a servo full speed counterclockwise signal with pin 13 and
 full speed clockwise signal with pin 12.
 */

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left servo signal
Servo servoRight;                            // Declare right servo signal

void setup()                                 // Built in initialization block
{
  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                     // Attach right signal to pin 12

  servoLeft.writeMicroseconds(1300);         // Pin 13 clockwise
  servoRight.writeMicroseconds(1300);        // Pin 12 clockwise
  delay(3000);                               // ..for 3 seconds
  servoLeft.writeMicroseconds(1700);         // Pin 13 counterclockwise
  servoRight.writeMicroseconds(1700);        // Pin 12 counterclockwise
  delay(3000);                               // ..for 3 seconds
  servoLeft.writeMicroseconds(1500);         // Pin 13 stay still
  servoRight.writeMicroseconds(1500);        // Pin 12 stay still
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Chapter 2 Summary

The focus of this chapter was calibrating and testing the servos, and building indicator lights to monitor the servo signals.  In addition to some hardware setup, many concepts related to electronics, programming, and even a few good engineering concepts were introduced along the way.

Hardware Setup

  • How to mount the Board of Education Shield on your Arduino module
  • How and why to provide an external battery power supply for the system
  • How to connect the servos to the Board of Education Shield

Electronics

  • What a resistor does, what its schematic symbol looks like, and how to read its value by decoding the color-bands on its case
  • What tolerance is, in relation to a resistor’s stated value
  • What an LED does, what the schematic symbol for this one-way current valve looks like, and how to identify its anode and cathode
  • What a solderless breadboard is for, and how it connects electronic devices
  • How to build LED indicator light circuits
  • The parts of a servo motor, how to connect it to the Board of Education Shield
  • What a potentiometer is, and how to calibrate a Parallax continuous rotation servo by adjusting its potentiometer
  • What a pulse train is, and how to control a servo with pulse width modulation

Programming 

  • How to use the Arduino’s pinMode and digitalWrite functions to send high and low output signals
  • How to use #include to add the Servo library to a sketch
  • Using the Servo library’s functions to attach and control a servo
  • Writing sketches to control the servo’s speed, direction, and run time

Engineering

  • Building a circuit on a solderless prototyping area
  • Using an indicator light to monitor communication between devices
  • What subsystem testing is, and why it is important
  • Creating a reference table of input parameters and how they affect a device’s output

Chapter 2 Challenges

Questions

  1. How do you connect two leads together using a breadboard?
  2. What function sets a digital pin’s direction? 
  3. What function sets pin 13 to 5 V?  What function sets it to 0 V?
  4. How can a sketch control the duration of a 5 V signal?
  5. What are the pulse durations that tell a continuous rotation servo to turn
    1. full speed clockwise,
    2. full speed counterclockwise,
    3. stay still.
  6. Which call would make a servo turn faster?
    1. servoLeft.writeMicroseconds(1440) or
    2. servoLeft.writeMicroseconds(1420).  Why?
  7. How can a sketch control the duration of a certain servo signal?

Exercises

  1. Write a loop function that makes an LED blink 5 times per second, with an on time that’s 1/3rd of its off time. (Disconnect the servos for this exercise!)
  2. Write a setup function that makes the pin 13 servo turn full speed clockwise for 1.2 seconds, while the pin 12 servo stays still.  After that, set both servos to stop.
  3. Write a setup function that makes one servo turn the same direction for 3 seconds.  The other servo should turn the opposite direction for the first 1.5 seconds and the same direction for the second 1.5 seconds.  Then, make both servos stop.

Projects

  1. Look up the servo library’s detach function and use it in place of servoLeft and servoRight.writeMicroseconds(1500) to stop servos after they turn for 3 seconds.
  2. Write a program that makes the pin 13 servo turn counterclockwise while the pin 12 servo turns clockwise.  After 3 seconds, make both servos turn counterclockwise for 0.6 seconds.  Then, make both turn clockwise for 0.6 seconds.  Then, make the pin 13 servo turn clockwise and the pin 12 servo turn counterclockwise for 3 seconds.

Chapter 2 Solutions

Question Solutions

  1. Plug the two leads into the same 5-socket row on the breadboard.
  2. The pinMode function. 
  3. The digitalWrite function does both, depending on its value parameter:
digitalWrite(13, HIGH) // 5 V
digitalWrite(13, LOW)  // 0 V
  1. Assuming a pin has just been set high, the delay call can keep it high for a certain amount of time.  Then, a digitalWrite call can set it low.
  2. (a)1.3 ms pulses for full speed clockwise, (b)1.7 ms pulses for full speed clockwise, and (c)1.5 ms pulses for stay still.
  3. (b) servoLeft.writeMicroseconds(1420).  Full speed clockwise is servoLeft.writeMicroseconds(1300), and stop is servoLeft.WriteMicroseconds(1500).  Since 1420 is further from stop and closer to full speed, it’s the correct value for faster clockwise rotation even though it is smaller.
  4. Servo.writeMicroseconds(value) followed by delay(ms) followed by Servo.writeMicroseconds(newValue) or Servo.detach(pin) will keep the servo turning for ms milliseconds.

Exercise Solutions

  1. The total on + off time has to be 200 ms, which is 1/5th of a second.  So, on for 50 ms, off for 150 ms:
void loop()                    // Main loop auto-repeats
{                              // 200 ms -> 5 blinks/second
  digitalWrite(13, HIGH);      // Pin 13 = 5 V, LED emits light
  delay(50);                   // ..for 0.05 seconds
  digitalWrite(13, LOW);       // Pin 13 = 0 V, LED no light
  delay(150);                  // ..for 0.15 seconds
}
  1. Set pin 13 servo to full speed clockwise and the pin 12 servo to stop.  Then, delay for 1200.  Since servoRight is already stopped, all the code has to do is stop servoLeft.
void setup()                   // Built in initialization block
{
  servoLeft.attach(13);        // Attach left signal to pin 13
  servoRight.attach(12);       // Attach right signal to pin 12

  servoLeft.writeMicroseconds(1300);  // 1.3 ms -> clockwise
  servoRight.writeMicroseconds(1500); // 1.5 ms -> stop
  delay(1200);                        // ..for 1.2 seconds
  servoLeft.writeMicroseconds(1500);  // 1.5 ms -> stop
}
  1. In this example, the pin 13 servo starts counterclockwise and the pin 12 servo starts out clockwise.  This goes on for 1.5 seconds.  Then, the pin 12 servo is changed to counterclockwise, and this goes on for another 1.5 seconds.  After that, both servos are stopped.
void setup()                   // Built in initialization block
{
  servoLeft.attach(13);        // Attach left signal to pin 13
  servoRight.attach(12);       // Attach right signal to pin 12

  servoLeft.writeMicroseconds(1700);  // 1.7 ms -> cc-wise
  servoRight.writeMicroseconds(1300); // 1.3 ms -> clockwise
  delay(1500);                        // ..for 1.5 seconds
  servoRight.writeMicroseconds(1700); // 1.7 ms -> cc-wise
  delay(1500);
  servoLeft.writeMicroseconds(1500);  // 1.5 ms -> stop
  servoRight.writeMicroseconds(1500); // 1.5 ms -> stop
}

Project Solutions

  1. The detach function detaches the instance of Servo from its pin.  This sketch verifies that it stops the servo after 3 seconds of run time.  The chapter examples sent pulses telling the servo to stay still.  In contrast, detach stops sending signals to the servo—the pin doesn’t tell the servo to do anything, so it goes dormant instead of holding the “stop speed.”  The end result is the same, the servo motor stops.  The advantage to detach is that it prevents the servos from turning slowly if the servo is not precisely calibrated.
/*
 Robotics with the BOE Shield – Chapter 2, Project 1
 Generate a servo full speed counterclockwise signal with pin 13 and
 full speed clockwise signal with pin 12.
 */

#include <Servo.h>                    // Include servo library
 
Servo servoLeft;                      // Declare left servo signal
Servo servoRight;                     // Declare right servo signal

void setup()                          // Built in initialization block
{
  servoLeft.attach(13);               // Attach left signal to pin 13
  servoRight.attach(12);              // Attach right signal to pin 12

  servoLeft.writeMicroseconds(1700);  // Pin 13 counterclockwise
  servoRight.writeMicroseconds(1300); // Pin 12 clockwise
  delay(3000);                        // ..for 3 seconds
  servoLeft.detach();                 // Stop servo signal to pin 13
  servoRight.detach();                // Stop servo signal to pin 12
}  
 
void loop()                           // Main loop auto-repeats
{                                     // Empty, nothing needs repeating
}
  1. Solution is the sketch ForwardLeftRightBackward, from a later chapter.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2023


Source URL: https://learn.parallax.com/tutorials/robot/shield-bot/robotics-board-education-shield-arduino/chapter-2-shield-lights-servo-21

Links
[1] https://learn.parallax.com/sites/default/files/content/shield/robo_ch2/code/RoboticsBOEShield_Ch2_20120229.zip
[2] http://learn.parallax.com/tutorials/robot/shield-bot/boe-bot-shield-bot-retrofit-kit-arduino-uno
[3] https://learn.parallax.com/184
[4] https://learn.parallax.com/185