User Manual For The Arduino Kit V5.5
User Manual For The Arduino Kit V5.5
USER MANUAL
SOUTH DAKOTA SCHOOL OF MINES & TECHNOLOGY
MECHANICAL ENGINEERING DEPARTMENT
This manual does not assume that the reader has much familiarity with the Arduino hardware or with
the Arduino IDE (Integrated Development Environment), which is the programming environment for
Arduino. However, the reader is referred to the many books and web sites that are available to learn
about the Arduino Uno and the Arduino IDE since this document is not intended to be a substitute for
those references.
Finally, there is a companion PowerPoint presentation that instructors that adopt the kit can use in class
and serves as a supplement for some of the topics discussed in this document.
ACKNOWLEDGMENTS
This document was prepared as part of a project supported by the Office of Naval Research under
Award No. N00014-15-1-2419. The views and conclusions contained in this document are those of the
authors and should not be interpreted as representing the official policies, either expressed or implied,
of the Office of Naval Research or the U.S. Government.
Contents ii
CONTENTS
CHAPTER 1 Hardware Components ............................................................................................................ 1
2.2.3 Linux............................................................................................................................................ 12
3.3 Adding the Grove Starter Kit Sketchbook and Library to the Arduino IDE ...................................... 21
4.1.1 Adding the Library Required for Using the Adafruit Motor/Stepper/Servo Shield .................... 40
4.2.1 Adding the Library Required for Using the Adafruit PWM/Servo Shield .................................. 50
4.3 SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller ........................................ 59
4.3.2 Adding the Library Required for Using the SparkFun USB Host Shield ................................... 59
4.3.5 Using the SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller ................ 63
APPENDIX A – Pictures Showing the Layout of the Arduino Uno and the Shields in the Kit ............... 74
REFERENCES .......................................................................................................................................... 88
Error! Use the Home tab to apply 1
HeGeneral Information About the
Hardware Kitading 3 to the text that you
HARDWARE
COMPONENTS
General Information About the Hardware Kit 2
HARDWARE COMPONENTS
OBJECTIVES
■ Introduce the Components of the Arduino Kit
1.1.1 Components
The electronic components corresponding to the kit are listed below. These are the minimum
components that are required. You can add more sensors and actuators as needed.
General Information About the Hardware Kit 3
Microcontroller
• Arduino UNO Rev3
Shields
• Grove Base Shield (comes with the Grove Starter Kit)
• Adafruit Motor/Stepper/Servo Shield
• Adafruit 16-Channel 12-bit PWM/Servo Shield
• SparkFun USB Host Shield
Other Components
• 3x4 Right Angle Male Header
• USB Mini-B Cable - 6 Foot
• Battery pack (12V) with on/off switch:
• USB A to B Cable - 6 Foot
8AA batteries, bare ends
• Sony Dual Shock 3, PS3 controller
• Battery pack (12V) with on/off switch:
• Bluetooth 4.0 USB Module
8AA batteries, barrel jack plug (5.5mm /
• Arduino Stackable Header Kit
2.1mm)
• Arduino Uno Plastic Enclosure
• Battery pack (6V) with on/off switch:
4AA batteries, bare ends
The following figures show the different components of the Arduino kit.
General Information About the Hardware Kit 4
Microcontroller
Shields
Figure 1-10. Buzzer. Figure 1-11. Sound Sensor. Figure 1-12. Touch Sensor.
Figure 1-16. Light Sensor. Figure 1-17. Button. Figure 1-18. Cables.
Other Components
Appendix B provides a list of the components mentioned in this chapter indicating a possible supplier
for each one as well as its estimated unit cost. Some assembly/soldering is needed to get the Adafruit
Motor/Stepper/Servo Shield, the Adafruit 16-Channel 12-bit PWM/Servo Shield, and the SparkFun
USB Host Shield ready for use with the Arduino UNO. Appendix C shows the assembly/soldering
steps required for each of those shields.
General Information About the Hardware Kit 7
CHAPTER 2
GETTING
STARTED
Overview of the Arduino UNO Rev3 8
GETTING STARTED
OBJECTIVES
■ Provide a general overview of the Arduino Uno
■ Learn how to power the Arduino Uno
■ Understand how to upload code to the Arduino
The digital pins are the digital inputs and outputs of the Arduino. These are where you connect items
such as buttons, LEDs, sensors, and others to interface the Arduino with other pieces of hardware. Pins
marked with a tilde (~) (which are also called PWM pins) can be used to approximate an analog output,
which you can use to dim LEDs or run servo motors or DC motor drivers.
There are six analog inputs on the analog header. These pins have analog-to-digital converters (ADC),
which can be used to read in an analog voltage between 0 and 5V. These are useful if you need to read
the output of analog sensors. All six analog pins can also serve as digital inputs and outputs.
The power header consists of voltage supply pins. These pins are used as power sources for other
pieces of hardware (like LEDs, potentiometers, and other circuits). The ‘3.3V’ and ‘5V’ pins are
regulated 3.3V and 5V voltage sources. The ‘GND’ pins are the common ground. ‘VIN’ is the input
voltage; it will equal the voltage of your input supply if you have a wall adapter connected. If nothing
is connected to the barrel jack, and you are powering the board via USB, ‘VIN’ should be around 5V.
Overview of the Arduino UNO Rev3 9
The Arduino can be powered via either the USB or barrel jack connectors. If you choose to power it
via USB, the other end of the USB cable can be connected to either a computer as shown in Figure 2-2
or a 5V regulated USB wall charger. USB is usually the easiest way to power the board, especially
when you are programming it, because the USB interface with the PC is required for uploading the
code. However, its limited power can become a problem for some applications like driving a motor.
The power jack in the Arduino accepts a center-positive barrel jack connector with an outer diameter
of 5.5mm and inner diameter of 2.1mm. 9V and 12V adapters are good choices if you are looking to
power the Arduino through the barrel jack connector. Any wall adapter connected to this jack should
supply a DC voltage between 7V and 12V. You can also use a battery pack as shown in Figure 2-3.
Overview of the Arduino UNO Rev3 10
Figure 2-3. Powering the Arduino using a barrel jack, 12V 8AA battery pack.
You need to use the barrel jack when you need more power than the USB port can supply. A USB port
is usually only allowed to supply 500mA; if you need more than that, the barrel jack is the only choice.
We can connect both a barrel jack and a USB connector at the same time as shown in Figure 2-4; the
Arduino has power control circuitry to automatically select the best power source.
Installing the Arduino IDE Software 11
Figure 2-4. Powering the Arduino using both a USB cable and a battery pack.
When plugging the Arduino to your PC for the first time, your computer will apply the compatible
drivers. You should see a notification like the one shown in Figure 2-5.
2.2.2 Mac
Before you plug in the Arduino to your PC, you need to install the Arduino IDE.
• Download the software from https://www.arduino.cc/en/Main/Software which is only offered
in a .zip file format.
• Double-click the .zip file to unzip it and you will see an "Arduino.app" file.
• Copy the Arduino application into your applications folder to complete installation. For
details see https://www.arduino.cc/en/Main/Software.
2.2.3 Linux
Before you plug in the Arduino to your PC, you need to install the Arduino IDE. There are many
different distribution “flavors” of Linux. Installing Arduino is slightly different for many of these
distributions and some already have the required software installed. Follow the procedure given in
https://www.arduino.cc/en/Main/Software.
Figure 2-8. Selecting the COM port where the Arduino is connected.
Figure 2-9. Message indicating that the code was successfully uploaded to the Arduino.
If you see an error message in the console window, it means something went wrong. There are some
things you can check such as making sure that you selected the correct board and COM port.
Using the Serial Monitor 16
EXAMPLE 2.1
Serial Monitor Test
1. Connect the Arduino to your PC as shown in Figure 2-10.
2. Open the Arduino IDE software and delete everything on the default sketch.
3. Copy the code presented below and paste it in the code area of the Arduino IDE.
4. Save the code as SerialPortTest.ino.
5. Make sure that the appropriate Arduino board is selected.
6. Make sure that the correct COM port is selected.
7. Upload the code to the Arduino.
8. Open the Serial Monitor indicated by #6 in Figure 2-6, and make sure that baud rate
matches the one highlighted in yellow in the code below.
/*
SerialPortTest.ino
*/
/* Serial Port Test code*/
void setup()
{
/* put your setup code here, to run once: */
Serial.begin(115200);
Serial.println("Serial port is open");
Using the Serial Monitor 17
void loop()
{
/* put your main code here, to run repeatedly: */
Serial.println("Serial port Test");
delay(500);
}
If you see the requested messages in the Serial Monitor (see Figure 2-11), the serial
communication was successful.
EXAMPLE 2.2
Blink the built-in Arduino LED
This example will blink the built-in LED that is connected to digital pin 13. The LED will
blink every half a second and the number of blinks will be displayed in the Serial Monitor. For
the time being, don’t worry about the specific commands that are used in the code. You will
become acquainted with them in the next chapter. Instead of copying and pasting the code in
the Arduino IDE, try typing it.
int counter = 0;
int pin= 13;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
}
void loop()
{
// put your main code here, to run repeatedly:
counter = counter + 1;
digitalWrite(pin,HIGH);
Serial.print("Blink #");
Serial.println(counter);
delay(500);
digitalWrite(pin,LOW);
delay(500);
}
Using the Arduino Built-In LED 19
CHAPTER 3
INTRODUCTION
TO THE GROVE
BASE SHIELD
OBJECTIVES
■ Become acquainted with the Grove Starter Kit
■ Learn about digital and analog signals
■ Understand the concept of Pulse Width Modulation (PWM)
3.1 Introduction
A quick search on the Internet reveals that using a microcontroller, such as the Arduino UNO, is only
limited by the level of knowledge of the person using it. Coding for microcontrollers can range from
simple to very complex. A good way to start learning how to write code for microcontrollers is to select
a “friendly” hardware platform, begin with very simple programs and progressively increase the level
of difficulty.
As shown in Figure 3-1, there are 8 digital ports (one is labeled UART and the other seven are labeled
D2 to D8) that are connected to digital pins 0/1 through 8/9 on the Arduino Uno. They are used when
reading a digital sensor that only outputs 0 or 1 (5V) or when writing to a digital device. The digital
pins 0 and 1 are reserved for UART (an integrated circuit used for serial communication over a
computer or peripheral device serial port). On the left-hand-side there are four Grove ports (labeled A0
Adding the Grove Starter Kit Sketchbook and Library to the Arduino IDE 21
to A3) for taking analog readings. Analog sensors can return readings ranging from 0 to 1023. Below
the Grove digital ports there are four I2C Grove ports (labeled I2C). I2C is a low-speed bus protocol
that transfers data via two wires: SCL and SDA. SCL is the clock line that synchronizes data transfer
over the I2C bus, and the SDA is the data line. For details about this shield go to the following link:
http://www.seeedstudio.com/wiki/Base_shield_v2.
Each of the Grove connectors has four lines: normally two signal lines, power and ground. Sensors
normally need at least one signal line and the power and ground lines. This system eliminates the need
for external wiring. The lowest digital pin number listed in each Grove plug/socket is the one that is
connected to the sensor. For example, if you want to connect the sensor to digital pin 2, you should
connect it to D2 in the Grove Base Shield. Similarly, digital pin 3 is connected to D3 in the Grove Base
Shield.
In this section, we will show how to add the sketchbook and library that comes with the Grove Starter
Kit to the Arduino IDE and how to access the example code available for each component. In contrast
Adding the Grove Starter Kit Sketchbook and Library to the Arduino IDE 22
to the normal way of adding Libraries to the Arduino IDE (i.e., download a Library → put it into the
Arduino libraries directory → open an example and use it), we directly use a sketchbook provided by
the supplier (Seeed Studio) to avoid the risk of compilation errors due to missing libraries. You can
find out how to use the sketchbook that is available for the Grove Starter Kit at the following web site:
http://wiki.seeedstudio.com/wiki/How_To_Use_Sketchbook.
Follow the steps below to add the sketchbook and library to your IDE and use the example code.
Figure 3-2. Selecting the sketchbook provided with the Grove Starter Kit as the default sketchbook.
4. Click Browse, locate the sketchbook folder you saved earlier, and click OK (see Figure 3-3).
23
5. Now the sketchbook is ready to use. To access the examples, click File on the menu bar and place
the mouse cursor over the Sketchbook menu option to see all the available examples (see Figure
3-4).
Figure 3-4. Access the example code for the components in the Grove Base Shield.
Installing the Grove Base Shield 24
o Double check that all the headers/pins are properly attached to the shield. It is possible that you
could get a version of the Grove Base Shield that does not have the headers/pins attached. In that
case, please refer to “APPENDIX C - Required Hardware Preparations” for instructions on how to
attach the headers/pins.
o Also, now is a good time to check that all the pins are straight. If you see that any of the pins are
not straight (see example in Figure 3-5 – the picture corresponds to a different shield), straighten
them out before installing the shield. The best way to straighten a pin is to carefully use needle nose
pliers to bend it back to a straight position.
Figure 3-5. Watch out for bent pins, like the one pictured above, before installing shields.
o Check to make sure that all the pins are present. It is easy to break a pin and not notice the missing
pin when installing the shield. This, of course, will lead to components not working properly.
To install the shield, first align all the pins into the headers, as shown in Figure 3-6.
Installing the Grove Base Shield 25
Figure 3-6. Aligning the pins before pushing them into the headers.
Next, gently push on both sides until the shield is inserted into the headers, as shown in Figure 3-7. It
is crucial that the pins in the shield slide into the headers evenly because if the shield is not pushed
down evenly the pins could bend.
Figure 3-7. Pushing the pins in the shield into the headers.
Digital Input and Output 26
EXAMPLE 3.1
Digital Output: Blinking LED
In this example, we will demonstrate how to use the Grove LED module with the Grove Base
Shield and the Arduino UNO. The program presented below will have an LED flash on and off
every second. To connect the LED to the Arduino and flash it on and off, follow the steps below.
1. Make sure that the voltage selector in the Grove Base Shield is set to 5V. (Note: the shield
has a switch next to the “A0” socket that can be used to select either 3.3V or 5V).
2. Prepare the LED module for use. The longer “leg” of the LED needs to be connected to
the “+” side. Also, the module has a variable resistor (it has a yellow “cap”) that can be
adjusted with a Phillips screwdriver. The variable resistor controls the amount of current
that passes through the LED and, consequently, its brightness when it is turned on.
3. Connect one end of a Grove cable (the cable is polarized. i.e. it will go into the socket in
only one way) to the Grove LED Module, as shown by #1 in Figure 3-8.
4. Connect the other end of the Grove cable to the socket labeled D2 (which is the second
digital pin) on the Grove Base Shield, as shown by #2 in Figure 3-8.
5. Connect the Arduino board to the computer via a USB cable.
6. Open the Arduino software, delete everything on the default sketch, copy the code
provided on the next page (the code is located below Figure 3-8), and save the sketch as
blink.ino.
Digital Input and Output 27
/* blink.ino */
void setup()
{
pinMode(led_pin,OUTPUT);/* Set the led_pin as output*/
}
void loop()
{
digitalWrite(led_pin,HIGH);/* turn the LED on (HIGH) */
delay(delayTime); /* wait for a second*/
digitalWrite(led_pin,LOW );/* turn the LED off (LOW) */
delay(delayTime); /* wait for a second*/
}
EXAMPLE 3.2
Digital Input: Push Button
A push button is a component that connects two points in a circuit when you press it. The push
button used in this case is normally open which means that when you press the button, it will
close the circuit. Follow the steps below to connect the Grove push button and the Grove LED
module to the Grove Base Shield, and to turn on the LED when you press the button.
Digital Input and Output 29
1. Connect one end of a Grove cable to the Grove push button module and the other end to
the socket labeled D3 as shown in Figure 3-10.
2. Connect one end of a Grove cable to the LED module and the other end to the socket
labeled D7 as shown in Figure 3-10.
Figure 3-10. Connecting the push button module to the LED module.
3. Copy and paste the code below to a new sketch in the Arduino IDE and save the sketch
as button.ino.
/*
button.ino
Define the pins to which the button and LED are connected.
*/
const int pinButton = 3;
const int pinLed = 7;
void setup()
{
/* Configure the button's pin for input signals. */
pinMode(pinButton,INPUT);
/* Configure the LED's pin for output. */
pinMode(pinLed,OUTPUT);
}
void loop()
{
if(digitalRead(pinButton))
{
/* When the button is pressed, turn the LED on. */
digitalWrite(pinLed,HIGH);
}
else
{
Digital Input and Output 30
SUGGESTED PRACTICE
EXERCISE 3.1: Traffic light control
Use three LED modules to mimic a traffic light. Turn the green LED on for 5 seconds, followed
by the blue LED for 2 seconds, and finally the red LED for 5 seconds.
EXERCISE 3.2: Use the digital input to control the states of the digital outputs
Use a push button to control the state of two LEDs. When the button is pressed turn the red LED
ON and the green LED OFF. When the button is not pressed, the green LED should be ON and
the red LED should be OFF.
PWM (“Analog”) Output 31
EXAMPLE 3.3
PWM Output: Change the brightness of an LED
The brightness of an LED can be modified by changing the PWM duty cycle. In this example,
we will send a PWM signal to digital pin 6.
1. Connect the Grove LED module to socket D6 with a Grove cable (see Figure 3-11).
2. Copy and paste the code below to a new sketch in the Arduino IDE and save the sketch
as pwm.ino.
3. Upload the code to the Arduino.
/* pwm.ino
Generate a PWM signal to pin 6
*/
int pwmPin= 6;
int MAX_PWM_LEVEL = 255; /* value can change between 0-255*/
int value = 0;
void setup()
{
/* Configure the serial communication line at 115200 bauds (bits
per second.) */
Serial.begin(115200);
Serial.print("PWM value set for pin,");
Serial.println(pwmPin);
}
void loop()
{
value += 50;
if (value > MAX_PWM_LEVEL)
{
value = 0;
}
analogWrite(pwmPin, value);
Serial.println(value); /* print PWM*/
delay(500); /* delay half a second*/
}
4. Open the Serial Monitor and observe that the PWM value is changing by 50 every half a
second. After the value is equal to 250, it is reset to 0.
Note: A PWM value of 0 corresponds to 0V and a value of 255 corresponds to 5V.
SUGGESTED PRACTICE
EXERCISE 3.3: Change the intensity of two LED’s
Change the intensity of two LED’s using PWM. Use the red and green LEDs. Change the
intensity of the red LED from dim to bright and the intensity of the green LED from bright to
dim at the same time. Observe the effect of using smaller and larger step values for changing the
PWM value.
Analog Input 33
EXAMPLE 3.4
Analog Input: Temperature Sensor
Temperature sensing is a common application in embedded systems. The temperature sensor
included in the Grove Starter Kit is a thermistor that can be easily used to measure ambient
temperature. This program will read the analog value provided by the temperature sensor
included in the Grove Starter Kit, convert it to a temperature value in degrees Celsius using
appropriate equations, and print the temperature value to the Serial Monitor. Follow the steps
below to learn how to use the temperature sensor.
1. Connect the temperature sensor to the analog pin A0 as shown in Figure 3-12.
Figure 3-12. Grove temperature sensor connected to the analog pin A0.
2. We need to define the “B value” of the thermistor in the code so that the Arduino can
report the correct temperature. The specifications listed in the product web site give a B
Analog Input 34
value range of 4,250K to 4,299K. Any value within that range can be used, but a good
rule of thumb is to use the central value, which in this case is about 4,275K.
Notes:
o The datasheet/specifications for the temperature sensor can be found at:
http://wiki.seeed.cc/Grove-Temperature_Sensor_V1.2/
o To learn more about what B values are and how they are calculated visit the web site:
http://www.electronics-tutorials.ws/io/thermistors.html
3. Copy and paste the code below in the Arduino IDE and save it as temp_sensor.ino.
/* temp_sensor.ino */
void setup()
{
Serial.begin(115200); /* Open serial port*/
}
void loop()
{
/* Get the analog value from the temperature sensor. */
int val=analogRead(pinTemp);
SUGGESTED PRACTICE
EXERCISE 3.4: Change the state of three LEDs using the reading from the temperature sensor
In this exercise, we will read the temperature sensor and use the temperature value to light up
different LEDs. Let’s assume that the ambient temperature is between 64 and 73 degrees
Fahrenheit. If the reading is within this range, turn ON the green LED. If it is less than 64 degrees
Fahrenheit turn ON the blue LED. If it is above 73 degrees Fahrenheit turn ON the red LED.
Now grab the temperature sensor with your fingers to test the high temperature case. Next, place
a cold item against the sensor to test the cold temperature case.
I2C Communication 36
EXAMPLE 3.5
I2C Communication: Grove LCD RGB Backlight
In this example, you will use the LCD module included in the Grove Starter Kit. The Grove LCD
has a backlight that uses the RGB (Red, Green, Blue) color model and can have 16,777,216 (i.e.,
256 x 256 x 256) different color values. Thus, you can basically set the color of the backlight to
whatever you like. With I2C, the number of pins required for data exchange and backlight control
shrinks from about 10 to 2.
1. Connect the Grove LCD RGB Backlight to one of the I2C sockets on the Grove Base
Shield, as shown in Figure 3-13.
Note: It doesn’t matter which I2C socket you decide to use.
2. Copy and paste the code below in the Arduino IDE and save it as Grove_RGB_LCD.ino.
Notes:
o The code uses two different libraries: Wire and rgb_lcd. The Wire library comes
standard with the base Arduino IDE installation. The rgb_lcd library is a contributed
library and must be present in the libraries folder of the sketchbook that you are
using.
o The difference between the <,> and the “,” in the #include statements in the code is
that the first (e.g., #include <Wire.h>) indicates that the library is in a standard
location whereas the second (e.g., #include "rgb_lcd.h") indicates that the library is
local.
I2C Communication 37
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
/* This is the color code. You can change it to whatever you want */
const int colorR= 255;
const int colorG= 0;
const int colorB= 0;
void setup()
{
/* set up the number of columns and rows in the LCD */
lcd.begin(16, 2);
lcd.setRGB(colorR,colorG,colorB);
/* Print a message to the LCD. */
lcd.print("Hello Friends!");
delay(1000);
}
void loop()
{
/* set the cursor to column 0, line 1 (note: line 1 is the
second row, since counting begins with 0): */
lcd.setCursor(0, 1);
/* print the number of seconds since reset: */
lcd.print(millis()/1000);
delay(100);
}
I2C Communication 38
SUGGESTED PRACTICE
EXERCISE 3.5: Make a color of the LCD fade or brighten
Change the value of red, green, or blue from 255 to 0 or from 0 to 255 to make the backlight
color fade or brighten.
EXERCISE 3.6: Change the color of the LCD using a push button
Use a push button to change the backlight color of the LCD in a random fashion. To do this,
generate a random number between 0 and 255 for each of the three colors: red, green, and blue.
Then, set the color of the backlight using those random numbers. The backlight color should
remain the same until the push button is pressed again.
NOTES:
o To practice and understand how the other sensors from the Grove Starter Kit work, you can go
through the short manual that comes with the kit and test the demo codes provided by the
manufacturer (Seeed Studio). All the demo codes can be accessed as shown in Figure 3-4.
o The next chapter explains how to use the additional hardware items that are not a part of the Grove
Starter Kit.
I2C Communication 39
CHAPTER 4
INTRODUCTION
TO THE OTHER
SHIELDS IN THE
KIT
Adafruit Motor/Stepper/Servo Shield 40
OBJECTIVES
■ Introduce the other shields in the kit
■ Learn how to use the other shields in the kit
■ Show the suggested way to stack the shields
In this chapter, we will be briefly go over the other shields that are part of the kit. Code examples are
provided to show the basic functions of each one. The information presented is only intended to help
you get started using the shields. Please refer to the documentation provided by the manufacturer of
each shield for additional details.
When shields are stacked, it is not possible to see the information printed on the boards. Appendix A
has pictures showing the layout of the Arduino Uno as well as the other shields in the kit. We suggest
that you have a hardcopy of Appendix A at hand when you are working with the shields in case you
need to see the layout of the boards.
Figure 4-1. Adding the library required by the Adafruit motor shield.
4. Click on the Add .ZIP Library… menu option and locate and select the folder where you saved
the library for the Adafruit motor shield.
5. Close and reopen the Arduino IDE and the library should be ready to use.
Notes:
o Every time you add a new library you need to close and reopen the Arduino IDE to be able to
start using it.
o If the library that you added is not showing in the Arduino IDE menu items related to libraries,
it means that the library was not installed correctly or that you forgot to restart the Arduino IDE.
o When you change the default sketchbook that the Arduino IDE is using, it is a good idea to
restart the Arduino IDE.
Additional Resources
To learn more about the motor shield and its associated library visit the following web sites:
o https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/using-dc-motors.
Adafruit Motor/Stepper/Servo Shield 42
o https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/library-reference.
The first aspect to consider is the voltage indicated in the motor specification/data sheet. The motor
controllers on the shield are designed to run between 5V and 12V. Commonly you will be using small
DC motors with the shield that need between 6V and 12V. In this regard, it is important to point out
that most 1.5V to 3V motors will not work with the shield.
The motor shield needs an external power supply to drive the motors. Here we will use a 12V battery
pack for that purpose. The battery pack that we selected uses eight 1.5V AA batteries connected in
series, has an on/off switch, a cover, and two lead wires with bare ends. We strongly suggest that you
use external power supplies that have an on/off switch and that you make sure that the switch is in the
off position when you are not driving motors with the shield or when you store the battery pack.
As shown in Figure 4-2, connecting the battery pack to the motor shield is relatively simple. However,
since you are working with DC, you need to be very careful with the polarity. The battery pack has a
positive (“+”) lead wire and a negative (“-”) lead wire. If they are not clearly marked in the battery
pack that you have, you can use a multimeter that is set to measure DC voltage to figure out which wire
is the “+” and which wire is the “-”. The information printed on the board of the motor shield clearly
indicates where the lead wires of the battery pack need to be connected. In particular, the board clearly
indicates the connection point for the “+” lead wire and for the “-” lead wire of the battery pack.
If you are not familiar with DC, there are many references on the subject available online. The company
SparkFun has different tutorials that you may find helpful. The following is a link to one of their
tutorials: https://learn.sparkfun.com/tutorials/voltage-current-resistance-and-ohms-law.
Adafruit Motor/Stepper/Servo Shield 43
EXAMPLE 4.1
Driving DC motors using the Adafruit Motor/Stepper/Servo Shield
In this example, we will drive two DC motors forward and backward.
Before we begin:
• Remove any shields that you may have on the Arduino.
• Carefully place the Adafruit Motor/Stepper/Servo Shield on the Arduino.
• Have two 12V battery packs ready: one with a barrel jack connector and one with lead wires
that have bare ends. The battery pack with the barrel jack connector will be used to power
the Arduino when the Arduino is not connected to a PC via a USB cable. The battery pack
with lead wires that have bare ends will be used to power the motor shield. We recommend
Adafruit Motor/Stepper/Servo Shield 44
that you use battery packs that have an on/off switch and that you keep the switch in the off
position when you are not using the Arduino and the motor shield.
• Select the DC motors that you will be controlling with the motor shield. One possible option
is to use VEX EDR 393 motors from VEX Robotics.
• The shield can control up to four DC motors. The places where motors can be connected to
the shield (i.e., the connection pins for motors) are clearly labeled on the board as “M1”,
“M2”, “M3”, and “M4”.
Follow the steps below to make the required hardware connections and upload the example
code to the Arduino.
1. Connect one DC motor to “M1” and one DC motor to “M2”, as shown in Figure 4-3.
From now on, we will refer to those two motors as Motor 1 and Motor2, respectively.
Notes:
o In order to drive motors bi-directionally, be sure to connect the motor leads between
the two terminals for each motor. DO NOT connect one of the leads to ground.
o Sometimes when you are working on a project the direction in which a motor is
rotating is the opposite of what you expected (i.e., the “forward” and “backward”
directions are reversed). To solve this problem, you only need to reverse the way in
which the lead wires of the motor are connected to the shield.
o If you want, you can connect two more DC motors to the shield: One to “M3” and
one to “M4”.
2. Connect the 12V external power supply to the motor shield (see Figure 4-2). The external
power supply is mainly used to power all the motors connected to the shield.
3. Do a final check to make sure that the motors and battery packs are connected as shown
in Figure 4-4.
Figure 4-4. Overview of how to connect the motors and battery packs to use the Adafruit
Motor/Stepper/Servo shield.
4. Copy and paste the code below in the Arduino IDE and save it as dcmotor.ino.
5. Upload the code to the Arduino.
Important Notes:
o The first three #include lines in the example code should always be added at the
beginning of programs that use the Adafruit Motor/Stepper/Servo shield. They include
all the libraries that are required to use the shield.
o The shield uses PWM to drive the motors.
Adafruit Motor/Stepper/Servo Shield 46
o The rotational speed of a motor is set by using the function setSpeed(speed) where the
value of the variable speed can range from 0 (stopped) to 255 (full speed). You can set
the speed of the motor to whatever value you want in that range. Note that you are not
actually setting the speed, but rather the PWM duty cycle that is sent to the motor. The
actual motor speed will depend on the load that the motor experiences.
o To direction of rotation of a motor is set using the function run(direction) where the
variable direction can take the following values: FORWARD, BACKWARD, or
RELEASE.
/*
dcmotor.ino
This code drives two dc motors forward and backward.
The general layout of functions that can be used to drive a vehicle
left, right, and to stop the vehicle are also included so that
you can use them in the future if you need them.
*/
#include<Wire.h>
#include<Adafruit_MotorShield.h>
#include"utility/Adafruit_MS_PWMServoDriver.h"
/* Create the motor shield object with the default I2C address */
Adafruit_MotorShield AFMS =Adafruit_MotorShield();
/* or, if you have more than one motor shield stacked on top of each
other, you can create the motor shield object with a different I2C
address */
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
void setup() {
Serial.begin(9600); /* set up the serial port at 9600 bps*/
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
AFMS.begin(); /* create with the default frequency of 1.6KHz */
//AFMS.begin(1000); /* OR with a different frequency, say 1KHz*/
Adafruit Motor/Stepper/Servo Shield 47
void loop() {
int Level;
int Speed[3] = {75, 150, 255};
/* Run forward and backward at each speed level */
for (Level = 0; Level < 3; Level++) {
Serial.print("Speed Level ");
Serial.println(Level);
forward(Speed[Level]); /* The forward function is defined below
*/
delay(1000);
Stop();
delay(1000);
backward(Speed[Level]); /* The backward function is defined
below */
delay(1000);
Stop(); /* The Stop function is defined below */
delay(2000);
}
}
void forward(int spd) /* Drive all the motors forward */
{
myMotor_1->setSpeed(spd);
myMotor_1->run(FORWARD);
myMotor_2->setSpeed(spd);
myMotor_2->run(FORWARD);
}
void backward(int spd) /* Drive all the motors backward */
{
myMotor_1->setSpeed(spd);
myMotor_1->run(BACKWARD);
myMotor_2->setSpeed(spd);
myMotor_2->run(BACKWARD);
}
/* Function to drive M1 and M2 backward and M3 and M4 forward */
void TurnLeft(int spd)
{
// motor 1
myMotor_1->setSpeed(spd);
Adafruit Motor/Stepper/Servo Shield 48
myMotor_1->run(BACKWARD);
// motor 2
myMotor_2->setSpeed(spd);
myMotor_2->run(BACKWARD);
}
/* Function to drive M1 and M2 forward and M3 and M4 backward */
void TurnRight(int spd)
{
// motor 1
myMotor_1->setSpeed(spd);
myMotor_1->run(FORWARD);
// motor 2
myMotor_2->setSpeed(spd);
myMotor_2->run(FORWARD);
}
So that you can have an idea of how PWM is used to control the speed of a motor, a PWM value
of 75 corresponds to a duty cycle of 29.5%, which means the power is “on” for 29.5% of the
period of the signal and “off” for 70.5% of the period of the signal. You can change this value
Adafruit Motor/Stepper/Servo Shield 49
between 0 and 255, which mimics voltages between 0V and the maximum voltage that the
shield can supply to the motor.
SUGGESTED PRACTICE
EXERCISE 4.1: Driving four motors
In Example 4.1 we used the Adafruit Motor/Stepper/Servo shield to drive two DC motors. In this
exercise, you will drive four DC motors as in a differential drive robot. Consider a scenario in
which the motors are being used as part of a robot that has four wheels: The motors on “M1” and
“M2” are driving the two wheels located on one side of the robot and the motors on “M3” and
“M4” are driving the two wheels located on the other side of the robot.
Notes:
• Drive all the motors in the “forward” direction to move the robot forward along a “straight”
line.
• Drive all the motors in the “backward” direction to move the robot backward along a
“straight” line.
• Drive motors “M1” and “M2” in the “forward” direction and motors “M3” and “M4” in the
“backward” direction to make the robot turn “right”.
• Drive motors “M1” and “M2” in the “backward” direction and motors “M3” and “M4” in the
“forward” direction to make the robot turn “left".
• Write your code in such a way that the robot makes a square loop, driving in a straight line
on each side of the square for 2 seconds. Do you think that the robot will come back to its
starting point? If your answer is "No", what do you think the problem is? How do you think
you can fix it?
Adafruit 16-Channel 12-Bit PWM/Servo Shield 50
The Adafruit 16-Channel 12-Bit PWM/Servo Shield can drive up 16 servos. The only Arduino pins
required to run the shield are the Ground, 5V, and the SDA and SCL I2C control pins. Since I2C is a
“shared bus”, you can still connect other I2C devices to the SDA and SCL pins of the Arduino as long
as they do not have conflicting I2C addresses. The default address for the Adafruit servo shield is 0x40.
Although the Adafruit 16-Channel 12-Bit PWM/Servo Shield can be used to drive up to 16 servos,
only 4 servos can be easily connected to the shield due to issues arising from stacking the different
shields in the kit on top of each other. A 3x4 right angle male header was soldered to the shield to
facilitate the connection of the 4 servos. If additional servos are needed for your project, you can find
ways to use the other pin connections in the shield. However, it is very likely that some soldering will
be required.
3. Check that the “Adafruit_PWMServoDriver” folder contains the following two files:
Adafruit_PWMServoDriver.cpp and Adafruit_PWMServoDriver.h.
4. Place the “Adafruit_PWMServoDriver” folder in the “libraries” folder of the Arduino
sketchbook that you are using to save your sketches.
5. Close and reopen the Arduino IDE software and the library required by the Adafruit PWM/Servo
Shield should be ready to use.
Additional Resources
To learn more about the servo shield and its associated library visit the following web sites:
o https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-shield/using-the-adafruit-library.
o https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-shield/library-reference.
Figure 4-5. Powering the Adafruit PWM/Servo Shield with a 6V, 4 AA battery pack.
Figure 4-6. Close up of the connection of the positive (red) and negative (black) cables from the 6V
battery pack to the “V+” terminal block of the Adafruit PWM/Servo Shield. Getting this connection
backwards could result in the servos not working correctly and/or in damage to the servos.
Adafruit 16-Channel 12-Bit PWM/Servo Shield 53
EXAMPLE 4.2
Driving servos using the Adafruit 16-Channel 12-Bit PWM/Servo Shield
In this example we will be driving two servos using the Adafruit PWM/Servo Shield.
Before we begin:
• We only need the Arduino UNO and the Adafruit 16-Channel 12-Bit PWM/Servo Shield for
this example.
• If you did Example 4.1 and have the Adafruit Motor/Stepper/Servo Shield stacked on top of
the Arduino, you don’t need to remove that shield. Simply stack the Adafruit PWM/Servo
Shield on top of the motor shield.
Notes:
o Since the code in this example doesn’t make use of the Adafruit Motor/Stepper/Servo
Shield, you can disconnect the external power supply (i.e., the 12V battery pack) and the
DC motors from that shield.
o Remember that the Adafruit Motor/Stepper/Servo Shield and the Adafruit PWM/Servo
Shield use different battery packs. The motor shield uses a 12V battery pack whereas the
servo shield uses a 6V battery pack.
• We will be using VEX EDR 3-Wire Servos in this example. However, you can use other
servos if you like. In that case, some lines in the code will need to be adjusted based on the
characteristics of the servos that you selected.
• An important characteristic of a servo motor is the angle that it can rotate. The VEX EDR 3-
Wire Servo only has 100 degrees of rotation.
• We will use the labels Servo 1 and Servo 2 to refer to the two servos that we will be using in
this example.
Follow the steps below to make the required hardware connections and upload the example
code to the Arduino.
Adafruit 16-Channel 12-Bit PWM/Servo Shield 54
1. Connect the 6V battery pack to the Adafruit PWM/Servo Shield as shown in Figure 4-6.
Remember that both the red and the green LEDs on the shield must be lit for the shield
to work.
2. Connect Servo 1 and Servo 2 to the first 2 sets of pins of the 3x4 right angle male header
as shown in Figure 4-7.
Figure 4-7. Overview of how to connect the servos to the Adafruit PWM/Servo Shield.
3. Copy and paste the code below in the Arduino IDE and save it as servo.ino.
4. Upload the code to the Arduino.
Important Notes:
o The first two #include lines in the example code should always be added at the beginning
of programs that use the Adafruit PWM/Servo Shield. They include all the libraries that
are required to use the shield.
o The function setPWMFreq(freq) can be used to adjust the PWM frequency (freq), which
determines how many full “pulses” per second are generated. In other words, the
frequency (freq) determines how “long” each pulse is in duration from start to finish,
considering both the high and low segments of the pulse. The value of freq is given in Hz
and needs to be in the range between 40 and 1000. This frequency should match the
Adafruit 16-Channel 12-Bit PWM/Servo Shield 55
frequency used by your servo; see the datasheet for your device to choose the correct
value.
o The function setPWM(Channel, On, Off) sets the start (On) and end (Off) of the high
segment of the PWM pulse on a specific channel (Channel). You specify the “tick” value
between 0 and 4095 when the signal will turn on (On), and when it will turn off (Off).
Channel indicates which of the 16 PWM outputs that the shield has (which are labeled 0
to 15 on the board) should be updated with the new values. If you set the value of On to
0, then the value that you assign to Off will determine the pulse width.
o The rotation angle of the servos that you selected can be used together with the standard
Arduino map(value, fromLow, fromHigh, toLow, toHigh) function to convert a desired
angular position to the corresponding pulse width of the control signal. For that purpose,
you can use a statement such as
pulseWidth = map(degrees, 0, rotationAngle, SERVOMIN, SERVOMAX)
to convert the desired angular position degrees into the corresponding pulse width value
pulseWidth. The parameters rotationAngle, SERVOMIN, and SERVOMAX need to be
replaced by specific values applicable to the servo that is being controlled. As with the
frequency, these values may be obtained from your servo’s datasheet.
o The rotation angle (rotationAngle) and the pulse width values corresponding to the
minimum (SERVOMIN 0 degrees) and maximum (SERVOMAX rotationAngle
degrees) angular positions of a servo vary between different brands and models. For
precise position control, you can calibrate the minimum and maximum pulse widths in
your code to match the minimum and maximum angular positions of the servo. To find
the minimum (SERVOMIN), use the example code and edit the SERVOMIN value until
the low-point of the servo reaches the minimum range of travel. Approach this gradually
and stop before the physical limit of travel is reached. Do the same procedure to find out
the maximum (SERVOMAX). While you can use the values provided by the
manufacturer, this calibration approach allows you to correct for any part-to-part
variation between servo motors.
o For VEX EDR 3-Wire Servos you need to use a value of 100 for the parameter
rotationAngle.
o The #define directive allows the definition of macros within your code. These macro
definitions allow constant values to be declared for use throughout your code. Macro
definitions are not variables and cannot be changed by your program code like variables.
Adafruit 16-Channel 12-Bit PWM/Servo Shield 56
You generally use this option when creating constants that represent numbers, strings or
expressions. The syntax is #define CNAME value.
#include<Wire.h>
#include<Adafruit_PWMServoDriver.h>
/* The pulse width min and max may vary depending on your servo.
You want these values to be as small and large as possible without
hitting the hard stop on both ends of the range. You will have
to tweak the values to match the servo that you are using. */
/* Calibrate the min and max pulse width values according to the
instructions given in:
https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-
shield/using-the-adafruit-library */
/*
pulselength = map(degrees, Minangle, MaxAngle, SERVOMIN, SERVOMAX);
*/
void setup() {
Serial.begin(115200);
Serial.println("16 channel servo shield test!");
pwm.begin();
/* Analog servos run at approximately 60 Hz updates */
pwm.setPWMFreq(60);
/* Don’t worry about the yield function included below */
yield();
}
void loop() {
/* Drive each servo one at a time using a statement like */
// pwm.setPWM(servonum_1, 0, pulselength)
pwm.setPWM(servonum_1, 0, SERVOMAX);
pwm.setPWM(servonum_2, 0, SERVOMAX);
delay(2000);
pwm.setPWM(servonum_1, 0, SERVOMIN);
pwm.setPWM(servonum_2, 0, SERVOMIN);
delay(2000);
angle = 0;
while (angle < MaxAngle) {
pulselength=map(angle, MinAngle, MaxAngle, SERVOMIN, SERVOMAX);
pwm.setPWM(servonum_1, 0, pulselength);
pwm.setPWM(servonum_2, 0, pulselength);
angle = angle + increment;
delay(1000);
}
while (angle >= MinAngle) {
pulselength=map(angle, MinAngle, MaxAngle, SERVOMIN, SERVOMAX);
pwm.setPWM(servonum_1, 0, pulselength);
pwm.setPWM(servonum_2, 0, pulselength);
angle = angle - increment;
delay(1000);
}
Adafruit 16-Channel 12-Bit PWM/Servo Shield 58
yield();
delay(2000);
}
SUGGESTED PRACTICE
EXERCISE 4.2: Find the minimum and maximum pulse width for your servo
In the example given above, the SERVOMIN and SERVOMAX values will be different for
different servos. The approximate value for the servo used in this manual is given. Try changing
these values to see the maximum range for your specific device.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 59
The SparkFun USB Host Shield takes its power from the “Vin” pin of the Arduino. Power from that
pin is regulated to both 5V and 3.3V on the shield.
Figure 4-8. Accessing the examples that come with the library for the SparkFun USB Host Shield.
Additional Resources
To learn more about the SparkFun USB Host Shield visit the following web site:
o https://www.sparkfun.com/products/9947.
Follow the steps provided below to get the MAC address of a Bluetooth dongle.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 61
1. Plug in the Bluetooth dongle into a USB port in your computer and wait until your computer
recognizes the Bluetooth device and installs the required drivers.
2. Go to the Windows “Device Manager” to see the list of “Bluetooth Radios” that are connected to
your computer (see Figure 4-9).
Note: You can find the Windows “Device Manager” in the Windows “Control Panel”.
Figure 4-9. Locating the “Bluetooth Radios” connected to your computer in the Windows “Device
Manager”.
3. Right click the “Generic Bluetooth Radio” and click “Properties”. You should see a “window”
like the one shown in Figure 4-10.
4. Click the “Advanced” tab on the “Generic Bluetooth Radio Properties”. You should see a
“window” like the one shown in Figure 4-11.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 62
5. The numbers next to the word “Address” in Figure 4-11 are the MAC address of the Bluetooth
dongle. For example, the MAC address of the Bluetooth dongle corresponding to Figure 4-11 is
0x00:0x15:0x83:0xd1:0xe8:0xc3 (00:15:83:d1:e8:c3), where the prefix “0x” simply indicates that
the number is given in hexadecimal. The MAC address will be used to connect the Sony PS3
controller to the Bluetooth dongle.
It is important to point out that when we tried to use a PS3 controller that was not manufactured by
Sony, we were unable to connect it to the SparkFun USB Host Shield using the Bluetooth dongle. Thus,
we strongly suggest that you use a Sony PS3 controller to avoid experiencing connection problems.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 63
Figure 4-12. Analog and digital inputs provided by the Sony PS3 controller.
EXAMPLE 4.3
Sending analog and digital signals from the Sony PS3 controller to the Arduino
using a Bluetooth connection.
The following example will show the procedure to use the SparkFun USB Host Shield, Bluetooth
dongle, and Sony PS3 controller together to make a Bluetooth wireless connection between the
Sony PS3 controller and the SparkFun USB Host Shield that can send analog and digital signals
from the Sony PS3 controller to the Arduino.
Before we begin:
• We only need the Arduino UNO, the SparkFun USB Host Shield, the Bluetooth dongle, and
the Sony PS3 controller for this example.
• If you have other shields stacked on top of the Arduino, you don’t need to remove those
shields. However, when several shields are stacked on top of the Arduino, you need to make
sure that the SparkFun USB Host Shield is at the top of the stack.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 64
Figure 4-13. Changing the MAC address on the Sony PS3 controller.
4. Type the MAC address of the Bluetooth dongle you are going to use in the “Change
Master” input area and click “Update”.
5. The “Current Master” MAC address of the Sony PS3 controller will change to the new
MAC address that you provided.
Follow the steps below to make the required hardware connections and upload the example
code to the Arduino.
1. Place the SparkFun USB Host Shield on top of the last shield stacked on top of the
Arduino. If there are no shields stacked on top of the Arduino, place the SparkFun USB
Host Shield on top of the Arduino.
2. Insert the Bluetooth dongle in the USB port of the SparkFun USB Host Shield as shown
in Figure 4-14.
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 65
Figure 4-14. Inserting the Bluetooth dongle in the USB port of the SparkFun USB Host
Shield.
3. Power the Arduino so that the SparkFun USB Host Shield can receive power.
Note: Remember that the SparkFun USB Host Shield takes its power from the “Vin” pin
of the Arduino.
4. Make sure that the on/off switch of the SparkFun USB Host shield is set to the “on”
position as shown in Figure 4-15.
Figure 4-15. Location of the on/off switch of the SparkFun USB Host Shield.
5. Copy and paste the code below in the Arduino IDE and save it as BluetoothDongle.ino.
6. Upload the code to the Arduino.
/* BluetoothDongle.ino */
#include<PS3BT.h>
#include<usbhub.h>
/* Satisfy the Arduino IDE, which needs to see the include statements
below in the ino file too. */
#ifdefdobogusinclude
#include<spi4teensy3.h>
#include<SPI.h>
SparkFun USB Host Shield, Bluetooth Dongle, and Sony PS3 Controller 66
#endif
USB Usb;
//USBHub Hub1(&Usb); /* Some dongles have a hub inside */
void setup() {
Serial.begin(115200);
if (Usb.Init() ==-1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt (infinite loop since while (1) is always true)
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop() {
Usb.Task();
Serial.print(RightX); Serial.print("\t");
Serial.println(RightY);
}
7. Open the Serial Monitor in the Arduino IDE and make sure that the correct baud rate is
selected.
8. Observe the message displayed in the Serial Monitor. It should indicate that the USB
host shield and the Bluetooth Library have started. If that is not the case, press the reset
button on the USB host shield to see if the Library starts. If it still doesn’t start, make sure
that the Bluetooth dongle is properly connected to the USB host shield and that the Sony
PS3 controller has the MAC address of the Bluetooth dongle that you are using.
9. Once the Bluetooth Library starts, press the on/off button of the Sony PS3 controller. At
first, all the LEDs in the front of the Sony PS3 controller, which are labeled from 1 to 4,
will be blinking. Wait until only the LED labeled as 1 is steadily on, which indicates that
the Bluetooth connection between the Sony PS3 controller and the Bluetooth dongle has
been made.
Notes:
o If the LED labeled as 1 is not steadily on, no connection has been made. To solve this
problem, reset the USB host shield to see if you can make the connection. If that fails,
make sure that the Bluetooth dongle is properly connected to the USB host shield and
that the Sony PS3 controller has the MAC address of the Bluetooth dongle that you
are using and repeat this step.
o If you close and open the Serial Monitor the wireless connection between the Sony
PS3 controller and the Bluetooth dongle may be lost. Repeat this step to reestablish
the wireless connection.
o If the Sony PS3 controller gets disconnected from the Bluetooth dongle due to power
loss or any other reason, press the reset button of the USB host shield before trying to
connect the Sony PS3 controller again.
10. Look at the Serial Monitor and observe all the readings that are displayed. Move the two
joysticks to see how the values that are displayed change between 0 and 255 depending
on the position of each joystick.
Each Bluetooth dongle has a different MAC address. Thus, you need to make sure that the Sony
PS3 controller is set to the MAC address of the Bluetooth dongle that you are using.
Although the code in the example is written to read and assign all the digital and analog signals
to variables, only the variables corresponding to the analog signals from the two joysticks are
printed to the Serial Monitor. You can observe the value of the other variables in the Serial
Monitor by “un-commenting” (removing the “//” at the beginning of the line of code) and
“commenting” (adding a “//” at the beginning of the line of code) different parts of the code as
needed.
Hardware Assembly Process 70
Figure 4-16. The base of the complete stack is the Arduino UNO.
Figure 4-17. Connect the Adafruit Motor/Stepper/Servo Shield on top of the Arduino UNO. Make sure
that the connecting pins are properly aligned with the headers of the Arduino UNO.
Hardware Assembly Process 71
Figure 4-18. Connect the Adafruit 16-Channel 12-Bit PWM/Servo Shield on top of the Adafruit
Motor/Stepper/Servo Shield. Make sure that the extensions from the power connector of the Adafruit
PWM/Servo Shield are not touching the connectors of the Adafruit Motor/Stepper/Servo Shield.
Figure 4-19. Connect the Grove Base Shield on top of the Adafruit 16-Channel 12-Bit PWM/Servo
Shield.
Hardware Assembly Process 72
Figure 4-20. Connect the SparkFun USB Host Shield on top of the the Grove Base Shield.
Figure 4-21. To facilitate access to the analog and digital sockets of the Grove Base Shield, one can
place headers as spacers between the Grove Base Shield and the SparkFun USB Host Shield.
After stacking all the shields together, some of the Arduino’s digital pins will be taken by the SparkFun
USB Host Shield. The digital pins that remain available are D0 to D6. All the analog pins (i.e., pins A0
Hardware Assembly Process 73
to A5) remain available. Figure 4-22 indicates all the digital and analog pins that remain available. You
can use those pins to connect sensors or actuators.
Figure 4-22. Arduino digital and analog pins that remain available after stacking all the shields.
Hardware Assembly Process 74
PART UNIT
ITEM SUPPLIER QTY
NUMBER PRICE
Microcontroller/Shields
Arduino UNO Rev3 Adafruit 50 1 $24.95
Adafruit Motor/Stepper/Servo Shield Adafruit 1438 1 $19.95
Adafruit 16-Channel 12-bit PWM/Servo Shield - I2C
Adafruit 1411 1 $17.50
interface
SparkFun USB Host Shield Sparkfun DEV-09947 1 $24.95
Grove Components
Grove Starter Kit for Arduino Seeed Studio 110060024 1 $49.90
Extra Buttons Seeed Studio 101020003 2 $1.90
Extra LED Modules Seeed Studio 104030009 3 $1.90
Other Components
USB Cable A to B - 6 Foot Sparkfun CAB-00512 1 $3.95
SparkFun USB Mini-B Cable - 6 Foot Sparkfun CAB-11301 1 $3.95
Arduino Stackable Header Kit Sparkfun 10007 3 $1.50
Arduino Uno Enclosure Sparkfun 12839 1 $7.95
Battery Holder 4xAA with Cover and Switch Sparkfun 12083 1 $1.95
Holder 8 AA Cell W/Cover and switch Digi-Key BK-6049-ND 1 $6.53
Bluetooth 4.0 USB Module (v2.1 Back-Compatible) Adafruit 1327 1 $11.95
Shield stacking headers for Arduino (R3 Compatible) Adafruit 85 1 $1.95
8 x AA battery holder with 5.5mm/2.1mm Plug and
Adafruit 875 1 $5.95
On/Off Switch
3x4 Right Angle Male Header - 4 pack Adafruit 816 1 $2.95
Sony Dual Shock 3 - Black (PS3) WalMart 000599384 1 $49.99
Hardware Assembly Process 80
Follow the steps in Figure C-1 to Figure C-4 to assemble the motor shield.
Figure C-2. Start by sliding the 10-pin, the two 8-pin and the 6-pin stacking headers into the outer
rows of the top side of the shield. Then flip the board over so that it is resting on the four headers.
Figure C-3. Tack one pin of each header to set the headers in place before doing more soldering.
Figure C-4. Once you have tacked and straightened all the headers, solder the remaining pins of each
header.
Hardware Assembly Process 82
Figure C-6. Start by sliding the 10-pin, the two 8-pin and the 6-pin stacking headers into the outer
rows of the top side of the shield. Then flip the board over so that it is resting on the four headers.
Hardware Assembly Process 83
Figure C-7. Tack one pin of each header to set the headers in place. Once you have tacked and
straightened all the headers, solder the remaining pins of each header.
Figure C-8. Insert the power conector terminals and solder them as shown.
Hardware Assembly Process 84
Figure C-9. Insert the 3x4 right angle male header and solder its pins on the bottom side of the shield.
Note: As shown in the picture, we are going to place the 3x4 right angle male header in a position that
does not interfere with the stackable header for the Arduino pins.
Figure C-11. Insert the two 8-pin and the two 6-pin Arduino stackable headers as shown. Then flip the
board over so it is resting on the four headers.
Figure C-12. Tack one pin of each header to set the headers in place. Once you have tacked and
straightened all the headers, solder the remaining pins of each header.
Now the Adafruit Motor/Stepper/Servo Shield, the Adafruit 16-Channel 12-bit PWM/Servo Shield,
and the SparkFun USB Host Shield are fully assembled and ready for use. These shields will be stacked
together with the Arduino UNO Rev3 and the Grove Base Shield and used as a complete hardware kit
(see Chapter 4).
Hardware Assembly Process 86
In Figure D-1 the separation between the vertical green lines represents a specified time period. This
time duration or period is the inverse of the PWM frequency. With Arduino's PWM frequency at about
500Hz, the time between two consecutive green lines would be about 2 milliseconds. The PWM value
ranges between 0 and 255. The command analogWrite(255) induces a 100% duty cycle (always ON),
and analogWrite(127) is a 50% duty cycle (ON half of the time). Likewise analogWrite(64) is a 25%
duty cycle.
For more detail explanations and examples on PWM see the following web sites:
• https://learn.sparkfun.com/tutorials/pulse-width-modulation
• https://en.wikipedia.org/wiki/Pulse-width_modulation
Hardware Assembly Process 88
REFERENCES
1. Blum, Richard. Sams Teach Yourself Arduino Programming in 24 Hours. Pearson Education, Inc. 2015.
2. https://en.wikipedia.org/wiki/Arduino
3. https://www.arduino.cc/
4. https://www.seeedstudio.com/Grove-Starter-Kit-for-Arduino-p-1855.html
5. http://www.seeedstudio.com/wiki/Base_shield_v2.
6. https://www.adafruit.com/product/1438
7. https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-shield/overview?view=all
8. https://www.sparkfun.com/products/9947
9. https://www.adafruit.com/products/1327
10. http://www.vexrobotics.com/motors.html
11. http://curriculum.vexrobotics.co.uk/curriculum/speed-power-torque-and-dc-motors/dc-motors