Hardware Reference
In-Depth Information
Now, suppose one of those resistors is replaced with a variable resistor, such
as a photoresistor. Photoresistors (see Figure 3-10) change resistance depend-
ing on the amount of light that hits them. In this case, I'll opt to use a 200kΩ
photoresistor. When in complete darkness, its resistance is about 200kΩ; when
saturated with light, the resistance drops nearly to zero. Whether you choose
to replace R1 or R2 and what value you choose to make the fixed resistor will
affect the scale and precision of the readings you receive. Try experimenting
with different configurations and using the serial monitor to see how your values
change. As an example, I will choose to replace R1 with the photoresistor, and
I'll make R2 a 10kΩ resistor (see Figure 3-11). You can leave the RGB LED in place
for now, though you'll only use one of the colors for this exercise.
Figure 3-10: Photoresistor
Load up your trusty serial printing sketch again (Listing 3-1) and try chang-
ing the lighting conditions over the photoresistor. Hold it up to a light and cup
it with your hands. Odds are, you aren't going to be hitting the full range from
0 to 1023 because the variable resistor will never have a resistance of zero. Rather,
you can probably figure out the maximum and minimum values that you are
likely to receive. You can use the data from your photoresistor to make a more
intelligent nightlight. The nightlight should get brighter as the room gets darker,
and vice versa. Using your serial monitor sketch, pick the values that represent
when your room is at full brightness or complete darkness. In my case, I found
that a dark room has a value of around 200 and a completely bright room has
a value around 900 . These values will vary for you based upon your lighting
conditions, the resistor value you are using, and the value of your photoresistor.
UsingAnalogInputstoControlAnalogOutputs
Recall that you can use the analogWrite() command to set the brightness of an
LED. However, it is an 8-bit value; that is, it accepts values between 0 and 255
only, whereas the ADC is returning values as high as 1023. Conveniently, the
Arduino programming language has two functions that are useful for mapping
between two sets of values: the map() and constrain() functions. The map()
function looks like this:
output = map(value, fromLow, fromHigh, toLow, toHigh)
Search WWH ::




Custom Search