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!
Coding the Screaming Cauldron
The Trinket M0 will be responsible for a few things:
- Checking the IR distance sensor to see when a hand is getting close and then breaking a threshold
- Controlling the NeoPixel color as the hand gets closer
- Triggering the AudioFX Sound Board to play sounds when a distance threshold is met
Here's the code written in CircuitPython to do all of those things:
# SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries # # SPDX-License-Identifier: MIT # Screaming Cauldron # for Adafruit Industries Learning Guide import time import board import neopixel from analogio import AnalogIn from digitalio import DigitalInOut, Direction led = DigitalInOut(board.D13) # on board red LED led.direction = Direction.OUTPUT aFXPin = DigitalInOut(board.D2) # pin used to drive the AudioFX board aFXPin.direction = Direction.OUTPUT aFXPin.value = False # runs once at startup time.sleep(.1) # pause a moment aFXPin.value = True # then turn it off analog0in = AnalogIn(board.D1) neoPin = board.D0 # pin int0 which the NeoPixels are plugged numPix = 30 # number of NeoPixels pixels = neopixel.NeoPixel(neoPin, numPix, auto_write=0, brightness=.8) pixels.fill((0, 0, 0,)) pixels.show() def get_voltage(pin): return (pin.value * 3.3) / 65536 def remap_range(value, left_min, left_max, right_min, right_max): # this remaps a value from original (left) range to new (right) range # Figure out how 'wide' each range is leftSpan = left_max - left_min rightSpan = right_max - right_min # Convert the left range into a 0-1 range (int) valueScaled = int(value - left_min) / int(leftSpan) # Convert the 0-1 range into a value in the right range. return int(right_min + (valueScaled * rightSpan)) while True: distRaw = analog0in.value # read the raw sensor value print("A0: %f" % distRaw) # write raw value to REPL distRedColor = remap_range(distRaw, 0, 64000, 0, 255) distGreenColor = remap_range(distRaw, 0, 64000, 200, 0) if distRaw > 40000: # at about 4 inches, this goes off! led.value = True aFXPin.value = False time.sleep(.35) else: led.value = False aFXPin.value = True # change pixel colors based on proximity print(distRedColor, distGreenColor, (distRedColor + distGreenColor)) pixels.fill((distRedColor, distGreenColor, 0)) pixels.write() time.sleep(0.1)
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
Your circuit is now ready for testing! Make sure the battery pack is turned on, then press the reset button once on the Trinket M0. you should hear the first scream sound trigger upon reboot, and then it will light the NeoPixels green, waiting to detect an unlucky hand!
Move your hand up and down over the IR sensor and watch the color shift from green to red. When you get too close (about 3-4" away) a sound will trigger, in this case the T00NEXT0.WAV sound. Do it again (if you dare!) and you'll trigger T00NEXT1.WAV and so on, until it loops back around to the first sound.
Next, we'll hide the circuit inside the cauldron!
Page last edited January 21, 2025
Text editor powered by tinymce.