CircuitPython Code

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.

# Close Encounters Hat with 10 NeoPixels
# ported from Leslie Birch's Arduino to CircuitPython
#
# 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 neopixel
import simpleio

# Initialize input/output pins
photocell_pin = board.A1  # cds photocell connected to this ANALOG pin
speaker_pin = board.D0  # speaker is connected to this DIGITAL pin
pixpin = board.D1  # pin where NeoPixels are connected
numpix = 10  # number of neopixels`
darkness_min = (2 ** 16 / 2)  # more dark than light > 32k out of 64k
photocell = analogio.AnalogIn(photocell_pin)
strip = neopixel.NeoPixel(pixpin, numpix, brightness=.4)

# this section is Close Encounters Sounds
toned = 294
tonee = 330
tonec = 262
toneC = 130
toneg = 392


def alien():
    strip[8] = (255, 255, 0)  # yellow front
    strip[3] = (255, 255, 0)  # yellow back
    simpleio.tone(speaker_pin, toned, 1)  # play tone for 1 second

    time.sleep(.025)

    strip[8] = (0, 0, 0)  # clear front
    strip[3] = (0, 0, 0)  # clear back

    time.sleep(.025)

    strip[7] = (255, 0, 255)  # pink front
    strip[2] = (255, 0, 255)  # pink back
    simpleio.tone(speaker_pin, tonee, 1)  # play tone for 1 second

    time.sleep(.025)

    strip[7] = (0, 0, 0)  # clear front
    strip[2] = (0, 0, 0)  # clear back

    time.sleep(.025)

    strip[4] = (128, 255, 0)  # green front
    strip[9] = (128, 255, 0)  # green back
    simpleio.tone(speaker_pin, tonec, 1)  # play tone for 1 second

    time.sleep(.025)

    strip[4] = (0, 0, 0)  # clear front
    strip[9] = (0, 0, 0)  # clear back

    time.sleep(.025)

    strip[5] = (0, 0, 255)  # blue front
    strip[0] = (0, 0, 255)  # blue back
    simpleio.tone(speaker_pin, toneC, 1)  # play tone for 1 second

    time.sleep(.075)

    strip[5] = (0, 0, 0)  # clear front
    strip[0] = (0, 0, 0)  # clear back

    time.sleep(.1)

    strip[6] = (255, 0, 0)  # red front
    strip[1] = (255, 0, 0)  # red back
    simpleio.tone(speaker_pin, toneg, 1)  # play tone for 1 second

    time.sleep(.1)

    strip[6] = (0, 0, 0)  # clear front
    strip[1] = (0, 0, 0)  # clear back

    time.sleep(.1)


# Loop forever...
while True:

    # turn lights and audio on when dark
    # (less than 50% light on analog pin)
    if photocell.value > darkness_min:
        alien()  # close Encounters Loop

This code requires the neopixel.mpy and simpleio.mpy libraries. A factory-fresh board will have neopixel.py installed. If you’ve just reloaded the board with CircuitPython, create the “lib” directory and obtain neopixel.mpy and simpleio.mpy from the latest Adafruit_CircuitPython_Bundle

This guide was first published on Aug 06, 2014. It was last updated on Aug 06, 2014. This page (CircuitPython Code) was last updated on Apr 05, 2019.