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

# SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import analogio
import board
from rainbowio import colorwheel
import neopixel

# Initialize input/output pins
sensor_pin = board.A1  # input pin for the potentiometer
sensor = analogio.AnalogIn(sensor_pin)

pix_pin = board.D1  # pin where NeoPixels are connected
num_pix = 8  # number of neopixels
strip = neopixel.NeoPixel(pix_pin, num_pix, brightness=.15, auto_write=False)

color_value = 0
sensor_value = 0


def remap_range(value, left_min, left_max, right_min, right_max):
    # this remaps a value from original (left) range to new (right) range
    # Figure out how 'wide' each range is
    left_span = left_max - left_min
    right_span = right_max - right_min

    # Convert the left range into a 0-1 range (int)
    valueScaled = int(value - left_min) / int(left_span)

    # Convert the 0-1 range into a value in the right range.
    return int(right_min + (valueScaled * right_span))


# Loop forever...
while True:

    # remap the potentiometer analog sensor values from 0-65535 to RGB 0-255
    color_value = remap_range(sensor.value, 0, 65535, 0, 255)

    for i in range(len(strip)):
        strip[i] = colorwheel(color_value)
    strip.write()

This guide was first published on Feb 11, 2015. It was last updated on Feb 11, 2015.

This page (CircuitPython Code) was last updated on May 22, 2023.

Text editor powered by tinymce.