The Circuit Playground Express and Bluefruit have a light sensor on the right side, near the eye printed on the board. Though the images are of the Circuit Playground Express, the sensor is in essentially the same location on the Bluefruit. It senses the amount of ambient light and returns the light level based on that data. We've made it super easy to use. Let's take a look!

Add the following code to your code.py. Remember, if you need help with this, check here.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example uses the light sensor on your Circuit Playground, located next to the picture of
the eye. Try shining a flashlight on your Circuit Playground, or covering the light sensor with
your finger to see the values increase and decrease."""
import time
from adafruit_circuitplayground import cp

while True:
    print("Light:", cp.light)
    time.sleep(0.2)

Open the serial console and try shining a flashlight at your Circuit Playground. The printed values go up! If you place your hand over the board to block the light, the values go down.

Let's look at the code. First we import time and cp.

Inside our loop, we print to the serial console, Light: followed by the light value, cp.light. Then we have time.sleep(1) to slow down the speed at which it prints to the serial console. If it's too fast, it's really hard to read!

Plotting Light

Let's take a look at these values on the Mu plotter! Add the following code to your code.py:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""If you're using Mu, this example will plot the light levels from the light sensor (located next
to the eye) on your Circuit Playground. Try shining a flashlight on your Circuit Playground, or
covering the light sensor to see the plot increase and decrease."""
import time
from adafruit_circuitplayground import cp

while True:
    print("Light:", cp.light)
    print((cp.light,))
    time.sleep(0.1)

The code is almost identical, but we've added one line, print((cp.light,)).

Note that the Mu plotter looks for tuple values to plot. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14). Since we have only one value, we need to have it print out like (1.0,) - note the parentheses around the number, and the comma after the number. Thus the extra parentheses and comma in print((cp.light,)).

As well, the Mu plotter requires that the tuple value be on a line all its own. That's why we can't simply add extra parenthesis and a comma to the print("Light:", cp.light) line. The plotter doesn't know what to do with it if there's other information in there.

Click on the Plotter button on the top of Mu to see the plotter. Try shining a flashlight on your Circuit Playground and watch the plotter line go up! Remove or block the light with your hand to see it go down. Have fun with it!

NeoPixel Light Meter

You can also use the light values to create a fun light meter using the NeoPixels on your Circuit Playground! Add the following code to your code.py:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example uses the light sensor on the Circuit Playground, located next to the picture of the
eye on the board. Once you have the library loaded, try shining a flashlight on your Circuit
Playground to watch the number of NeoPixels lit up increase, or try covering up the light sensor
to watch the number decrease.
"""

import time
from adafruit_circuitplayground import cp

cp.pixels.auto_write = False
cp.pixels.brightness = 0.3


def scale_range(value):
    """Scale a value from 0-320 (light range) to 0-9 (NeoPixel range).
    Allows remapping light value to pixel position."""
    return round(value / 320 * 9)


while True:
    peak = scale_range(cp.light)
    print(cp.light)
    print(int(peak))

    for i in range(10):
        if i <= peak:
            cp.pixels[i] = (0, 255, 255)
        else:
            cp.pixels[i] = (0, 0, 0)
    cp.pixels.show()
    time.sleep(0.05)

Now try shining the flashlight on your Circuit Playground and watch the LEDs light up!

Slowly remove the light to watch the number of LEDs lit up slowly go down.

Let's take a look at the code. First we import time, and cp.

Next, we set cp.pixels.auto_write = False. This means that anything we tell the LEDs to do will not happen automatically. By default, this is set to True. This means, we tell the LEDs to turn on, and they turn on. If it's set to False, it means we have to include cp.pixels.show() after anything we try to tell the LEDs to do. This is required for this code to work since the LEDs turn on based on the light values.

We set the brightness to 0.3, or 30%.

Next we have a helper function called scale_range. The light values are approximately 0-320 but there are only 10 NeoPixels. So, we include a helper function that scales the 0-320 range to 0-9 so we can map light levels to pixel position.

Our loop begins with setting peak = scale_range(cp.light). Then we print the cp.light values and the peak values.

The next section takes the peak value and says for the total number of LEDs, whatever number peak is equal to or less than, light up that many LEDs, and otherwise turn them off. So, if peak is 4, light up 4 LEDs!

Then we have cp.pixels.show() to make the LEDs light up. And a time.sleep(0.05) to create a little delay.

You can change the number values in cp.pixels[i] = (0, 255, 255) to change the color of the light meter. Give it a try!

This guide was first published on Jun 05, 2018. It was last updated on Mar 19, 2024.

This page (Light) was last updated on Mar 18, 2024.

Text editor powered by tinymce.