GEMMA M0 boards can run CircuitPython — a different approach to programming compared to Arduino sketches. In fact, CircuitPython comes factory pre-loaded on GEMMA 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 GEMMA M0 guide.

These directions are specific to the “M0” GEMMA board. The original GEMMA 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 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 GEMMA M0 doesn’t show up as a drive, follow the GEMMA M0 guide link above to prepare the board for CircuitPython.

import time

import analogio
import board
import digitalio
import neopixel

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

num_leds = 24  # 24 LED NeoPixel ring
neopixel_pin = board.D0  # Pin where NeoPixels are connected
vibration_pin = board.D1  # Pin where vibration switch is connected
analog_pin = board.A0  # Not connected to anything
strip = neopixel.NeoPixel(neopixel_pin, num_leds)

default_frame_len = 0.06  # Time (in seconds) of typical animation frame
max_frame_len = 0.25  # Gradually slows toward this
min_frame_len = 0.005  # But sometimes as little as this
cooldown_at = 2.0  # After this many seconds, start slowing down
dim_at = 2.5  # After this many seconds, dim LEDs
brightness_high = 0.5  # Active brightness
brightness_low = 0.125  # Idle brightness

color = [0, 120, 30]  # Initial LED color
offset = 0  # Animation position
frame_len = default_frame_len  # Frame-to-frame time, seconds
last_vibration = 0.0  # Time of last vibration
last_frame = 0.0  # Time of last animation frame

# Random number generator is seeded from an unused 'floating'
# analog input - this helps ensure the random color choices
# aren't always the same order.
pin = analogio.AnalogIn(analog_pin)
random.seed(pin.value)
pin.deinit()

# Set up digital pin for reading vibration switch
pin = digitalio.DigitalInOut(vibration_pin)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP

while True:  # Loop forever...

    while True:
        # Compare time.monotonic() against last_frame to keep
        # frame-to-frame animation timing consistent.  Use this
        # idle time to check the vibration switch for activity.
        t = time.monotonic()
        if t - last_frame >= frame_len:
            break
        if not pin.value:  # Vibration switch activated?
            color = [  # Pick a random RGB color...
                random.randint(32, 255),
                random.randint(32, 255),
                random.randint(32, 255)]
            frame_len = default_frame_len  # Reset frame timing
            last_vibration = t  # Save last trigger time

    # Stretch out frames if nothing has happened in a couple of seconds:
    if (t - last_vibration) > cooldown_at:
        frame_len += 0.001  # Add 1 ms
        if frame_len > max_frame_len:
            frame_len = min_frame_len

    # If we haven't registered a vibration in dim_at ms, go dim:
    if (t - last_vibration) > dim_at:
        strip.brightness = brightness_low
    else:
        strip.brightness = brightness_high

    # Erase previous pixels and light new ones:
    strip.fill([0, 0, 0])
    for i in range(0, num_leds, 6):
        strip[(offset + i) % num_leds] = color

    strip.write()  # and issue data to LED strip

    # Increase pixel offset until it hits 6, then roll back to 0:
    offset = (offset + 1) % 6

    last_frame = t

This code requires the neopixel.py library. A factory-fresh board will have this already installed. If you’ve just reloaded the board with CircuitPython, create the “lib” directory and then download neopixel.py from Github.

 

This guide was first published on May 22, 2015. It was last updated on May 22, 2015.

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

Text editor powered by tinymce.