CircuitPython Code

Trinket M0 boards can run CircuitPython — a different approach to programming compared to Arduino sketches. In fact, CircuitPython comes factory pre-loaded on Trinket 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 Trinket M0 guide.

These directions are specific to the "M0” boards. The original Trinket 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 Trinket 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 Trinket M0 doesn’t show up as a drive, follow the Trinket M0 guide link above to prepare the board for CircuitPython.

import time
import board
import simpleio
import pulseio
import digitalio

# PWM is not available on Trinket D1
vibration_pin = board.D1    # vibration switch is connected
speaker_pin = board.D2      # PWM speaker
pwm_leds = board.D4         # PWM "fading" LEDs

# initialize PWM for LEDs
pwm = pulseio.PWMOut(pwm_leds, frequency=256, duty_cycle=50)
led_fade_delay = .001       # delay in seconds makes color fade visible
led_fade_step = 1024        # fade amount

# initialize vibration sensor
vpin = digitalio.DigitalInOut(vibration_pin)
vpin.direction = digitalio.Direction.INPUT
vpin.pull = digitalio.Pull.UP

def led_fade(brightness):
    pwm.duty_cycle = brightness
    brightness_start = brightness

    while brightness >= (brightness_start / 2):
        brightness -= led_fade_step
        pwm.duty_cycle = brightness
        time.sleep(led_fade_delay)

while True:
    # wait for vibration sensor detect (reverse logic)
    # play Super Mario Bros. coin sound
    # fade LEDs
    if not vpin.value:
        led_fade((2 ** 16) - 1)                 # full brightness
        simpleio.tone(speaker_pin, 988, 0.083)  # tone1 - B5
        led_fade(2 ** 15)                       # half brightness
        simpleio.tone(speaker_pin, 1319, 0.83)  # tone2 - E6
        led_fade(2 ** 14)                       # quarter brightness
    pwm.duty_cycle = 0                          # turn off LEDs

Installing Libraries:

The simpleio library must be installed for the above code to run correctly. The latest version of the Adafruit CircuitPython Library Bundle contains this library. You want to download the latest stable mpy bundle which will have a filename like this:

adafruit-circuitpython-bundle-x.x.x-mpy-date.zip

The Trinket M0 has limited space, but so in this case we will be selective about which files are copied over to the CIRCUITPY drive. A detailed explanation for installing libraries is available.

Copy the following file from the unzip'd CircuitPython Library Bundle to the CIRCUITPY drive to a new folder called 'lib'. 

  • simpleio.mpy
This guide was first published on Nov 20, 2014. It was last updated on Nov 20, 2014. This page (CircuitPython Code) was last updated on Jun 16, 2019.