The Arduino Inventor's Guide (25 page)

The two halves of the breadboard are divided by a ditch that separates the rows. Plug the RGB LED into the right side, starting with the red pin in row 4.

Next, find three 330 Ω resistors; their color bands are orange-orange-brown. Use the three resistors to bridge the red, green, and blue pins across the ditch to open rows on the other side, as shown in
Figure 5-14
. The resistors need to straddle the ditch so that the two ends of the resistors aren’t shorted together. Run a wire from the common cathode (negative) leg of the LED to the ground rail on the left side of the breadboard. Finally, connect the three pins of the RGB LED to the Arduino by running a wire from pin 11 on the Arduino to the resistor connected to the red pin on the breadboard, pin 10 on the Arduino to the resistor for the green pin on the breadboard, and pin 9 on the Arduino to the resistor for the blue pin on the breadboard. When you’re done, it should resemble the diagram in
Figure 5-14
. Notice that the red, green, and blue wires correspond to the red, green, and blue positive legs on the LED.

With the RGB LED hooked up, you can control each color like a separate LED, using pins 9, 10, and 11. Open up the Arduino IDE, and let’s play with this idea!

TEST THE NIGHT-LIGHT WITH BASIC COLOR MIXING

You’ll tackle a few new concepts with this project, starting with how to mix colors using an RGB LED. The RGB LED is really three LEDs in one, and you need to treat it that way in your code. Create a new sketch, and replace the default code with the
setup()
and
loop()
functions in
Listing 5-1
.

LISTING 5-1:
A simple code example to display cyan on the RGB LED

  
void
setup
()
  {

   
pinMode
(11,
OUTPUT
);    
//red
    
pinMode
(10,
OUTPUT
);    
//green
    
pinMode
(9,
OUTPUT
);     
//blue
  }
  
void
loop
()
  {

   
digitalWrite
(11,
LOW
);  
//red

   
digitalWrite
(10,
HIGH
);
//green

   
digitalWrite
(9,
HIGH
);  
//blue
  }

Each pin that controls the RGB LED needs its own
pinMode()
function to set that pin as an
OUTPUT

. Previous projects used the
digitalWrite()
function to turn LEDs on and off with individual pins, and you’re going to do the same with the RGB LED. Using the color wheel in
Figure 5-6
, choose from any of the colors represented. We selected cyan, the combination of green and blue. To create cyan, you need to turn on the green

and blue

LEDs using the
digitalWrite()
function. To make sure that the red LED is off, the code also needs a third
digitalWrite()
function

.

Upload this sketch to your Arduino, and if the circuit is wired up correctly and the code is correct, your RGB LED should glow with a soft cyan light. If it’s a different color or not lighting up at all, check your wiring and the orientation of the RGB LED.

Notice that even though you’re using the RGB LED to mix colors, your code is still pretty similar to the code from other LED projects in this book. Each LED is turned on by a separate digital pin using the
pinMode()
function. You’re just controlling the colors by using multiple
digitalWrite()
functions at the same time.

TRY IT OUT: MIX MORE COLORS!

Try changing the color of the LED on your own, and this time, incorporate red into the color. Can you make magenta? What about yellow? What color do you get when you turn all three colors on? Use the color wheel as a reference.

PROGRAM THE NIGHT-LIGHT

In the Night-Light circuit, the photoresistor is used as a light sensor. The photoresistor voltage divider is connected to analog input pin A0. Recall that the Arduino can be used to measure voltage on any of the analog input pins. You can have the Arduino read the sensor value from that pin using the
analogRead()
function. The
analogRead()
function reads the voltage applied to an analog input pin and returns a value between 0 and 1,023, scaled from a voltage range of 0 V to 5 V. For example, if you applied 2.5 V to A0, the
analogRead(A0)
function would return a value of about 512, or roughly half of 1,023.

As the amount of light hitting the photoresistor changes, its resistance changes, and because of the voltage divider circuit, the voltage on the analog input pin changes, too. Let’s see how to code this on the Arduino. To determine whether the Night-Light should be on or off, you want to read the voltage from the photoresistor and compare it against a value that indicates whether the room is dark or bright. You should already have the circuit wired up, with the RGB LED and the photoresistor hooked up to pin A0.
Listing 5-2
shows the Arduino sketch in its entirety. You can either modify the sketch you made for
Listing 5-1
to match it or just add this code to a brand-new sketch.

LISTING 5-2:
Complete Night-Light code

  
int
calibrationValue;
  
int
lightValue;
  
void
setup
()
  {
    
pinMode
(9,
OUTPUT
);
    
pinMode
(10,
OUTPUT
);
    
pinMode
(11,
OUTPUT
);

   calibrationValue =
analogRead
(A0);
  }
  
