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 “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, 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.

# NeoPixie Dust Bag
# learn.adafruit.com/neopixel-pixie-dust-bag

import time

import board
import digitalio
import neopixel

try:
    import urandom as random  # for v1.0 API support
except ImportError:
    import random

neo_pin = board.D0  # DIGITAL IO pin for NeoPixel OUTPUT from GEMMA
touch_pin = board.D2  # DIGITAL IO pin for momentary touch sensor to GEMMA
pixel_count = 30  # Number of NeoPixels connected to GEMMA
delay_sec = .010  # delay between blinks, smaller numbers are faster
delay_mult = 8  # Randomization multiplier, delay speed of the effect

# initialize neopixels
pixels = neopixel.NeoPixel(
    neo_pin, pixel_count, brightness=.4, auto_write=False)

oldstate = True  # counting touch sensor button pushes
showcolor = 0  # color mode for cycling

# initialize capacitive touch input
button = digitalio.DigitalInOut(touch_pin)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

while True:

    rcolor = 100  # swtich cycles colors, initially GOLD
    gcolor = 0
    bcolor = 0

    if showcolor == 0:  # Garden PINK
        rcolor = 242
        gcolor = 90
        bcolor = 255

    elif showcolor == 1:  # Pixie GOLD
        rcolor = 255
        gcolor = 222
        bcolor = 30

    elif showcolor == 2:  # Alchemy BLUE
        rcolor = 50
        gcolor = 255
        bcolor = 255

    elif showcolor == 3:  # Animal ORANGE
        rcolor = 255
        gcolor = 100
        bcolor = 0

    elif showcolor == 4:  # Tinker GREEN
        rcolor = 0
        gcolor = 255
        bcolor = 40

    # sparkling
    # select a random pixel
    p = random.randint(0, (pixel_count - 2))
    # color value from momentary switch
    pixels[p] = (rcolor, gcolor, bcolor)
    pixels.write()

    # delay value randomized to up to delay_mult times longer
    time.sleep(delay_sec * random.randint(0, delay_mult))

    # set to a dimmed version of the state color
    pixels[p] = (int(rcolor / 10), int(gcolor / 10), int(bcolor / 10))
    pixels.write()

    # set a neighbor pixel to an even dimmer value
    pixels[p + 1] = (int(rcolor / 15), int(gcolor / 15), int(bcolor / 15))
    pixels.write()

    # button check to cycle through color value sets
    # get the current button state
    newstate = button.value

    # Check if state changed from high to low (button press).
    if newstate and not oldstate:
        # Short delay to debounce button.
        time.sleep(0.020)

        # Check if button is still low after debounce.
        newstate = button.value

    if not newstate:
        showcolor += 1

    if showcolor > 4:
        showcolor = 0

    # Set the last button state to the old state.
    oldstate = newstate

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 Oct 22, 2014. It was last updated on Oct 22, 2014.

This page (CircuitPython Code) was last updated on Nov 17, 2021.

Text editor powered by tinymce.