Shields and Libraries
So far we've been making our own circuits and writing our own
programs to extend our Arduino's capabilities. This is great if you are
learning the basics and simply experimenting. For more complicated
projects, you might like to take advantage of someone else's work.
Conversely if you've come up with an interesting solution to a problem,
you might want to share your code with someone, or make a more
permanent and easily swappable circuit. Arduino circuits can be made
modular with shields. Reusable Arduino code can be shared with
libraries.
In this lesson you will learn:
Shields
You have been given a Danger Shield from SparkFun. It is yours to use
for the rest of the week. Arduino shields work a little bit like game
cartridges. They use the (sort of) standard layout of your Arduino's
pins to allow you to plug in different circuits. This makes it easy to
move your Arduino from project to project. There are many shields
available in addition to the Danger Shield, which is mostly just and
fun and learning. Most shields make a duplicate set of Arudino headers
available to
you so you can add your own circuits to unused pins. Some are even
designed to be stacked one on top of the other so you can mix and match
features that meet your project's needs.
Here's a list of links to some more useful shields:
- Network - plug your Arduino directly into your LAN. Use it to
send emails or serve webpages that can report on or control the
Arduino:
- Display - pictures, text and flashing lights!
- Sound - this stuff is way better than tone functions and a piezo buzzer.
- Motor - thinking of making an Arduino powered robot?
You can see many more from various companies the shields sections at Solarbotics.
Because the Arduino world is a bit of a DIY (Do It Yourself) kind of
place, many shields are shipped as a printed circuit board and loose
parts. You get to solder them together yourself. The Danger Shield came
that way. It can be great fun to learn to solder, and it is very
rewarding when the finished product works.
Plug In the Shield
Once your shield is unpacked and assembled (this should have been
done for you by the mentors), you plug it into the top of your Arduino
like this:
Note that you should line up the shield pins with the digital 0 and
analog 5 pins on your Arduino because newer Arduinos like the Uno revision 3 have extra pins near the USB jack.
Make sure the Danger Shield isn't touching the USB jack or Button
A2/D10 will short and fail to work correctly.
Download Sample Code
The Danger Shield, like most shields, comes with sample code. You can find the sample code on it's product page at Sparkfun.
- Go to the SparkFun web site: http://www.sparkfun.com
- Search for the Danger Shield. Make sure you find the product that matches the part number on the box.
- Locate the Example Code link in the Documents section of the product description.
- Download the zip file and extract the folder inside to your personal Arduino sketchbook folder: Finder (two tone face in dock) | Documents | Arduino
- Open the the file called Danger_shield.pde inside.
- .pde is the old Arduino file extension. The new one is .ino. You may have to use the Arduino IDE to open these older files.
- Try to upload the code.
- You can't, you'll get errors.
- You need the Capacitive Sensing library. You will install it as an exercise in the next section.
Libraries
In computer programming, a library is a collection of code related to
performing some task. In C and C++, the languages the Arduino language
is based on, you have to request libraries by writing #include
statements. If you downloaded the DangerShield example code, you will
see that it tries to include the capsense library in the very first
line:
#include <CapSense.h>
/*
* Danger Shield Example Sketch
* Copyright (c) 2010 SparkFun Electronics. All right reserved.
* Written by Chris Taylor
*
* This code was written to demonstrate the Danger Shield from SparkFun Electronics
*
* This code will test all of the devices on the Danger Shield one at a time.
* Press button 1 (D10) to cycle through the different tests. View their output on
* a terminal running at 9600 baud.
*
* http://www.sparkfun.com
*/
The Capacitive Sensing library is just one of many libraries for the
Arduino. Some are built in to the language and you never have to worry
about them. Some have been contributed to the Arduino project by others
and are listed on the Arduino web site, but you have to install and #include
them yourself. Others are meant for specific hardware. You'll usually
find these or a link to them on the hardware's product page.
If you want to write your own library,
you can follow the instructions on the Arduino site. You should find
the example provided there very familiar. Be aware that Arduino
libraries are written in proper C++ with special Arduino related
includes. The data types used in libraries are different than the ones
you use in a regular Arduino project.
Install and Use the Capacitive Sensing Library
Please try the following exercise. Feel free to ask for help if you need it.
Instructions for installing new libraries can be found on the Arduino Libraries page under the section Contributed Libraries. You should:
- find the library installation instructions. You will not need root access to install libraries, they will go in your personal Arduino folder.
- find, download and install the Capacitive Sensing library
- the Capacitive Sensing library is listed on the Contributed Libraries page.
- The link there leads to instructions and download links.
- the file you want is called CapSense04.zip
- restart your Arduino IDE
- try uploading the Danger Shield code again
- the Danger Shield example uses Serial communication to guide
you through tests of all the components of the Danger Shield. Follow
the test protocol.
Read the Danger Shield example code to see:
- that it's structured similarly to the multiple brightness code from the Buttons! lesson
- what pins the Danger Shield's components are attached to.
- a simple way to program an edge detect button using a method similar to the Intermediate challenge I proposed last week. It's only used on button 1.
- how to make numbers appear on the 7 segment display.
- Don't worry if it doesn't make perfect sense yet. Just notice that:
- the ledCharSet array contains the patterns for the numbers
- the numbers are sent to the display with digitalWrite commands to a pin called latch and with a special shiftOut command for communicating with shift registers.
Do you remember what pins are used for capacitive sensing?
- Open the CapSense sample sketch and modify it to use the Danger
Shield's CapSense pins to turn on a light when someone is touching the
Cap Sense pad
- Open File | Examples | CapSense
- Delete or comment out the code for CapSense pins you aren't using.
Project
Once you have read through the lesson and tried to do everything
described above, you can try some soldering. This is good practice for
your future hardware projects. If you want to be able to make something
useful today, then try soldering an Arduino TV Out cable. You cut an
old video cable and solder two of your kit's wires to it. Ask me for a
video cable if you want one.
There is a low-res, black and white (think Pong) TV Out library available through Google Code. Download TVoutBeta1.zip and put all the folders in your libraries folder.
The TVOut site has detailed instructions on how to connect a TV Out
cable to your Arduino. The plans call for a 470Ω and a 1kΩ resistor. Your
kit doesn't include 470Ω resistor. You can combine resistors to
make different values. Wire them in parallel to reduce resistance. Wire
them in series to increase resistance. To make a ~470Ω resistor from
the parts in your kit, wire two 1kΩ resistors and a 10kΩ resistor in
parallel. See the diagrams below for help:
When you're done making your cable and your circuit, and installing the
library, you can test the whole thing out with the TV Out Example: File | Examples | TVout | DemoNTSC. This
is a really cool example that has some real 3D graphics. It might
remind you of the Asmotek tour. Maybe you can do your end of camp
presentation with this running behind you like Tony did.
If you're really bored, you can help the first week campers, or try
making CapSense pads with aluminium foil. I brought some. Ask me for it.