CircuitPython

Getting Familiar

CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

If you haven't used CircuitPython together before, make sure you've updated it with the latest version of CircuitPython. This guide will show you how.

Demo Code

Plug your Grand Central into your computer (mac/PC/Linux) via a known good USB A to micro-B cable. Your board should appear to the computer as a flask disk drive named CIRCUITPY. If you see a disk name GCM4BOOT, try to press the reset button again. If the only drive name you get is GCM4BOOT, CircuitPython may not be loaded on the board. You can load CircuitPython per this guide.

Copy code.py from the link below and put it in CIRCUITPY root directory. You can work with this code in any text editing application, or open and save with Mu if you prefer. 

# Adafruit Grand Central Robot Xylophone Demo Program
# Dano Wall and Mike Barela for Adafruit Industries
# MIT License

import time
import board
from digitalio import DigitalInOut, Direction

solenoid_count = 8  # Set the total number of solenoids used
start_pin = 2       # Start at pin D2

# Create the input objects list for solenoids
solenoid = []
for k in range(start_pin, solenoid_count + start_pin + 1):
    # get pin # attribute, use string formatting
    this_solenoid = DigitalInOut(getattr(board, "D{}".format(k)))
    this_solenoid.direction = Direction.OUTPUT
    solenoid.append(this_solenoid)

STRIKE_TIME = 0.01  # Time between initiating a strike and turning it off
TIME_BETWEEN = 0.5  # Time between actions in seconds

song = [3, 4, 5, 4, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 4, 5, 4, 3, 3, 3, 2, 2, 3, 4, 5]

def play(key, time_to_strike):
    solenoid[key].value = True
    time.sleep(time_to_strike)
    solenoid[key].value = False

def rest(time_to_wait):
    time.sleep(time_to_wait)

while True:
    # Play each of the bars
    for bar in range(solenoid_count):
        play(bar, STRIKE_TIME)
        rest(TIME_BETWEEN)

    time.sleep(1.0)  # Wait a bit before playing the song

    # Play the notes defined in song
    # simple example does not vary time between notes
    for bar in range(len(song)):
        play(song[bar], STRIKE_TIME)
        rest(TIME_BETWEEN)

    time.sleep(1.0)

The demonstration shows a simple scale and a short tune. You can use the play function to strike the various tones for a specified length of time then rest between notes. This allows you to do more complex melodies.

Have fun with your new musical instrument!

Troubleshooting

Problem: Grand Central M4 isn't recognized by Mu!

Solution: Make sure your board is set up with CircuitPython, which has the board show up as a flash drive named CIRCUITPY when you connect it to your computer. If it is showing up as GCM4BOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.

This guide was first published on Feb 18, 2019. It was last updated on Feb 18, 2019.
This page (CircuitPython) was last updated on May 27, 2020.