This example demonstrates how to attach a speaker to CRICKIT to make Sparky talk.
Getting Familiar
This uses CircuitPython, which requires installing Mu. If you haven't previously used Mu, this guide will get you started.
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.
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.
Mount the speaker
Time to give Sparky a voice. This small 0.5 Watt speaker is just right, although larger 4 and 8 ohm speakers work also.
Cut out a small square of double sided foam tape.
Use this to stick the speaker to the outside edge of the box.
Then connect the speaker wires to CRICKIT as depicted.
Connect the speaker wires to CRICKIT as shown in the diagram below.
Audio Files
Click the button below to download a collection of audio clips for Sparky.
Unzip, then drag and drop these audio clips onto your CIRCUITPY drive.
If you want to create and use your own wav files, see this guide to ensure they are formatted for playing on CRICKIT.
Load the Code
The CircuitPython code for this project is available below.
Copy and paste this code into your Mu editor.
Save the code to your CIRCUITPY drive in your computer's file exploration program as code.py.
# SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT import os import time import random import board import audioio import audiocore from adafruit_crickit import crickit # Sparky automaton # Find all Wave files on the storage wavefiles = [file for file in os.listdir("/") if (file.endswith(".wav") and not file.startswith("._"))] print("Audio files found: ", wavefiles) # mouth servo mouth_servo = crickit.servo_1 # TowerPro servos like 500/2500 pulsewidths mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500) # Servo angles MOUTH_START = 100 MOUTH_END = 90 # Starting servo location mouth_servo.angle = MOUTH_START # Audio playback object and helper to play a full file a = audioio.AudioOut(board.A0) # Play a wave file and move the mouth while its playing! def play_file(wavfile): print("Playing", wavfile) with open(wavfile, "rb") as f: wav = audiocore.WaveFile(f) a.play(wav) while a.playing: # turn servos, motors, etc. during playback mouth_servo.angle = MOUTH_END time.sleep(0.15) mouth_servo.angle = MOUTH_START time.sleep(0.15) while True: # Play a random quip play_file(random.choice(wavefiles)) # then hang out for a few seconds time.sleep(3)
import os import time import random import board import audioio import audiocore from adafruit_crickit import crickit
Then we look on the little flash drive with os.listdir("/")
to find all ".wav"
audio files. We have a check to make sure we don't include any "._"
files, these are special macOS files we ought to ignore
# Find all Wave files on the storage wavefiles = [file for file in os.listdir("/") if (file.endswith(".wav") and not file.startswith("._"))] print("Audio files found: ", wavefiles)
We get from the crickit
library access to the Servo #1 port, where we have our micro-servo plugged in. The tiny TowerPro SG-9x servos we're using work best with a 500us to 2500us pulse width, so we let the servo know that.
Then we define some helper variables for the angles that correspond to where the mouth starts and ends when the automaton speaks:
# mouth servo mouth_servo = crickit.servo_1 # TowerPro servos like 500/2500 pulsewidths mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500) # Servo angles MOUTH_START = 100 MOUTH_END = 90 # Starting servo location mouth_servo.angle = MOUTH_START
Our helper function play_file will use the audio capabilities of the CircuitPlayground to play the audio file. As it is playing we also move the servo up and down by assigning mouth_servo.angle
to those helper variables above! When the audio is done, the servo stops moving
# Audio playback object and helper to play a full file a = audioio.AudioOut(board.A0) # Play a wave file and move the mouth while its playing! def play_file(wavfile): print("Playing", wavfile) with open(wavfile, "rb") as f: wav = audiocore.WaveFile(f) a.play(wav) while a.playing: # turn servos, motors, etc. during playback mouth_servo.angle = MOUTH_END time.sleep(0.15) mouth_servo.angle = MOUTH_START time.sleep(0.15)
Finally, in our loop we will play a random wavefile that we found on the disk and then pause in between.
while True: # Play a random quip play_file(random.choice(wavefiles)) # then hang out for a few seconds time.sleep(3)
Save and Run!
Your CIRCUITPY drive should include the code file and audio files.
If you have all these covered, you're ready to rock with Sparky!
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.
Problem: My motor isn't moving!
Solution: Make sure you've updated your Circuit Playground Express with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.
Exploring further
If you enjoy CircuitPython and want to continue learning you can find lots more CircuitPython projects on the Adafruit Learn System.
Page last edited January 21, 2025
Text editor powered by tinymce.