We're going to use CircuitPython, Mu, and the light sensor on Circuit Playground Express to plot color levels. We'll run this code on our Circuit Playground Express and use Mu to plot the color data that CircuitPython prints out.

Save the following as code.py on your Circuit Playground Express board, using the Mu editor:

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

import analogio
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
light = analogio.AnalogIn(board.LIGHT)

while True:
    pixels.fill((0, 0, 0))
    pixels[1] = (255, 0, 0)
    raw_red = light.value

    red = int(raw_red * (255 / 65535))
    pixels[1] = (0, 255, 0)
    raw_green = light.value

    green = int(raw_green * (255 / 65535))
    pixels[1] = (0, 0, 255)
    raw_blue = light.value

    blue = int(raw_blue * (255 / 65535))
    pixels.fill((0, 0, 0))

    # Printed to match the color lines on the Mu plotter!
    # The orange line represents red.
    print((green, blue, red))

Let's take a look at the code!

First we import neopixel, analogio, time and board. Next, we create the pixels object for the NeoPixels and the light object for the light sensor.

LED colors are set using a combination of red, green, and blue, in the form of an (RG, B) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set the LED to red, the tuple would be (255, 0, 0), which has the maximum level of red, and no green or blue. Green would be (0, 255, 0), etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255), with equal amounts of green and blue.

The light sensor works as a color sensor by using the NeoPixel next to it (pixel 1) to flash red, green and blue, and then record the reflected light levels of each color to determine the color of the object being held against it. So, inside our main loop, we need to flash red, green and blue, record the reflected light levels, and then use math to determine the level of each color.

The first thing we do in our main loop is make sure the NeoPixels are off with pixels.fill((0, 0, 0)). Next, we begin with red. We flash pixel[1] red by assigning it (255, 0, 0), for 0.5 seconds. Then we grab the light sensor value and assign it to raw_red.

The light sensor returns a value between 0 and 65535. Since color values are 0-255, we need to use math to scale the raw light sensor value down to a viable color value. So, to get our red value, we use red = int(raw_red * (255/65535)). This takes whatever value the light sensor provides, and returns an equivalent value between 0 and 255. Now we have our red level!

We repeat the exact same steps for green, and then for blue.

The last thing we do, is print our red, green and blue values in the form of a tuple, so we can see them on the plotter! We've changed the order a bit to print((green, blue, red)) so the color of the lines on the plotter match the colors they represent: green for green, blue for blue and orange for red.

Once you have everything setup and running, try holding a colored item up to the light sensor on the Circuit Playground Express, and watch the plotter immediately react! Hold up a green item to watch the green line go higher than the blue or red. Hold up a red item and watch the orange line go higher than the green or the blue!

This is a great way to see color levels sense using the light sensor, and plot the changes as you hold up different colors!

This guide was first published on Apr 09, 2018. It was last updated on Oct 04, 2023.

This page (Color) was last updated on Oct 04, 2023.

Text editor powered by tinymce.