The Arduino Inventor's Guide (11 page)

BOOK: The Arduino Inventor's Guide
9.18Mb size Format: txt, pdf, ePub
Connect the Red LED to the Breadboard

Now you’ll start to translate the schematic into an actual circuit. In the first project, you blinked an LED built into the Arduino board. This LED was internally wired to pin 13 on the Arduino. Because you’ll be using three discrete LEDs, you need to wire these up yourself. Take out your breadboard, and, following the schematic in
Figure 2-6
or the illustrated diagram in
Figure 2-7
, connect pin 13 to the positive (long) leg of the LED.

NOTE

For a refresher on how breadboards work, see “
Prototyping Circuits
” on page
6
.

To wire this on the breadboard, we suggest that you first position your Arduino and breadboard as shown in
Figure 2-7
. (This will be the standard layout throughout the book.) Then, find a red LED and a 330 Ω resistor. Bend the resistor legs as shown in
Figure 2-8
so that the resistor is easier to insert into the breadboard. We suggest using wire cutters to trim both resistor legs by about half their length to make the resistor easier to work with. Resistors aren’t polarized like LEDs, so you don’t have to keep track of which leg is positive or negative.

FIGURE 2-8:
Bending a resistor

Figure 2-9
shows a diagram of a typical breadboard. Most breadboards have labeled columns and numbered rows as references. Using these reference points, insert the LED into your breadboard as shown in
Figure 2-7
. The long, positive leg (anode) should be in column E, row 1 (E1) on the breadboard, and the short, negative leg (cathode) should be in column E, row 2 (E2). Now, find a 330 Ω (orange-orange-brown) resistor. Insert one leg of the resistor into any hole in row 2 of the breadboard to connect the resistor to the short leg of the LED. In our diagram, we insert this leg of the resistor into A2 on the breadboard. On all standard breadboards, for each row, columns A–E are connected, and columns F–J are connected. Now, insert the other leg of the resistor into the breadboard’s
negative power rail
, which is the column marked with a blue or black line and a – (minus) symbol.

FIGURE 2-9:
A breadboard has numbered rows and columns labeled with letters.

Add Power to the Breadboard

Grab two male-to-male jumper wires. We suggest using black for ground (GND) and red for power, and that’s the convention we’ll follow throughout this book.

Connect the black wire from the GND pin on the Arduino to the negative power rail on the breadboard. There are three pins labeled GND on the Arduino. You can use any of these. The power for each LED will actually come from the digital pins. Since pin 13 will power the red LED, connect a wire from pin 13 on the Arduino to A1 on the breadboard.

Plug your Arduino board into your computer using a USB cable, and the “Hello, world!” sketch from
Project 1
should run, causing your LED to blink. In fact, both the LED on the breadboard and the LED on the Arduino should be blinking, because they’re both wired into pin 13.

If the breadboard LED doesn’t blink but the Arduino one does, double-check your wiring and the orientation of the LED. Make sure that the shorter leg is in the second row of the breadboard, connected to the resistor, and that the resistor is connected to GND through the negative power rail. After you get the red LED blinking, disconnect the Arduino from the computer so that you can safely build the rest of the circuit. It’s best practice to disconnect the board while building your circuit.

Add the Yellow and Green LEDs

Now, connect the yellow LED to pin 12 on the Arduino and the green LED to pin 11; you can follow the same basic instructions you followed for the red LED, but use different pairs of rows for each new LED, as in the final wiring diagram in
Figure 2-10
.

FIGURE 2-10:
The final Stoplight circuit, using pins 11, 12, and 13

Each LED should have its own resistor wired to the ground rail, just like the schematic from
Figure 2-6
. Notice, too, that we gave each LED a little space on the breadboard so that we could have room to plug in wires without messing up other parts of the circuit. Although we suggested a specific way to plug in this circuit, remember that you can use any part of the breadboard—so long as the two wires you’re trying to connect are in the same row. Once you’re done, your circuit should resemble
Figure 2-11
.

