CircuitPython Code

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 prior page of this guide.

Below is CircuitPython code that works similarly to 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 board
import neopixel
import time
try:
	import urandom as random  # for v1.0 API support
except ImportError:
	import random

numpix = 17        # Number of NeoPixels
pixpin = board.D1  # Pin where NeoPixels are connected
strip  = neopixel.NeoPixel(pixpin, numpix)

minAlpha   = 0.1   # Minimum brightness
maxAlpha   = 0.4   # Maximum brightness
alpha      = (minAlpha + maxAlpha) / 2  # Start in middle
alphaDelta = 0.01  # Amount to change brightness each time through loop
alphaUp    = True  # If True, brightness increasing, else decreasing

strip.fill([0, 0, 255])  # Fill blue, or change to R,G,B of your liking

while True:  # Loop forever...
	if random.randint(1, 5) == 5:       # 1-in-5 random chance
		alphaUp = not alphaUp       # of reversing direction
	if alphaUp:                         # Increasing brightness?
		alpha += alphaDelta         # Add some amount
		if alpha >= maxAlpha:       # At or above max?
			alpha   = maxAlpha  # Limit to max
			alphaUp = False     # and switch direction
	else:                               # Else decreasing brightness
		alpha -= alphaDelta         # Subtract some amount
		if alpha <= minAlpha:       # At or below min?
			alpha   = minAlpha  # Limit to min
			alphaUp = True      # and switch direction

	strip.brightness = alpha            # Set brightness to 0.0 to 1.0
	strip.write()                       # and issue data to LED strip

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.

Last updated on 2017-09-28 at 06.57.17 PM Published on 2013-10-02 at 12.21.46 PM