Are you new to using CircuitPython? No worries, there is a full getting started guide here.
If using MakeCode previously, you'll have to go back to switch to CircuitPython mode. Find out how to do that here.
To edit the CircuitPython code and receive realtime feedback in the REPL, Adafruit suggests using the Mu Editor. You can learn about Mu and installation in this tutorial.
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.
Open up the Mu editor or an editor of your choice with a REPL.
Full Code
Download the code below or copy and paste into Mu and save as code.py on the CIRCUITPY drive.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT # Isaac Wellish # Code adapted from Anne Barela's Hello World of Robotics and # Make it Move with Crickit guides at learn.adafruit.com # Power must be plugged into right side of motor 1 on CRICKIT # to turn counter clock wise import time import audioio import audiocore import board import neopixel from digitalio import DigitalInOut, Pull, Direction from adafruit_crickit import crickit # Set audio out on speaker speaker = audioio.AudioOut(board.A0) # Two onboard CPX buttons for input (low level saves memory) button_a = DigitalInOut(board.BUTTON_A) button_a.direction = Direction.INPUT button_a.pull = Pull.DOWN button_b = DigitalInOut(board.BUTTON_B) button_b.direction = Direction.INPUT button_b.pull = Pull.DOWN # Create one motor on seesaw motor port #1 motor = crickit.dc_motor_1 # NeoPixels on the Circuit Playground Express Light Blue pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.7) # Fill them with our favorite color "#0099FF light blue" -> 0x0099FF # (see http://www.color-hex.com/ for more colors and find your fav!) # set pixels to blue on start up pixels.fill(0x0099FF) motorInc = 0 # Start playing the file (in the background) def play_file(wavfile): audio_file = open(wavfile, "rb") wav = audiocore.WaveFile(audio_file) speaker.play(wav,loop = True) while True: if button_a.value: pixels.fill(0xFC4044) play_file("circus_chair.wav") # play WAV file motor.throttle = -0.20 time.sleep(0.2) motor.throttle = -0.11 + motorInc # increase speed motorInc -= 0.01 if button_b.value: speaker.stop() pixels.fill(0x0099FF) # magenta i = motor.throttle while i < -0.05: i += 0.005 motor.throttle = i # slow down! time.sleep(0.1) motor.throttle = 0 # stop motorInc = 0
Import Libraries and Initialize Values
We'll begin the program by importing the necessary libraries and initialize the variables we'll be using.
If you think you might be missing a library, you'll be covered by installing the whole library package. Find out how in the CircuitPython Essentials Guide on CircuitPlayground Libraries.
import time import audioio import audiocore import board import neopixel from digitalio import DigitalInOut, Pull, Direction from adafruit_crickit import crickit # Set audio out on speaker speaker = audioio.AudioOut(board.A0) # Two onboard CPX buttons for input (low level saves memory) button_a = DigitalInOut(board.BUTTON_A) button_a.direction = Direction.INPUT button_a.pull = Pull.DOWN button_b = DigitalInOut(board.BUTTON_B) button_b.direction = Direction.INPUT button_b.pull = Pull.DOWN # Create one motor on seesaw motor port #1 motor = crickit.dc_motor_1 # NeoPixels on the Circuit Playground Express Light Blue pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.7) # Fill them with our favorite color "#0099FF light blue" -> 0x0099FF # (see http://www.color-hex.com/ for more colors and find your fav!) #set pixels to blue on start up pixels.fill(0x0099FF) motorInc = 0
Set up and initialize speaker, buttons, motor and NeoPixels.
Adding Music
The music for this project was provided by robcro6010 on freesound.org a "huge collaborative database of audio snippets, samples, recordings, bleeps, ... released under Creative Commons licenses that allow their reuse"
CircuitPython needs sound files formatted a certain way to play them. See this guide for ensuring your sound files are in the correct format.
Here is a properly formatted .wav file of the music we are using. Click to download:
Once you have the file, place it in the top level (root) directory in your Circuit Playground Express CIRCUITPY drive.
Function for playing .wav files
# Start playing the file (in the background) def play_file(wavfile): audio_file = open(wavfile, "rb") wav = audiocore.WaveFile(audio_file) speaker.play(wav,loop = True)
while True: if button_a.value: pixels.fill(0xFC4044) # magenta play_file("circus_chair.wav") # play WAV file motor.throttle = -0.20 time.sleep(0.2) motor.throttle = -0.11 + motorInc # increase speed! motorInc -= 0.01 if button_b.value: speaker.stop() pixels.fill(0x0099FF) # blue i = motor.throttle while i < -0.05: i += 0.005 motor.throttle = i # slow down! time.sleep(0.1) motor.throttle = 0 # stop motorInc = 0
The while True:
loop will be running forever once the program has entered it.
When Button A is pressed:
- NeoPixels turn magenta
- start the music
- set the motor to value of -20 to get torque needed to start moving
- quickly set motor to -0.11 which will be slowest speed
- decrement
motorInc
by 0.01 - each time Button A is pressed again, the motor will increase in speed by 0.01
When Button B is pressed:
- stop the music
- turn NeoPixels blue
- decrement motor speed over time until motor stops
* Note the values for the motor are negative because I wanted the ride to spin counter-clock wise. I could have also changed the orientation of the motor wires in CRICKIT.
There you have it!
Troubleshooting:
- If the music isn't playing, make sure the file is formatted correctly and that the file is in the top level (called the root) directory of the CIRCUITPY drive.
- Use the REPL in Mu to tackle code related issues.
- More CircuitPython help here.
-
If having motor problems, check that you have a 5V power supply attached to the DC jack on your CRICKIT board and that the on/off switch next to the power jack is in the on position.
Page last edited January 22, 2025
Text editor powered by tinymce.