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:

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.

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:
Read the Danger Shield example code to see:
Do you remember what pins are used for capacitive sensing?

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.