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.
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.
# SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries # # SPDX-License-Identifier: MIT # Laser Dog Goggles # https://learn.adafruit.com/laser-dog-goggles import time import board import pwmio from adafruit_motor import servo # servo pin for the M0 boards: pwm = pwmio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50) my_servo = servo.Servo(pwm) speed = .04 # 40ms lower value means faster movement max_turn = 180 # rotation range 180 degree, half a circle while True: # move stepper max_turn degrees clockwise for angle in range(0, max_turn, 1): my_servo.angle = angle time.sleep(speed) # move stepper max_turn degrees counter clockwise for angle in range(max_turn, 0, -1): my_servo.angle = angle time.sleep(speed)
This code requires an additional library be installed:
- 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
Page last edited January 21, 2025
Text editor powered by tinymce.