CircuitPython Code

Program the Trinket M0 w/ CircuitPython

Trinket M0 Setup for CircuitPython

Now it's time to set up the Trinket M0 for use with CircuitPython and our Screaming Cauldron code.

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

After prepping the Trinket 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 Trinket M0 with CircuitPython.

Installing NeoPixel Library

Download the latest adafruit-circuitpython-bundle-xxxx.zip (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 Trinket M0.

Finally, if you had a neopixel.py file that was already in that same lib directory you can delete it (the mpy is a compressed version)

Saving CircuitPython Code

Once you've got things working, you can edit the main.py file on the Trinket 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 Trinket M0, and it immediately runs!

#
# Kaleidoscope_Eyes_NeoPixel_LED_Goggles.py
#
import time

import board
import neopixel

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

numpix = 32  # Number of NeoPixels
pixpin = board.D0  # Pin where NeoPixels are connected

mode = 0  # Current animation effect
offset = 0  # Position of spinny eyes

rgb_colors = ([255, 0, 0],  # red
              [0, 255, 0],  # green
              [0, 0, 255])  # blue

rgb_idx = 0  # index counter - primary color we are on
color = rgb_colors[rgb_idx]
prevtime = 0

pixels = neopixel.NeoPixel(pixpin, numpix, brightness=.3, auto_write=False)

prevtime = time.monotonic()

while True:
    i = 0
    t = 0

    # Random sparks - just one LED on at a time!
    if mode == 0:
        i = random.randint(0, (numpix - 1))
        pixels[i] = color
        pixels.write()
        time.sleep(0.01)
        pixels[i] = (0, 0, 0)

    # Spinny wheels (8 LEDs on at a time)
    elif mode == 1:
        for i in range(0, numpix):
            c = 0

            # 4 pixels on...
            if ((offset + i) & 7) < 2:
                c = color

            pixels[i] = c  # First eye
            pixels[(numpix - 1) - i] = c  # Second eye (flipped)

        pixels.write()
        offset += 1
        time.sleep(0.05)

    t = time.monotonic()

    if (t - prevtime) > 8:  # Every 8 seconds...
        mode += 1  # Next mode
        if mode > 1:  # End of modes?
            mode = 0  # Start modes over

        if rgb_idx > 2:  # reset R-->G-->B rotation
            rgb_idx = 0

        color = rgb_colors[rgb_idx]  # next color assignment
        rgb_idx += 1

        for i in range(0, numpix):
            pixels[i] = (0, 0, 0)

        prevtime = t

Copy that code, and then paste it into a new text document in your favorite text/coding editor. Then, save it to your Trinket M0's CIRCUITPY drive as main.py

This guide was first published on Sep 13, 2013. It was last updated on Sep 13, 2013. This page (CircuitPython Code) was last updated on Aug 21, 2019.