The Arduino Inventor's Guide (10 page)

For a real challenge, try programming your Arduino to flash the letters of your name in Morse code with a series of short (dot) and long (dash) blinks.
Figure 1-22
shows a Morse code cheat sheet to help you figure out the blink patterns. The classic message that most people start with is S-O-S, or . . . - - - . . . (dot dot dot, dash dash dash, dot dot dot).

FIGURE 1-22:
Basic Morse code chart

Modify

A blinking light is a powerful thing! With your newfound superpower, you can physically add LEDs to a lot of things around the house. A Halloween costume is always a great place for some blinky bling. You could solder the LED legs to some wire to make the connections longer so it’s easy to hide the Arduino somewhere more comfortable for the wearer (like in a pocket). We took a Halloween spider we got from the local grocery store and hacked it with some creepy red eyes that blink (see
Figure 1-23
).

Another good fit for blinking and controlling LEDs is in scale modeling. Adding working LEDs to car headlights, buildings, or streetlights is always a great way to create the illusion of reality in any scale model or scene, as shown in
Figure 1-24
.

FIGURE 1-23:
A blinky scary spider

FIGURE 1-24:
A scale model with Arduino-controlled lights

SAVING YOUR SKETCH

Every project looks more stylish with a few blinking LEDs, so we suggest you keep your remixed Blink sketch handy so you can reuse parts of it in future builds. Save your sketch, and be sure to name it something descriptive that’ll remind you what it is. Your filename should not contain any spaces; if it does, Arduino will replace the spaces with underscore (_) characters. By default, when you save your sketches, Arduino will save them to the Arduino sketchbook folder, usually found in the
Documents
folder on your computer. You can choose to save them elsewhere, but it’s often a good idea to have all your sketches in one place.

When you’re ready to level up your blinking skills, head to
Project 2
, where we’ll show you how to build your very own Arduino-powered stoplight.

2
A Stoplight for Your House

In your first big step toward world domination through embedded electronics, you set up the Arduino IDE and blinked an LED. That’s huge, but with an Arduino, no project needs to stop at just one LED. This project will show you how to expand your first LED sketch to display
a blinking pattern on
three
LEDs. Your mission, should you choose to accept it, is to build and program a stoplight for a busy hallway in your house (see
Figure 2-1
).

FIGURE 2-1:
The completed Stoplight project

MATERIALS TO GATHER

The materials in this project are all pretty simple. All of the electronic parts are standard in the SparkFun Inventor’s Kit, except for the ones marked with an asterisk (*). If you’re using your own kit or piecing together parts yourself, see the following parts list.
Figure 2-2
shows all of the parts used in this project.

Electronic Parts

• One SparkFun RedBoard (DEV-13975), Arduino Uno (DEV-11021), or any other Arduino-compatible board

• One USB Mini-B cable (CAB-11301 or your board’s USB cable; not shown)

• One solderless breadboard (PRT-12002)

• One red LED, one yellow LED, and one green LED (COM-12062)

• Three 330 Ω resistors (COM-08377, or COM-11507 for a pack of 20)

• Male-to-male jumper wires (PRT-11026)

• Male-to-female jumper wires (PRT-09140*)

• (Optional) One 4 AA battery holder (PRT-09835*; not shown)

FIGURE 2-2:
Components for the Stoplight

Other Materials and Tools

If you want to build an enclosure like the one in
Figure 2-1
or follow the suggestions in “
Going Further
” on page
64
, you’ll also need the following supplies, shown in
Figures 2-3
and
2-4
:

• Pencil

• Craft knife

• Metal ruler

• Pliers

• Wire stripper

• Glue (hot glue gun or craft glue)

• (Optional) Drill and a 3/16-inch drill bit

• (Optional) Soldering iron

• (Optional) Solder

• (Optional) Helping hands (not shown)

• Cardboard (about 12 inches square) or a cardboard box

• Two ping-pong balls

• Enclosure template (see
Figure 2-15
on page
55
)

NOTE

Good, clean cardboard will be worth its weight in gold in these projects. We suggest picking up cardboard sheets from a craft or art supply store.

FIGURE 2-3:
Recommended tools

FIGURE 2-4:
Recommended building materials

