CircuitPython Code

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.

Planning

Above is a diagram numbering the NeoPixels in their programatic order. It's handy for writing animations that seem to flow between rings, like the basic sine wave animation here:

And here’s the CircuitPython code, using a table of pixel numbers to sequence the animation:

import time

import board
import neopixel

numpix = 64  # Number of NeoPixels
pixpin = board.D1  # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15)
color = [75, 250, 100]  # RGB color - teal

sine = [  # These are the pixels in order of animation - 36 pixels in total:
    4, 3, 2, 1, 0, 15, 14, 13, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28,
    36, 35, 34, 33, 32, 47, 46, 45, 44, 52, 53, 54, 55, 56, 57, 58, 59, 60]

while True:  # Loop forever...
    for i in range(len(sine)):
        # Set 'head' pixel to color:
        strip[sine[i]] = color
        # Erase 'tail,' 8 pixels back:
        strip[sine[(i + len(sine) - 8) % len(sine)]] = [0, 0, 0]
        strip.write()  # Refresh LED states
        time.sleep(0.016)  # 16 millisecond delay

Here's another fun animation, it's similar to the sine wave but loops back around on itself when it gets to the fourth ring in a "figure eight."

import time

import board
import neopixel

numpix = 64  # Number of NeoPixels
pixpin = board.D1  # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15)
color = [5, 250, 200]  # RGB color - cyan

sine = [  # These are the pixels in order of animation - 70 pixels in total:
    4, 3, 2, 1, 0, 15, 14, 13, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28,
    36, 35, 34, 33, 32, 47, 46, 45, 44, 52, 53, 54, 55, 56, 57, 58, 59, 60,
    61, 62, 63, 48, 49, 50, 51, 52, 44, 43, 42, 41, 40, 39, 38, 37, 36, 28,
    29, 30, 31, 16, 17, 18, 19, 20, 12, 11, 10, 9, 8, 7, 6, 5]

while True:  # Loop forever...
    for i in range(len(sine)):
        # Erase 'tail':
        strip[sine[i]] = [0, 0, 0]
        # Draw 'head,' 10 pixels ahead:
        strip[sine[(i + 10) % len(sine)]] = color
        strip.write()  # Refresh LED states
        time.sleep(0.04)  # 40 millisecond delay

This code randomly flashes pixels in three colors of your choice.

import board
import neopixel

try:
    import urandom as random
except ImportError:
    import random

numpix = 64  # Number of NeoPixels
pixpin = board.D1  # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.0)
colors = [
    [232, 100, 255],  # Purple
    [200, 200, 20],  # Yellow
    [30, 200, 200],  # Blue
]

while True:  # Loop forever...
    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
        strip.write()
    for i in range(5, 0, -1):
        strip.brightness = i / 5.0  # Ramp down brightness
        strip.write()
    strip[j] = [0, 0, 0]  # Set pixel to 'off'
This guide was first published on Dec 18, 2013. It was last updated on Dec 18, 2013. This page (CircuitPython Code) was last updated on Oct 21, 2019.