CircuitPython Code

Trinket M0 Setup for CircuitPython

Now it's time to set up the Trinket M0 for use with CircuitPython and our 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!

# 3D_Printed_Guardian_Sword
# https://learn.adafruit.com/breath-of-the-wild-guardian-sword-led-3d-printed

import time

import board
import neopixel

pin = board.D4  # DIGITAL IO pin for NeoPixel OUTPUT from GEMMA
pixel_count = 93  # number of neopixels
delayval = .01  # 10 ms delay

APIXELS = 14  # number of first orange pixels
BPIXELS = 84  # number of blue pixels
CPIXELS = 93  # second orange pixels

# initialize neopixels
pixels = neopixel.NeoPixel(pin, pixel_count, brightness=1, auto_write=False)

while True:

    # For the first 14 pixels, make them orange,
    # starting from pixel number 0.
    for i in range(0, APIXELS):
        # Set Pixels to Orange Color
        pixels[i] = (255, 50, 0)
        # This sends the updated pixel color to the hardware.
        pixels.write()
        # Delay for a period of time (in milliseconds).
        time.sleep(delayval)

    # Fill up 84 pixels with blue,
    # starting with pixel number 14.
    for i in range(APIXELS, BPIXELS):
        # Set Pixels to Orange Color
        pixels[i] = (0, 250, 200)
        # This sends the updated pixel color to the hardware.
        pixels.write()
        # Delay for a period of time (in milliseconds).
        time.sleep(delayval)

    # Fill up 9 pixels with orange,
    # starting from pixel number 84.
    for i in range(BPIXELS, CPIXELS):
        # Set Pixels to Orange Color
        pixels[i] = (250, 50, 0)
        # This sends the updated pixel color to the hardware.
        pixels.write()
        # Delay for a period of time (in milliseconds).
        time.sleep(delayval)

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 May 10, 2017. It was last updated on May 10, 2017. This page (CircuitPython Code) was last updated on Dec 12, 2018.