Trinket M0 and GEMMA M0 boards can run CircuitPython — a different approach to programming compared to Arduino sketches. In fact, CircuitPython comes factory pre-loaded on these boards. 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 and Adafruit GEMMA M0 guides.
Below is CircuitPython code that works similarly to the Arduino sketch shown on the prior page. To use this, plug the Trinket/GEMMA 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 or GEMMA M0 doesn’t show up as a drive, follow the corresponding guide link above to prepare the board for CircuitPython.
# SPDX-FileCopyrightText: 2017 Limor Fried/ladyada for Adafruit Industries # SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries # # SPDX-License-Identifier: MIT """ Adafruit Trinket/Gemma Example: Simple Theramin Read the voltage from a Cadmium Sulfide (CdS) photocell voltage divider and output a corresponding tone to a piezo buzzer Photocell voltage divider center wire to GPIO #2 (analog 1) and output tone to GPIO #0 (digital 0) """ import time import analogio import board import pwmio photocell_pin = board.A1 # CdS photocell connected to this ANALOG pin speaker_pin = board.D0 # Speaker is connected to this DIGITAL pin scale = 0.03 # Change this to adjust tone scale # Initialize input/output pins photocell = analogio.AnalogIn(photocell_pin) pwm = pwmio.PWMOut(speaker_pin, variable_frequency=True, duty_cycle=0) while True: # Loop forever... # Read photocell analog pin and convert voltage to frequency pwm.frequency = 220 + int(scale * float(photocell.value)) pwm.duty_cycle = 32767 # 50% duty cycle time.sleep(0.4) # Play for 400 ms (adjust to your liking) pwm.duty_cycle = 0 # Stop playing time.sleep(0.05) # Delay 50 ms between notes (also adjustable)
Page last edited January 22, 2025
Text editor powered by tinymce.