NEW COMPONENT: THE RESISTOR

Although you used an LED on its own in
Project 1
, in most cases it’s best to use a
resistor
to protect the LED from too much current. Resistors like the ones in
Figure 2-5
are everywhere. They are indispensable when you’re building circuits, and you’ll need them to complete this project, too.

FIGURE 2-5:
Resistors up close and personal

If you think of electricity like the flow of water through a pipe, a resistor is analogous to a point where the pipe size narrows, reducing the water flow. (If you’re curious, see “
Visualizing Electricity as Water in a Pipe
” on page
4
, which describes this metaphor in detail.) Resistors control or limit the flow of current.

Resistance is measured in
ohms
(typically shortened to Ω, the Greek symbol omega), and the colored bands on resistors represent their resistance. You’ll find a resistor color band decoder in “
Resistors and Bands
” on page
308
; however, in this book, you only need to be able to identify two different values of resistors: 330 Ω and 10 kΩ. The bands on a 330 Ω resistor are orange, orange, and brown (see
Figure 2-5
), while on a 10 kΩ resistor they’re brown, black, and orange. There is also a fourth band on a resistor, and its color indicates the resistor’s
tolerance
. A resistor’s value will be accurate within a certain tolerance: silver means the resistor has a 5 percent tolerance, while gold indicates a 10 percent tolerance. The projects in this book aren’t sensitive enough for the tolerance level to make a difference, though, so we’ll just refer to the resistors by their assumed value, which will work for either tolerance band.

Some components, like LEDs, can be damaged if the current flowing to them is too high, and resistors can protect those components by reducing the current. Having a resistor in line with an LED to limit the current to a safe level is a good precaution so your LED doesn’t burn out—or, in the worst case, pop! (Yes, they can literally pop.) From here on, we’ll use current-limiting resistors in all projects.

WHY THE STOPLIGHT USES 330 Ω RESISTORS

An average red LED has a maximum current rating of about 20 mA, as listed on its datasheet. In order to protect it, you need to add a resistor to keep the current below this limit. But how do you know to use a 330 Ω resistor?

The output pins on the Arduino provide 5 V when they are turned on. Depending on the color, each LED needs a slightly different amount of voltage to turn on, typically in the range of 2.0 to 3.5 V. A red LED turns on at about 2 V, and that leaves 3 V remaining. The 3 V will be dissipated across a resistor or anything else that is in line in the circuit. It’s generally good practice to limit the current going through an LED to about half the maximum, so for the red LED with a maximum current rating of 20 mA, you get 10 mA. You can calculate the resistor needed for 3 V and 10 mA with
Ohm’s law
(remember 10 mA = 0.01 A):

But 300 Ω isn’t a standard resistor value. The closest standard resistor value is 330 Ω, and usually the nearest standard resistor is good enough. This should ensure that the LED lasts for a very, very long time. Since the resistor will be dictating the current, this is a
current-limiting resistor
.

If you have different resistors available, you could use a different value resistor and see what happens. Bigger resistors will make the current smaller, and smaller resistors will make the current bigger. What happens if you use the 10 kΩ resistor instead?

BUILD THE STOPLIGHT PROTOTYPE

Now it’s time to build the circuit. First, take a look at the schematic shown in
Figure 2-6
. You’ll build this on a breadboard, as shown in
Figure 2-7
.

FIGURE 2-6:
Schematic diagram for the Stoplight project

The schematic illustrates how each component is connected electrically. Pin 13, pin 12, and pin 11 on the Arduino will each be used to control an individual LED on the Stoplight circuit. As you can see in the schematic, each LED is connected to an individual resistor, and each resistor is connected to GND (ground). Next, let’s look at the wiring.

FIGURE 2-7:
Connecting a red LED to a breadboard with a current-limiting resistor

Other books

Forever With You by Laurelin Paige
Glow by Ned Beauman
The Silent Waters by Brittainy Cherry
Boelik by Amy Lehigh
The Roots of the Olive Tree by Courtney Miller Santo
Countdown by Unknown Author
Down to My Soul (Soul Series Book 2) by Kennedy Ryan, Lisa Christmas
Sea Change by Aimee Friedman
Esalen Cookbook by Cascio, Charlie