FIGURE 2-11:
The completed Stoplight circuit, including the Arduino, LEDs, and resistors

To mimic a real stoplight, this project needs a way to turn on each light for a certain amount of time and then switch to the next one. Fortunately, an Arduino sketch can use all kinds of instructions, including timing commands, to control a circuit.

PROGRAM THE STOPLIGHT

Now, plug your Arduino back into your computer. It’s time to get programming! Open the Arduino IDE to start a new sketch.

Confirm Your IDE Settings

When writing any sketch, you should always start with a little housekeeping. First, check that the Board type and Port are properly set. Click
Tools

Board
now. If you’re using the SparkFun RedBoard or a standard Arduino Uno, select
Arduino/Genuino Uno
. Then, click
Tools

Port
. In Windows, your Arduino should be set to the highest numbered COM port. On OS X or Linux, the port should be listed as
/dev/cu.usbserial-A
, where

is a string of random characters unique to your Arduino.

Create Placeholders for Pin Numbers

With your IDE settings confirmed, you’re ready to create the sketch. As discussed in “
Anatomy of an Arduino Sketch
” on page
27
, a basic Arduino sketch consists of two parts: the
setup()
function and the
loop()
function. That simplified description is true for most simple sketches, but more complex sketches have many different parts. One new part that the Stoplight sketch uses is the
global
namespace
, which is the part of your sketch above the
setup()
function and completely outside of any function. In this space, you can define certain names (
variables
) as placeholders for values, and these values will then be available for all parts of your sketch to use. Arduino sketches can work with several types of values.

Data That Sketches Understand

The Arduino language includes a number of possible
data types
for values, and there are a few you’ll run into often when writing sketches. The following list isn’t exhaustive, but it touches on the big ones and shows how their names appear in code:

Integer (
int
)
A whole number that ranges from –32,768 to 32,767

Float (
float
)
A number that has a decimal point and ranges from –3.4028235E+38 to 3.4028235E+38

Byte (
byte
)
A number that ranges from 0 to 255

Character (
char
)
A single letter, denoted by a set of single quotes, such as
‘a’

String (
String
)
A series of characters, denoted by a set of double quotes, such as
"hello"

Boolean (
Boolean
)
A value of either
true
or
false
, which maps to
1
or
0
in the sketch and
HIGH
or
LOW
in terms of pin output

Arduino sketches require you to specify the data type of a variable when you define it. Let’s look at how that works.

Values That Can Change

Most values you’ll create to use in your sketches will be variables. Think of a variable as a placeholder for a piece of data. That data can be a number, a letter, or even a whole sentence.

Before you can use a variable, you have to
define
it, which includes giving it a name, declaring its data type, and initializing it with a value. It’s a good habit to give a variable a value at the moment you define it, which looks something like this:


int

val =

10;

This variable definition has three parts: the data type

, the name of the variable

, and the variable’s value

. At the end of this line, notice that there is a semicolon—this denotes the end of a statement or instruction. The semicolon is very important, and forgetting it is often the root cause of many compiler errors or bugs in code, so be careful to remember it!

When choosing a variable name, you can use any unbroken set of characters, including letters and numbers. There is one caveat here: variables cannot start with a number or consist of any special characters. We suggest making variable names as descriptive as possible, while keeping them short. It’s a chance for you to be a little creative with abbreviating words and descriptions. In this example, we chose to name the variable
val
(short for
value
), and
10
is the variable’s
initialized value
, or the value assigned to a variable to start with. You don’t need to initialize a variable when you define it, but doing both at the same time is helpful and a good practice.

For this project, you’ll create three variables to store pin numbers for the three LEDs the Arduino will control. It’s a lot easier to work with a variable that describes an LED color than it is to try to remember which LED is connected to which pin!

Other books

Nowhere Child by Rachel Abbott
Snowed In by Teodora Kostova
Next Summer by Hailey Abbott
My Lucky Star by Joe Keenan
The Lost by Sarah Beth Durst
Starry Nights by Daisy Whitney
Native Tongue by Suzette Haden Elgin, Susan Squier