Link Search Menu Expand Document

Light sensor on the CPX

The Circuit Playground (Classic, Express and Bluefruit) has an on-board ambient light sensor. The Circuit Playground software library handles the low-level details and the CircuitPlayground.lightSensor() function returns an 10-bit integer (\(0\le v \le 1023\)), where a value of \(v\sim300\) corresponds to a well lit office environment

Table of contents

  1. Light Sensor
  2. Reading the Light Sensor with Hello_LightSensor
  3. Light sensor reading scale
  4. A Simple Night Light

Light Sensor

The Circuit Playground Express has an ambient light sensor mounted permanently to the circuit board as shown in the following image.

Select the CPX board

Reading the Light Sensor with Hello_LightSensor

The Hello_LightSensor sketch is part of the Adafruit Circuit Playground library. You can open the sketch by selecting File –> Examples –> Adafruit Circuit Playground –> Hello_CircuitPlayground –>Hello_LightSensor. Running the sketch produces light sensor readings in the Serial Monitor.

Light sensor reading scale

Consult the source code in Hello_LightSensor to see how ambient light readings are made.

The reading of the light sensor is obtained with the statement

  value = CircuitPlayground.lightSensor();

where value is an int. The reading is an 8-bit value, i.e. an integer in the range between 0 and 1023, where 0 is completely dark and 1023 is the maximum brightness the sensor can record. Typical ambient light levels are about 300 on this scale.


A Simple Night Light

The light sensor and NeoPixels on the Circuit Playground can be used to create a simple night light. The basic logic is

  • If the ambient light is below a threshold, turn on the NeoPixels
  • Otherwise, turn off the NeoPixels

The nightlight.ino sketch illustrates the logic of a night light by turning on just one NeoPixel when the ambient light level falls below a threshold. Can you extend this code so that all NeoPixels turn on instead of one when the threshold is reached? Can you choose a pleasing color for the night light? Can you create a physical prototype of a night light out of cardboard to show your design to a potential client?

Download the nightlight1.ino sketch.