Now it's time to set up the Gemma M0 for use with CircuitPython and our cog animation code.

If you haven't already, follow this guide to preparing the Gemma M0, including updating it with the latest version of CircuitPython.

After prepping the Gemma M0 to run CircuitPython we'll also need to add a NeoPixel library. This guide tells you how, as well as providing a good primer on using NeoPixels on the Gemma M0 with CircuitPython.

Download the (or newer) from the releases directory and then unzip it somewhere easy to find, such as your desktop. Then, copy the neopixel.mpy file to your CIRCUITPY's lib directory on the Gemma M0.

Finally, trash the file that was already in that same lib directory.

Once you've got things working, you can edit the file on the Gemma M0 to adjust what it actually does. No need for a software IDE, complaining tools, or flashing the chip -- when you code with CircuitPython all you need is a text editor. Edit the code, save it to the Gemma M0, and it immediately runs!

The incredible ease of editing files, even when you're on the go, makes Gemma M0 running CircuitPython the perfect combination for cosplay effects and props!

Copy the code from below, and then paste it into your text editor. Then save the file to your Gemma M0 as

# SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries
# SPDX-License-Identifier: MIT

# CircuitPython Gemma Gear goggles
# uses two 16 NeoPixel rings (RGBW)
# connected to Gemma M0 powered by LiPo battery

import time

import board
import neopixel

pixpinLeft = board.D1  # Data In attached to Gemma pin D1
pixpinRight = board.D0  # Data In attached to Gemma pin D0
numpix = 16

# uncomment the lines below for RGB NeoPixels
stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, bpp=3, brightness=.18,
stripRight = neopixel.NeoPixel(pixpinRight, numpix, bpp=3, brightness=.18,

# Use these lines for RGBW NeoPixels
# stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, bpp=4, brightness=.18,
#    auto_write=False)
# stripRight = neopixel.NeoPixel(pixpinRight, numpix, bpp=4, brightness=.18,
#    auto_write=False)

def cog(pos):
    # Input a value 0 to 255 to get a color value.
    # Note: switch the commented lines below if using RGB vs. RGBW NeoPixles
    if (pos < 8) or (pos > 250):
        # return (120, 0, 0, 0) #first color, red: for RGBW NeoPixels
        return (120, 0, 0)  # first color, red: for RGB NeoPixels
    if pos < 85:
        return (int(pos * 3), int(255 - (pos * 3)), 0)
        # return (125, 35, 0, 0) #second color, brass: for RGBW NeoPixels
        # return (125, 35, 0)  # second color, brass: for RGB NeoPixels
    elif pos < 170:
        pos -= 85
        # return (int(255 - pos*3), 0, int(pos*3), 0)#: for RGBW NeoPixels
        return (int(255 - pos * 3), 0, int(pos * 3))  # : for RGB NeoPixels
        pos -= 170
        # return (0, int(pos*3), int(255 - pos*3), 0)#: for RGBW NeoPixels
        return (0, int(pos * 3), int(255 - pos * 3))  # : for RGB NeoPixels

def brass_cycle(wait, patternL, patternR):
    # patterns do different things, try:
    # 1 blink
    # 24 chase w pause
    # 64 chase
    # 96 parial chase
    # 128 half turn
    # 230 quarter turn w blink
    # 256 quarter turn
    for j in range(255):
        for i in range(len(stripLeft)):
            idxL = int((i * patternL / len(stripLeft)) + j)
            stripLeft[i] = cog(idxL & 64)  # sets the second (brass) color
            idxR = int((i * patternR / len(stripRight)) + j)
            stripRight[i] = cog(idxR & 64)  # sets the second (brass) color

while True:
    brass_cycle(0.01, 256, 24)  # brass color cycle with 1ms delay per step
    # patternL, patternR

The code will start running immediately, and the two rings will begin animating!

if you'd like, you can edit the colors and patterns of the cog animation to suit your costume. Look at the comments in the code to see how to make adjustments.

This guide was first published on Oct 03, 2017. It was last updated on Oct 03, 2017.

This page (CircuitPython Setup and Code) was last updated on Apr 28, 2022.

Text editor powered by tinymce.