CircuitPython

Below is CircuitPython code that works similarly (though not exactly the same) as the Arduino sketch. 

Your Gemma M0 already comes with CircuitPython but maybe there's a new version, or you overwrote your Gemma M0 with Arduino code. In that case, if you need to reinstall or update CircuitPython Gemma M0, see the set up CircuitPython Quick Start!

This code requires the neopixel.py library. A factory-fresh board will have this already installed. If you’ve just reloaded the board with CircuitPython, create the /lib directory on the CIRCUITPY flash drive that appears when you plug the Gemma M0 in and then download neopixel.py from Github.

To use this, plug the GEMMA M0 into USB - it should show up on your computer as a small flash drive. Edit the file code.py with your text editor of choice. Select and copy the code below and paste it into that file, entirely replacing its contents (don’t mix it in with lingering bits of old code). When you save the file, the code should start running almost immediately (if not, follow the GEMMA M0 guide link to troubleshoot).

If GEMMA M0 doesn’t show up as a drive, follow the GEMMA M0 guide link above to prepare the board for CircuitPython.

import time

import board
import neopixel

try:
    import urandom as random
except ImportError:
    import random

numpix = 3  # Number of NeoPixels
pixpin = board.D1  # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=.1, auto_write=True)
colors = [
    [30, 144, 255],  # Dodger Blue
    [232, 100, 255],  # Purple
    [204, 0, 204],  # Pink
    [200, 200, 20],  # Yellow
    [30, 200, 200],  # Blue
]


def flash_random(wait, howmany):
    for _ in range(howmany):

        c = random.randint(0, len(colors) - 1)  # Choose random color index
        j = random.randint(0, numpix - 1)  # Choose random pixel
        strip[j] = colors[c]  # Set pixel to color

        for i in range(1, 5):
            strip.brightness = i / 5.0  # Ramp up brightness
            time.sleep(wait)

        for i in range(5, 0, -1):
            strip.brightness = i / 5.0  # Ramp down brightness
            strip[j] = [0, 0, 0]  # Set pixel to 'off'
            time.sleep(wait)


while True:
    # first number is 'wait' delay, shorter num == shorter twinkle
    flash_random(.01, 1)
    # second number is how many neopixels to simultaneously light up
    flash_random(.01, 3)
    flash_random(.01, 2)
This guide was first published on Jan 06, 2019. It was last updated on Jan 06, 2019. This page (CircuitPython) was last updated on Aug 07, 2019.