Playground Light Sensor

The Circuit Playground Express can see you! OK, not really. That would be creepy. 

But, it can sense light and dark, as well as colors and even your pulse!!

The Light Sensor in the upper left of the board (look for the eye icon) is a phototransistor. Here's how to use it as a light sensor:

# CircuitPlaygroundExpress_LightSensor
# reads the on-board light sensor and graphs the brighness with NeoPixels

from simpleio import map_range
from analogio import AnalogIn
import board
import neopixel
import time

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=0, brightness=.05)
pixels.fill((0,0,0))
pixels.show()

analogin = AnalogIn(board.LIGHT)

while True:
    #light value remaped to pixel position
    peak = map_range(analogin.value, 2000, 62000, 0, 9)
    print(analogin.value)
    print(int(peak))

    for i in range(0, 9, 1):
         if i <= peak:
             pixels[i] = (0, 255, 0)
         else:
             pixels[i] = (0,0,0)
    pixels.show()

    time.sleep(0.01)

Copy and paste that code into a text editor and then save it to your Circuit Playground Express as code.py.

The code reads the light sensor and then lights up the NeoPixels like a bar graph depending on the light level. Try waving your hand over it, or shining it with a flashlight to see it change!

Last updated on 2017-12-01 at 04.56.58 PM Published on 2017-10-12 at 10.43.39 PM