Hello Light Sensor

To get an idea of how the alarm works, let's start by exploring the light sensor itself. You can read some technical details in the Lesson #0 Guide. And for reference, here is where the light sensor is located on the Circuit Playground.

There is an example sketch included with the Circuit Playground library that we can use to play around with the light sensor. It can be found in the following location:

File -> Examples -> Adafruit Circuit Playground -> Hello_CircuitPlayground -> Hello_LightSensor

With this sketch loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

The current value from the light sensor will be printed once a second.

Play around with shining different lights on the light sensor and changing the distance. Also try covering the sensor with your hand, a sheet of paper, or anything else lying around (like a diary). The value can range from 0 to 1023, with higher numbers meaning more light is getting to the sensor. When you cover the light sensor, you should see the value go down.

Another way to watch the value of the light sensor is to use the Serial Plotter. To do this, let's first modify the code slightly. The code below removes the text from the output and increases the rate at which the value is displayed.

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

int value;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  value = CircuitPlayground.lightSensor();
  
  Serial.println(value);
  
  delay(100);
}

With this sketch loaded and running on the Circuit Playground, open the Serial Plotter.

Tools -> Serial Plotter

The light sensor value will now be plotted like a strip chart as shown below. Again, play around with different lighting conditions and watch the value change.

Now we can explain how the alarm is going to work. With a diary placed on top of the Circuit Playground, the light sensor will have a low reading. If the diary is removed, more light will get to the sensor and the reading will increase. An example of what this would look like is shown below.

So if we simply monitor the value of the light sensor and watch for this increase, we can detect when the diary has been removed and sound an alarm.

Last updated on 2016-10-09 at 11.16.30 AM Published on 2016-10-11 at 08.56.04 PM