Using Arduino Serial Monitor

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.

Testing the Light Sensor in Arduino

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.

Using CircuitPython REPL

If you want to do the same thing using CircuitPython, here is a simple program to do that:

CircuitPython only works on the Circuit Playground Express.
    # Circuit Playground Express Hello Light Sensor
import time
from adafruit_circuitplayground.express import cpx

# Loop forever
while True:
    value = cpx.light
    print("Light Sensor: {}".format(value))
    time.sleep(1)
  

Save the above to a file named main.py on your Circuit Playground Express. Then read the following guide section for how to setup and access the REPL:

With the above program running on the Circuit Playground Express, you should see output like this in your REPL:

Try playing around with shining lights on the sensor and placing your hand over it to see the value change.

This guide was first published on Oct 11, 2016. It was last updated on Oct 11, 2016.

This page (Hello Light Sensor) was last updated on Oct 03, 2016.

Text editor powered by tinymce.