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 analogio
import board
import simpleio
from digitalio import DigitalInOut, Direction

# setup photocell
photocell = analogio.AnalogIn(board.A1) # analog #1 same pin as Digital #2
darkness_min = (2 ** 16) * .05          # light level < 5% means darkness

# setup speaker
speaker = DigitalInOut(board.D1)
speaker.direction = Direction.OUTPUT

# setup servo
servo = simpleio.Servo(board.D0)        # servo motor
angle = 0

def chirp():
    for i in range(200,180,-1):
        play_tone(i,9)

def play_tone(tone_value, duration):
    microseconds = 10 ** 6              # duration divider, convert to microseconds

    for i in range(0, duration):
        i += tone_value * 2
        speaker.value = True
        time.sleep(tone_value / microseconds)
        speaker.value = False
        time.sleep(tone_value / microseconds)

# loop forever...
while True:

    # when photocell goes dark (less than 5%)
    # turn on audio
    # rotate stepper
    if photocell.value < darkness_min:
        chirp()                         # bird chirp noise
        if servo.angle == 0:
            servo.angle = 180           # rotate bird head 180 degrees
        else:
            servo.angle = 0
        time.sleep(.5)                  # leave some time to complete rotation

Installing Libraries

The simpleio.mpy library must be installed for the above code to run correctly. The latest version of the Adafruit CircuitPython Library Bundle contains both libraries. 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 21, 2013. It was last updated on Nov 21, 2013. This page (CircuitPython Code) was last updated on Jul 15, 2019.