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 Mini 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 “code.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.

# SPDX-FileCopyrightText: 2018 Bill Earl and Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT
#
# Bionic Eye sketch for Adafruit Trinket.
#
# written by Bill Earl for Arduino
# ported to CircuitPython by Mikey Sklar
# for Adafruit Industries
#
# Required library is the Adafruit_SoftServo library
# available at https://github.com/adafruit/Adafruit_SoftServo
# The standard Arduino IDE servo library will not work with 8 bit
# AVR microcontrollers like Trinket and Gemma due to differences
# in available timer hardware and programming. We simply refresh
# by piggy-backing on the timer0 millis() counter
#
# Trinket:        Bat+    Gnd       Pin #0  Pin #2
# Connection:     Servo+  Servo-    Tilt    Rotate
#                 (Red)   (Black)   Servo   Servo
#                                   (Orange)(Orange)

import time
import random
import board
import pwmio
from adafruit_motor import servo

# we are intentionally avoiding Trinket Pin #1 (board.A0)
# as it does not have PWM capability
tilt_servo_pin = board.A2   # servo control line (orange) Trinket Pin #0
rotate_servo_pin = board.A1 # servo control line (orange) Trinket Pin #2

# servo object setup for the M0 boards:
tilt_pwm = pwmio.PWMOut(tilt_servo_pin, duty_cycle=2 ** 15, frequency=50)
rotate_pwm = pwmio.PWMOut(rotate_servo_pin, duty_cycle=2 ** 15, frequency=50)
tilt_servo = servo.Servo(tilt_pwm)
rotate_servo = servo.Servo(rotate_pwm)

# servo timing and angle range
tilt_min = 120      # lower limit to tilt rotation range
max_rotate = 180    # rotation range limited to half circle

while True:

    # servo tilt - on average move every 500ms
    if random.randint(0,100) > 80:
        tilt_servo.angle = random.randint(tilt_min, max_rotate)
        time.sleep(.25)

    # servo rotate - on average move every 500ms
    if random.randint(0,100) > 90:
        rotate_servo.angle = random.randint(0, max_rotate)
        time.sleep(.25)

This code requires an additional library be installed:

  1. adafruit_motor

If you’ve just reloaded the board with CircuitPython, create the “lib” directory and then download the Adafruit CircuitPython Bundle. You can copy 'adafruit_motor' folder into the lib directory.

$ mkdir /Volumes/CIRCUITPY/lib 
$ cp -pr adafruit_motor /Volumes/CIRCUITPY/lib

This guide was first published on Oct 13, 2014. It was last updated on Oct 13, 2014.

This page (CircuitPython Code) was last updated on Oct 22, 2021.

Text editor powered by tinymce.