Read The Arduino Inventor's Guide Online
Authors: Unknown
The total voltage across these two resistors is 5 V, and the voltages across
R
1
and
R
2
depend on the ratio of the two resistors’ resistances.
V
out
will be some voltage between 5 V and 0 V, because the voltage is divided between the two resistors. The relationship between
V
out
and the resistor values
R
1
and
R
2
can be characterized by the following equation.
We know what you’re thinking: that looks like math! Well, it is, and math is an important part of electronics, but it doesn’t have to be complicated. We’ll take things slow to make sure everyone understands it as we go along. This little equation is especially helpful when you’re dealing with this photoresistor or any other type of resistive sensor. In the voltage divider circuit, if you replace
R
1
with the photoresistor, you get the circuit shown in
Figure 5-9
.
FIGURE 5-9:
A voltage divider circuit with a photoresistor
The resistance of the photoresistor increases as the light around it gets dim. Now, look at the voltage divider equation. As resistance
R
1
increases, the denominator of the fraction increases, making the entire fraction smaller. That means
V
out
gets smaller as it gets darker.
With this circuit, you can accurately read the amount of light on the photoresistor by connecting
V
out
to an analog input pin on the left-hand side of the Arduino (the pins marked with an A).
Analog
signals are those that can vary across a range of values. Up to this point, you’ve only used the
digital
pins on the right-hand side of the Arduino board. Unlike a push button, which has only two states, the photoresistor can have a range of values based on the brightness of light and the voltage divider circuit. This is the difference between a digital and an analog signal.
That’s all you really need to know to use this voltage divider circuit, but if you want to practice the calculations, see “
Show Me Some Math: Voltage Dividers
” on page
130
.
SHOW ME SOME MATH: VOLTAGE DIVIDERS
Using a multimeter, you can measure the resistance of a photoresistor under different conditions. (For instructions on using a multimeter, see “
Measuring Electricity with a Multimeter
” on page
298
.) When we shined a bright light from a flashlight or cell phone on the photoresistor, we measured a resistance of about 100 Ω. When we covered the photoresistor with our hands, we saw a resistance of about 200 kΩ. With the fixed resistor (
R
2
) set at 10 kΩ, we’d expect to see the following values from the voltage divider in those two situations:
With an input voltage of 5 V, the voltage across the photoresistor varies from 0.24 V to 4.95 V through a range of light levels. We’ll show you how to use the Arduino to read these voltages in this chapter. Pretty cool, right? Math works!
Let’s put the RGB LED and the voltage divider together to build the Night-Light circuit. You’ll start by building the voltage divider circuit with the photoresistor and then add the RGB LED. When you’re done, your breadboard should look like
Figure 5-10
. We’ve also included a circuit diagram in
Figure 5-11
for your reference.
FIGURE 5-10:
Completed prototype circuit
FIGURE 5-11:
Circuit diagram of completed prototype Night-Light circuit
Find your photoresistor (it should look like the one in
Figure 5-7
) and a 10 kΩ resistor. Recall that a 10 kΩ resistor has brown, black, and orange color bands, as shown in
Figure 5-12
. See “
Resistors and Bands
” on page
308
for details on how to determine the value of a resistor from its color bands.
FIGURE 5-12:
10 kΩ resistor (brown-black-orange)
With your parts in hand, build the voltage divider circuit as shown in
Figure 5-13
. It’s good practice to connect both power (5 V) and ground when setting up the breadboard for building circuits, so do that first. Find the ground rail (–) and the power rail (+) on the left side of your breadboard. Connect 5 V on the Arduino to the power rail, and connect GND on the Arduino to the ground rail.
FIGURE 5-13:
Completed voltage divider, using the photoresistor
Next, plug the photoresistor in near the bottom of the breadboard, with each leg in its own row. Plug one side of the 10 kΩ resistor into the same row as one of the photoresistor legs (connecting the two together), and plug the other side of the resistor into a row by itself. Add a wire to connect the 5 V power rail (+) to the photoresistor leg that isn’t connected to the resistor. Then, add another wire to connect the ground rail (–) to the resistor leg that’s in a row by itself.
Finally, connect the photoresistor to the Arduino by running a wire from the breadboard row that’s shared with both a leg from the resistor and a leg from the photoresistor to the Arduino analog input pin A0. This wire is often called the output voltage of the photoresistor, or the
signal wire
. The analog input pins, A0–A5, can all be used to measure a range of voltages.
Notice how the breadboard circuit looks a lot like the diagram in
Figure 5-9
. This is one of the most basic sensor circuits used in Arduino projects. Many other analog sensors, like sensors for flex, temperature, and pressure, are variable resistors, too. To experiment with one of those later, just replace the photoresistor with that sensor.
Remember the Stoplight circuit in
Project 2
? That project had three LEDs. The RGB LED basically squishes those three LEDs together. The RGB LED has four legs, and the longest leg is the common cathode (negative) leg. With your RGB LED oriented as in
Figure 5-5
, find the red leg. Plug the RGB LED into the breadboard so that the red leg is at the top and the longest leg is the second one down, as shown in
Figure 5-14
.
FIGURE 5-14:
Adding the RGB LED to the voltage divider circuit