void
loop
()
  {

   lightValue =
analogRead
(A0);
    
if
(lightValue < calibrationValue - 50)
    {
      
digitalWrite
(11,
LOW
);   
//red
      
digitalWrite(1
0,
HIGH
);  
//green
      
digitalWrite(9
,
HIGH
);   
//blue
    }

   
else
    {
      
digitalWrite
(11,
LOW
);   
//red
      
digitalWrite
(10,
LOW
);   
//green
      
digitalWrite
(9,
LOW
);    
//blue
    }
  }

Upload this to your Arduino board, and make sure the room you’re in is brightly lit. When you shade the photoresistor enough with your hand, the RGB LED should turn on with a cyan color. If it doesn’t work, try cupping your hands around the photoresistor or covering it with a book or magazine to make sure that it doesn’t sense any light. Now, if you remove your hand and expose the photoresistor to light, the RGB LED should return to being off. Pretty cool! Let’s look at how this works.

Prepare to Check the Light Level

First, the sketch creates the
calibrationValue
and
lightValue
global variables without assigning values to them. Like the code in
Listing 5-1
, the
setup()
function calls
pinMode()
once for each pin of the RGB LED, setting pins 9, 10, and 11 to
OUTPUT
. Next, the sketch takes a single initial calibration reading from the photoresistor

and places it in the
calibrationValue
variable. This is the value the sketch will compare future measured light levels against to decide whether to turn the LED on or not.

Now, jump into the
loop()
function. The
loop()
function repeatedly reads the current light level and stores it in the
lightValue

variable. The value of
lightValue
will be updated every time the
loop()
function repeats.

Control the Night-Light Based on the Light Level

With the initial light level stored in the
calibrationValue
variable and the current light level stored in the
lightValue
variable, the Arduino can compare the two and decide whether to turn the Night-Light on
or off. You can tell the Arduino to do this by using an
if()
statement, a structure that controls the flow of code execution in a sketch. It allows the Arduino to make a decision based on the truth of an
expression
, a mathematical statement that has one of only two outcomes:
true
or
false
. You can see the basic flow of an
if()
statement in
Figure 5-15
.

FIGURE 5-15:
The structural flow of an
if()
statement

This sketch’s
if()
statement checks the expression
lightValue < calibrationValue - 50
. The
<
symbol means “less than,” so this statement reads, “Is
lightValue
less than
calibrationValue
minus 50?” If that expression is
true
, the sketch enacts the code inside the set of curly brackets just underneath the
if()
statement.

As the room gets darker, the voltage coming from the sensor circuit decreases. This expression checks if
lightValue
is significantly smaller than
calibrationValue
, which is
true
if the room has gotten darker. If it has, the sketch sets pins 9 and 10
HIGH
and sets pin 11
LOW
to turn the Night-Light on with a cyan color.

When the
if()
statement is
false
, the Arduino skips to the code that follows. This sketch includes an
else

statement, which gets executed only after an
if()
statement’s expression evaluates to
false
. Inside the
else
statement, the sketch turns off all three pins.

Prevent False Alarms

If the sketch just needs to check whether the light level has changed, why subtract 50 from the
calibrationValue
variable in the expression? Checking
lightValue
against a number smaller than
calibrationValue
increases the tolerance on the sketch. If you
were to use the
lightValue < calibrationValue
expression, your Night-Light would flicker on and off at the smallest changes in light (flip back to “
Logical Comparison Operators
” on page
106
for more on the
<
symbol). Subtracting 50 from the calibration value makes sure that the Night-Light turns on when the light level is more than 50 below the calibration (initial) measurement.

Recalibrate the Night-Light

One last piece of useful information before you prettify your Night-Light is how to reset the calibration value to recalibrate for different light levels. The value of
calibrationValue
is set in the
setup()
function, so it runs only once. When your Arduino is powered, there are two different ways to restart your sketch. First, you can turn the Arduino off and on again, but that’s kind of annoying and unsophisticated. The second way is a little more elegant. As with
Project 4
, you can simply press the reset button shown in
Figure 5-16
to restart your sketch. It works the same way as the reset button on a computer or game console. Every time you press that button, the calibration value for the Night-Light is reset.

FIGURE 5-16:
The reset button in all of its clicky glory

Other books

12 - Nine Men Dancing by Kate Sedley
The Knife That Killed Me by Anthony McGowan
J Speaks (L & J 2) by Emily Eck
Sybil Exposed by Nathan, Debbie
Possession by Elana Johnson
Rot & Ruin by Jonathan Maberry