Nightlight Pro

Here's the Pro version of the nightlight. Instead of being limited to the plain green color at one intensity, you can change the colors and brightness of the RGB LED using the capacitive touch pads on the Gemma M0.  Pads D0 and D2 on one side lower and raise the brightness, and D1 on the other side changes the color. You can tap the pads or hold them down. If you don't like the colors or the brightness range or steps in the program below, feel free to change them.

You need just one library, adafruit_dotstar. If it's not already in the lib folder on your CIRCUITPY drive, download the latest version, which will be in a .zip file at the link below. Choose the right version to match your version of CircuitPython, 2.x or 3.x, unzip it, and copy adafruit_dotstar.mpy to CIRCUITPY.

Copy the program below, and save it as code.py on your CIRCUITPY drive.

import time

import board
import touchio
import adafruit_dotstar

# Red, green, blue, and simple mixes of 2 or 3.
# Add your own choices here.
COLORS = (
    (0, 255, 0),
    (0, 0, 255),
    (255, 0, 0),
    (0, 255, 255),
    (255, 255, 0),
    (255, 0, 255),
    (255, 255, 255),
)

# The two left touch pads adjust the brightness.
# The right touch pad changes colors.
# Hold down or just tap.
brightness_down = touchio.TouchIn(board.D0)
brightness_up = touchio.TouchIn(board.D2)
change_color = touchio.TouchIn(board.D1)

dotstar = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
BRIGHTNESS_STEPS = 15
# Start at medium brightness, green.
brightness_step = 8
color_index = 0

while True:
    if brightness_down.value:
        # Don't go below 1.
        brightness_step = max(1, brightness_step - 1)

    if brightness_up.value:
        # Don't go above BRIGHTNESS_STEPS.
        brightness_step = min(BRIGHTNESS_STEPS, brightness_step + 1)

    if change_color.value:
        # Cycle through 0 to len(COLORS)-1 and then wrap around.
        color_index = (color_index + 1) % len(COLORS)

    # Scale brightness to be 0.0 - 1.0.
    dotstar.brightness = brightness_step / BRIGHTNESS_STEPS
    dotstar.fill(COLORS[color_index])

    time.sleep(0.2)
This guide was first published on Jul 07, 2018. It was last updated on Jul 07, 2018. This page (Nightlight Pro) was last updated on Oct 08, 2019.