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 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 or Adafruit Gemma M0 guide.

These directions are specific to the "M0” boards. The original Trinket and Gemma with an 8-bit AVR microcontroller do not 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 “” 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: GPL-3.0-or-later

Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
License: GPLv3

import time

import analogio
import board
import pwmio

    import urandom as random  # for v1.0 API support
except ImportError:
    import random

# Initialize photocell
photocell_pin = board.A1  # cds photocell connected to this ANALOG pin
darkness_max = (2 ** 16 / 2)  # more dark than light > 32k out of 64k
photocell = analogio.AnalogIn(photocell_pin)

# Initialize PWM
# PWM (fading) - Both LEDs are connected on D0
# (PWM not avail on D1)
pwm_leds = board.D0
pwm = pwmio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)
brightness = 0  # how bright the LED is
fade_amount = 1285  # 2% steping of 2^16
counter = 0  # counter to keep track of cycles

# blink delay
blink_delay = True
blink_freq_min = 3
blink_freq_max = 6

# Loop forever...
while True:

    # turn on LEDs if it is dark out
    if photocell.value < darkness_max:

        # blink frequency and timer
        if blink_delay:
            blink_delay = False
            blink_timer_start = time.monotonic()
            blink_freq = random.randint(blink_freq_min, blink_freq_max)

        # time to blink? Blink once every 3 - 6 seconds (random assingment)
        if (time.monotonic() - blink_timer_start) >= blink_freq:
            blink_delay = True
            pwm.duty_cycle = 0

        # send to LED as PWM level
        pwm.duty_cycle = brightness

        # change the brightness for next time through the loop:
        brightness = brightness + fade_amount

        # reverse the direction of the fading at the ends of the fade:
        if brightness <= 0:
            fade_amount = -fade_amount
            counter += 1
        elif brightness >= 65535:
            fade_amount = -fade_amount
            counter += 1

        # wait for 15 ms to see the dimming effect


        # shutoff LEDs, it is too bright
        pwm.duty_cycle = 0

This guide was first published on Oct 30, 2013. It was last updated on Jul 21, 2024.

This page (CircuitPython Code) was last updated on Jul 21, 2024.

Text editor powered by tinymce.