Getting Familiar

For this project, we will be using CircuitPython.

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 Circuit Playground Express with CRICKIT before, make sure you've updated it with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Setting Up

To get your CRICKIT for CPX set up to run this code, follow these steps:

1) Make sure you've updated the CircuitPython firmware for CRICKIT from the CRICKIT guide

2) Get the latest library pack, unzip it, and drag the libraries you need over into the /lib folder on CIRCUITPY
https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/

For this project you will need the following libraries:

  • adafruit_crickit.mpy
  • adafruit_seesaw folder
  • neopixel.mpy
# SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Music Box code in CircuitPython - Dano Wall and Anne Barela
# Revised by Ladyada 2019-01-16

from adafruit_crickit import crickit
from analogio import AnalogIn
from rainbowio import colorwheel
import neopixel
import audioio
import audiocore
import board

AUDIO_FILENAME = 'fur-elise.wav'

# Audio output
cpx_audio = audioio.AudioOut(board.A0)
audio = audiocore.WaveFile(open(AUDIO_FILENAME, "rb"))

# Rotating dancer
dancer = crickit.servo_2
dancer.angle = 0
MAX_SERVO_ANGLE = 160
move_direction = 1

# neopixels!
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
pixels.fill((0, 0, 0))

# light sensor
light = AnalogIn(board.LIGHT)


def rainbow(value):
    for i in range(10):
        pixels[i] = colorwheel((value * i) & 255)


while True:
    # turn off LEDs so we can tell if its dark out!
    pixels.brightness = 0
    # read light level
    light_level = light.value
    # turn LEDs back on
    pixels.brightness = 1

    # Turn things off if light level < value, its dark
    if light_level < 2000:
        pixels.fill((0, 0, 0))
        cpx_audio.stop()
    else:
        if not cpx_audio.playing:
            # Start playing the song again
            cpx_audio.play(audio)
        # calculate servo rotation
        if dancer.angle <= 0:
            move_direction = 1
        if dancer.angle > MAX_SERVO_ANGLE:
            move_direction = -1
        # Move servo one degree forward or backward.
        rainbow(int(dancer.angle * 255/MAX_SERVO_ANGLE))
        dancer.angle += move_direction

Uploading

Make sure you've connected the Circuit Playground Express to your computer with a micro USB cable and have Mu open and connected to your board.

Copy and paste the code above into your Mu window.

Once the code is copied into Mu, press the Save button - your code should be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py.

Make sure the file saved to CIRCUITPY is named "code.py", this will allow it to run automatically when your CPX is powered on.

Now it's time to give your music box a song to sing. This free version of Fur Elise is a good one for the music box (courtesy of freesound.org). 

Download the WAV file, the drag and drop that WAV file onto your CIRCUITPY drive.

When you're all done, your CIRCUITPY drive should contain the code.py file, a .wav file, and a /lib folder with the necessary libraries in it.

Troubleshooting

Problem: My Circuit Playground Express isn't recognized by Mu!

Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT 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 Jan 17, 2019. It was last updated on Jan 17, 2019.

This page (CircuitPython Code) was last updated on Sep 05, 2023.

Text editor powered by tinymce.