Use a Light Sensor Instead of the Pushbutton
Now we're going to try an interesting experiment. Take a light sensor, like the one pictured in Figure 5-5. You can get a five-pack of these from RadioShack (part number 276–1657).

Figure 5-5. Light-dependent resistor (LDR)
In darkness, the resistance of a light-dependent resistor (LDR) is quite high. When you shine some light at it, the resistance quickly drops and it becomes a reasonably good conductor of electricity. It is thus a kind of light-activated switch.
Build the circuit that came with Example 4-2 (see "Using a Pushbutton to Control the LED" in Chapter 4), then upload the code from Example 4-2 to your Arduino.
Now plug the LDR onto the breadboard instead of the pushbutton. You will notice that if you cover the LDR with your hands, the LED turns off. Uncover the LDR, and the light goes on. You've just built your first real sensor-driven LED. This is important because for the first time in this book, we are using an electronic component that is not a simple mechanical device: it's a real rich sensor.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access