GEMMA M0 boards can run CircuitPython — a different approach to programming compared to Arduino sketches. In fact, CircuitPython comes factory pre-loaded on GEMMA M0. If you’ve overwritten it with an Arduino sketch, or just want to learn the basics of setting up and using CircuitPython, this is explained in the Adafruit GEMMA M0 guide.

These directions are specific to the “M0” GEMMA board. The original GEMMA with an 8-bit AVR microcontroller doesn’t run CircuitPython…for those boards, use the Arduino sketch on the “Arduino code” page of this guide.

Below is CircuitPython code that works similarly (though not exactly the same) as the Arduino sketch shown on a prior page. To use this, plug the GEMMA M0 into USB…it should show up on your computer as a small flash drive…then edit the file “main.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, see notes at the bottom of this page).

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

# SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
from rainbowio import colorwheel
import board
import neopixel
from digitalio import DigitalInOut, Direction

try:
    import urandom as random
except ImportError:
    import random

pixpin = board.D1
numpix = 16

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

strip = neopixel.NeoPixel(pixpin, numpix, brightness=.2, auto_write=True)

colors = [
    [232, 100, 255],  # Purple
    [200, 200, 20],  # Yellow
    [30, 200, 200],  # Blue
]


# Fill the dots one after the other with a color


def colorWipe(color, wait):
    for j in range(len(strip)):
        strip[j] = (color)
        time.sleep(wait)


def rainbow(wait):
    for j in range(255):
        for i in range(len(strip)):
            idx = int(i + j)
            strip[i] = colorwheel(idx & 255)
        time.sleep(wait)


# Slightly different, this makes the rainbow equally distributed throughout


def rainbow_cycle(wait):
    for j in range(255 * 5):
        for i in range(len(strip)):
            idx = int((i * 256 / len(strip)) + j)
            strip[i] = colorwheel(idx & 255)
        time.sleep(wait)


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, 8)
    # second number is how many neopixels to simultaneously light up
    flash_random(.01, 5)
    flash_random(.01, 11)

    colorWipe((232, 100, 255), .1)
    colorWipe((200, 200, 20), .1)
    colorWipe((30, 200, 200), .1)

    rainbow_cycle(0.05)

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 and then download neopixel.py from Github.

This guide was first published on Mar 05, 2014. It was last updated on Mar 13, 2024.

This page (CircuitPython Code) was last updated on Mar 13, 2024.

Text editor powered by tinymce.