Digital Sandbox Experiment Guide
11: Automatic Night Light
We now have all the programming tools we need to make some totally awesome, interactive projects. Let's incorporate the light sensor - another analog input component - to create an automatic night light that turns on when it's dark.
You may not see them, but light sensors are incorporated into all sorts of modern electronic devices. There are light sensors in smartphones, which measure how bright your environment is and adjust the screen brightness accordingly. There are light sensors in smoke detectors that detect particles in the air. Photogates use a light sensor to determine when an object passes a certain point - great for those photo finishes!
The light sensor on the Digital Sandbox is called a photo-transistor. It produces an analog voltage relative to the amount of light it sees. The lower the analog value, the darker the environment. If you cover the sensor completely, you might get the output all the way down to zero. Shine a flashlight on it and you might get a maximized reading of 1023.
Here is the code block setup for this experiment:
There aren't any new blocks, but we may have to adjust the value of the
dark variable to make the night light work perfectly. That's where serial communication will come in handy!
Snap your blocks together to match the diagram above, and upload.
The trick to this experiment is finding the perfect setting for the
dark variable. If your room is nice and bright, the white LEDs on pins 4 and 8 should be off. When the lights are off (or the sensor is covered), the LEDs should light up.
If the lights aren't behaving properly, that's OK! You just need to fine-tune the
dark variable. Open up the serial monitor to view the output of the light sensor. Take note of the sensor's reading when the lights are on, then turn the lights off. What are the values now? Try setting the value of the
dark variable just a bit higher than that.
- If the brightness is right on the border of on/off, the LEDs can blink unpleasantly. Try adding another if statement to catch if the light sensor is right in the middle range; from there you can dim the LEDs based on what the sensor reads.
- Try incorporating the RGB LED into this project. If it's super-bright, make it light up yellow. Kind of dim? Green. Totally dark? Blue.