CircuitPython Sound Box
2018-05-01 | By Adafruit Industries
License: See Original Project 3D Printing
Courtesy of Adafruit
Guide by Ruiz Brothers
DIY Switches for Assistive Tech – Big Button Audio Sound Box
In this guide, you'll learn how to build an Audio Sound Box using Circuit Playground Express and CircuitPython. This device can play back short audio clips and features a really big arcade button that lights up when you press it. It’s a pretty simple project but it’s very useful for the assistive tech community. Similar products are actually quite pricey so a DIY solution can be cost-effective, and of course this is customizable!
Prerequisite Guides
If you’re new to electronics and the Adafruit Circuit Playground Express, I suggest you walk through the following guides to get the basics. The Circuit Python guide will walk you through setting it up.
Electronic Components
The Circuit Playground Express, massive arcade button, PAM8302 amplifier, and speaker are the main electronic components used in this project.
- 1 x Adafruit Circuit Playground Express
- 1 x Adafruit Mono 2.5W Class D Audio Amplifier
- 1 x Mono Enclosed Speaker - 3W 4 Ohm
- 1 x Arcade Button
- 1 x Panel Mount Right Angle 10K Linear Potentiometer
- 1 x Solid Machined Metal Knob - 1" Diameter
- 1 x Panel Mount 1/8" (3.5mm) TRS Jack Connector
- 1 x USB DIY Connector Shell
- 1 x USB Micro-B Breakout Board
- 1 x 3 x AA Battery Holder
Hardware and Supplies
Just a few screws, stickers and wires.
- 1 x JST Extension
- 4 x M2.5 x .45 x 5mm
- 2 x M3 x .45 x 5mm
- 1 x 30AWG Wire
- 1 x Solder Wire
- 1 x Heat Shrink Tubing
- 1 x Mounting Putty Tack
Cool Tools!
These help make the project a smooth building experience. You don't need them all of them, but I recommend them.
- 1 x Spudger
- 1 x Ultimaker 3
- 1 x Wire Strippers
- 1 x Wire Cutters
- 1 x Soldering Iron
- 1 x Panavise
- 1 x Helping Third Hands
Circuit Diagram
Circuit Diagram
This provides a visual reference for wiring of the components. They aren't true to scale, just meant to be used as a reference.
- VOUT from CPX to VIN on PAM8302
- GND from CPX to GND on PAM8302
- A0 from CPX to Pot
- Pot to GND on CPX
- Pot to A+ on PAM8302
- from PAM8302 to GND on PAM8302
- Button to GND on CPX
- Button to A4 on CPX
- Speaker to -+audio output on PAM8302
- TRS to GND and A4 on CPX
- Battery to Pot / JST Extension
Fritzing Diagram
The circuit diagram was assembled in the Fritzing software. All of the components are contained in the source so they can be reused to make new projects. Download and modify the circuit diagram.
Software
Setup Adafruit Circuit Playground Express for CircuitPython
We'll need to get our board set up so we can run CircuitPython code. The first thing we'll need to do is connect the board to your computer with a microUSB cable. Then double-click on the reset button to put it in "UF2" boot-loader mode. The NeoPixels will turn green. The board will then show up as a USB storage device on your computer named "CPLAYBOOT".
Follow the guide below to set up the firmware, once complete, come back here and proceed.
CircuitPython for Circuit Playground Express
Download Adafruit CircuitPython Library Bundle
In order to run the code, we'll need to download some libraries. The download linked below will contain all the libraries available for Circuit Python. To run the code for this project, we only need a few. Unzip the downloaded file and look for the following libraries.
Required Libraries
- Adafruit Neopixel – neopixel.mpy
- Adafruit HID – adafruit_hid
- Adafruit Circuit Playground – adafruit_circuitplayground
Install Circuit Python Libraries
Now that we have all of the libraries and know which ones this project needs, we'll need to copy them onto the Circuit Playground Express USB drive (which will be named CIRCUITPY after flashing the firmware). In the CIRCUITPY drive, create a new folder and name it "lib". Then, copy the libraries to that "lib" folder. The lib folder should contain neopixel.mpy, adafruit_hid and adafruit_circuitplayground.
Upload Code
OK, now it's time to upload the code for this project onto the CIRCUITPY drive. Create a new text document using a text app. Then, copy the code below and paste it into that newly created text document. Save that text document to the CIRCUITPY drive and name it "main.py". Once saved, the code will automatically run and will start working.
Editing Code
You'll want to use the Mu python editor to modify the code. Mu is a simple code editor that works with the Adafruit CircuitPython boards. It's written in Python and works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in so you get immediate feedback from your board's serial output! See the guide below to for download and setup instructions.
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_circuitplayground.express import cpx
import audioio
from adafruit_hid.mouse import Mouse
import neopixel
import time
# Audio file name
# By default we'll use a friendly sample of Adabot
# https://www.youtube.com/watch?v=exlRjDKHGRg
AUDIOFILE = "electrons.wav"
# What pad our button is connected to:
button = DigitalInOut(board.A4)
button.direction = Direction.INPUT
button.pull = Pull.UP
# From 0 to 1.0 - adjust as desired!
cpx.pixels.brightness = 0.5
# also be a 'mouse' if desired (requires being plugged into the tablet/computer)
mouse = Mouse()
#NeoPixel stuff
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
def simpleCircle(wait):
PURPLE = (0x10, 0, 0x10)
BLACK = (0, 0, 0)
for i in range(len(pixels)):
pixels[i] = PURPLE
time.sleep(wait)
for i in range(len(pixels)):
pixels[i] = BLACK
time.sleep(wait)
while True:
if not button.value:
# uncomment this line if you want it to click!
# mouse.click(Mouse.LEFT_BUTTON
audio = audioio.AudioOut(board.SPEAKER, open(AUDIOFILE, "rb"))
audio.play()
while audio.playing:
simpleCircle(.05)
pass # wait for audio to finish
# wait for button to be released
while not button.value:
time.sleep(0.1)
Audio Files
If you'd like to create your own sounds, they will need to be a certain file format. Use audio software or online converters to encode your audio files. You can download our audio sample for reference and to quickly test and get started.
- .Wav File format – 16-bit integer (Little Endian)
- Mono, 22.050 kHz
3D Printing
What If I Don't Have A 3D Printer?
Not to worry! You can use a 3D printing service such as 3DHubs or MakeXYZ to have a local 3D printer operator 3D print and ship your parts to you. This is a great way to get your parts 3D printed by local makers. You could also try checking out your local Library or search for a Maker Space.
3D Printed Parts
All of the parts are 3D printed with FDM type 3D printers using various colored filaments. All of the parts are separated into pieces to make 3D printing easier. Assembly is pretty easy and straightforward. Use the links below to download the STLs files.
Download STLs from Thingiverse
Slice Settings
These parts have been tested and 3D printed on an Ultimaker 2+ and 3 using PLA filament. The parts were sliced using CURA 3.x with the following slice settings.
- 220C extruder temp
- 65c bed temp
- 0.2 layer height
- 0.38 line width
- 2 Wall Line Count – 0.4 nozzle
- 20% infill
- 50mm/s print speed
Enclosure Design
To house all of the electronics I designed a parametric box using Autodesk Fusion 360. Most of the components will be paneled mounted or secured to built-in standoffs with machine screws.
Design Source Files
The camera body assembly was designed in Fusion 360. This can be downloaded in different formats like STEP, SAT and more. Electronic components like the PAM8302, Circuit Playground Express, potentiometer, and speaker can be extracted from the Fusion 360 Archive.
Button Assembly
Disassembling Arcade Button
We'll need to take apart the arcade button in order to embed the CPX. The button is designed to be disassembled so it should be fairly straightforward. We'll start by removing the micro switch from the button assembly.
Remove Micro Switch
Grasp onto the outer button housing and micro switch. Firmly twist the micro switch counterclockwise – It should make a click. Then, pull out the body of the micro switch away from the stem. The micro switch should have the LED housing intact.
Remove Threaded Ring
Unscrew the threaded ring from the stem of the button housing. The outer button housing should loosely pull out from the stem.
Separate Button Housing
Pinch the two white colored tips on the end of the button stem while pushing them into the stem – The white housing should lift up out of the black housing. Separate the two and set aside the compression spring.
Spudger Action
Pry open the dome from the button housing using a spudger or tool with a slim shim edge to get in between the two. Firmly grasp onto the dome while conducting the prying action.
Micro Switch Teardown
Pull back the taller black colored clip from the red side of the micro switch. Rotate the micro switch until it comes loose from the black colored housing. Pull out the LED housing and two metal terminals from the black housing.
Pre-drill Planning
We need to pass wires through the white colored housing so we'll need to drill some holes. We can use the holes from the outer housing as a reference for marking where the holes need to be.
Marking Holes
Place the white colored button housing into the main container. Flip the assembly over and use a sharpie marker to fill the holes.
Slotted Holes
Note the orientation of the legs on the white housing, this ought to be in the correct position to allow proper actuation. The two parts have some give so they can move slightly – you'll want to fill as much space available to create slotted holes.
Marked Holes
Remove the white housing from the container to the marked slotted holes. These don't have to be fully filled, just enough to get a good reference for drilling.
Drill Holes
Now it's time to make drill holes! Your choice of tool, a rotary tool like a Dremel or an impact drill will suffice. To prevent drilling holes into our tabletop, we place a scrap piece of wood under the white housing. This will catch the drill bit when it pokes through the housing.
Drilled Holes
Drilling plastic for too long can cause the material to overheat and melt leaving behind welded chips. I used a pointed sanding bit to grind away material while smoothing out the edges.
Test Fitting
Reinstall the housing to see if the holes line up – Be sure to orient the actuator legs in the correct positions. These holes should allow enough space for wires to pass through.
USB Cable
Wire Lengths
We'll need access the USB port on the CPX once it is embedded inside the arcade button. So, we'll need to DIY a USB cable so that we can still connect the CPX. We'll need five wires to create a micro USB cable.
Wire Tinning
I suggest using 30AWG silicone coated wires. Different colored wires allow you to tell the connections apart from each other. Using wire stripper, remove a bit of insulation from each wire. To prevent the strands of wires from fraying, you can add a bit of solder to tin them.
Soldering Connector
We'll need to solder these wires to the male micro USB connector. Due to the tiny pads, this can be quite difficult – A pointy tipped soldering iron is recommended. Follow the circuit diagram to get the proper connections.
DIY USB Connector
Once soldered, I suggest adding pieces of heat shrink tubing to keep all of the wires bundled together. You'll want to double check your solder joints to ensure they're making proper connections.
Amplifier
Wires for Audio Amplifier
The PAM8302 will need four wired connections. Three longer wires and one shorter wire. These wires will connect to the amplifier to the CPX. The potentiometer will be wired inline with the amp and CPX to allow volume adjustment.
Tinning Wires
Using wire strippers, remove a bit of insulation from the tips of each wire. Then, tin the strands of wire together using a bit of solder. Third helping hands can help keep the wires in place while applying solder. Heat shrink tubing will help keep the wires together.
Amplifier Wires Soldered
Solder the wires to the pins on the PAM8302. The shorter wire should be wired to the A- labeled pin.
Ground Audio Input
The wire connected to the A- pin will need to tie to the GND pin on the PAM8302. The GND pin should share two of the wired connections.
Speaker
Connecting Speaker to Amp
The two wires from the speaker will need to be soldered to the audio output on the PAM8302 amplifier. I do not suggest using the included screw block terminal because it will block the mounting holes – Wires should be soldered directly to the pins.
Speaker Audio Cable
The wire on the speaker is fairly long and can be shorted to keep the connections nice and neat. Use wire cutters to trim the cable short.
Shortened Speaker Wire
The wire can be about the length of the 3D printed box enclosure.
Tinning Speaker Wires
Separate the speaker cable by pulling the two apart. Then, strip each wire using wire strippers. You can tin the exposed strands of wire by applying a bit of solder.
Solder Speaker Wires to Amp
The red colored wire is the (+)positive connection while the black is the (-)negative connection. Solder the two wires from the speaker to the output on the PAM8302.
Pot Switch
Potentiometer and Switch Wires
The potentiometer will need two wires and the JST extension cable. This will need to be connected to both the battery and the PAM8302 audio amplifier.
Tin Wires
More wires mean more wire stripping and tinning! Using wire stripper, remove a bit of insulation from the tips of each wire and apply a bit of solder to tin the strands of wires. Adding a few pieces of heat shrink tubing will keep the wires bundled together.
Solder Wires to Pot
The two wires can then be soldered to the two legs on the potentiometer. The blue wire is connected to the far left leg. The green wire is connected to the right left. Grab the PAM8302 audio amplifier and pick out the wire connected to the A+ pin. This wire needs to be soldered to the middle leg of the potentiometer.
Remove nub
While we're working with the pot, remove the nub protruding from the top. This piece will prevent the pot from being panel mounted. I used flat pliers to grasp the nub and twisted it until it broke away.
Wired Potentiometer
Double check your wiring and ensure the wires are connected to the correct pins/terminals. Now is a good time to apply heat shrink tubing to keep the wires nice and tidy.
JST Extension
JST Extension Cable
We'll need to connect the JST extension cable to the on/off switch on the potentiometer. The cable features a male JST connector on one end and female on the other.
Shortened Wires
You can shorten the wires using wire cutters. The end with the connectors are separated into two wires. The length of the wires is about the length of the wire cutters.
Tinned Wires
Using wire strippers, remove a bit of insulation from the tips of each wire. Then, add a bit of solder to tin the stranded of wire. Bits of heat shrink tubing will keep the wires bundled together when splitting them apart.
Tinning Switch Leads
On the bottom of the potentiometer are two extra legs – These are the terminals for the built-in switch. Apply a bit of solder to them to make it easier to attach the wires from the JST wires.
Solder Wires to Switch
The red wires from the male and female JST wires need to be connected to the two terminals on the potentiometer switch. They should be wired in a "right-angled" orientation – This will make it nice and tidy once panel mounted to the 3D printed box enclosure.
Tie Grounds
The black wires (ground connection) from the male and female JST wire needs to be connected together. Use a piece of heat shrink tubing to insulate the exposed wires.
Wired JST to Pot Switch
Double check all of the wired connections to ensure everything is properly soldered. Now is a good time to adjust wire lengths or add addition heat shrink tubing.
Button
Disassemble Switch
I suggest taking the micro switch out of the housing. The assembly can be taken apart as shown in the photo. We won't be needing the LED, so you can discard it along with the electrode terminals. We will need the black housing bit, so keep that handy.
Wires for Micro Switch
We need two wires to connect the micro switch to the CPX. The polarity doesn't matter for switches but I used different colored wires for consistency. Don't forget the heat shrink!
Wire Tinning
More tinning here, you know the drill! Using wire strippers, remove a bit of insulation from the tips of each wire – Then apply a bit of solder to the strands of wire. Remember, third helping hands can help!
Tinning Terminals
OK, now it's time to wire up the micro switch. The terminals are rather chunky so you'll want to apply a good amount of solder to them.
Solder Wires to Terminals
Attach the wires! Again, polarity doesn't matter all that much but it’s good to keep the colors of the wires consistent with the rest of the connections.
Wired Micro Switch
Note the orientation of the wires. They're set up in a right-angle type position – This will accommodate for the space in the 3D printed box enclosure and keep a low-profile.
Reinstall Button Housing
Once wired, you can put the micro switch back into the black housing part.
TRS Jack
Wires for the TRS jack
TRS audio jacks normally need three wired connections but we only need two for this project. Prep the wires by stripping a bit of insulation from the tips of each wire and apply a bit of solder to the strands to tin them.
Jumper Jack
We'll need to tie the two smaller legs of the TRS jack together. So, I used a piece of header pins to do so. Each leg has a small hole in it, so I slipped on a single header pin to the holes, essentially bridging the two.
Tinning Terminals
Then, I apply some solder to fuse the header pin to the two legs. Be careful not to melt the plastic housing from the button or the header pin. You'll want to work quickly or the plastic will melt – Avoid holding the tip of the iron too long when soldering.
Soldered Wires
Now we can attach the two wires to the larger leg and one of the smaller ones. Polarity doesn't matter as long as one wire is attached to one of the smaller legs, and the other to the larger leg.
Check Solder
You'll want to remove the plastic bit from the header pin and double check to ensure the solder joints are fully fused and making a solid electrical connection.
Wired TRS Jack
The TRS jack is now wired and ready to connect! Don't forget to add heat shrink!
Wired Components
Wiring Check Point
Now is a great time to double check all of the components to ensure everything is wired up correctly. If any wire lengths need to be adjusted or need additional pieces of heat shrink tubing, now is the time to get those set up.
Assembly
Install Button Holder
Fit the 3D printed button holder over the black button housing by inserting the steam through the center hole. The side walls should match up with the nubs on the side of the holder. Rotate the holder until they line up and hold them together.
Install Button to Box
Now fit the stem of the button housing through the center hole on the 3D printed box enclosure. Hold them together and flip the box under. Insert the threaded ring by screwing it onto the threaded stem of the button housing.
Setup Box
Ensure the holes are lined up before fully tightening the threaded ring.
Orient Holes
Flip the box assembly back over and look to see if the holes are all lined up and matching. If the holes are slightly off center, you can reinstall the button housing by rotating it 180 degrees.
Install and Secure Speaker
Place the speaker into the 3D printed box enclosure. Orient the speaker so the cone lines up with the little holes on the side of the box. The tabs of the speaker should slide into the clips inside of the box. You can insert and fasten two M3 x 5mm screws to secure the top tabs on the speaker to the standoffs on the side of the box enclosure.
Tap Mounting Holes
The mounting holes on the PAM8302 audio amplifier are a bit tight, so I suggest using an M2.5 tapping tool to thread the holes. This will make it easier to fasten M2.5 machine screws through the mounting holes.
Secure Amplifier
Place the PAM8302 audio amplifier into the 3D printed box enclosure. Line up the mounting holes with the two standoffs inside the box. Hold the board down while fastening the machine screws to the standoffs.
Install Potentiometer
Insert the shaft of the potentiometer through the inside of the 3D printed box enclosure. Press the shaft all the through the hole in the corner of the box. Hold it up against the top surface and insert the washer and hex nut. Fasten the nut until it's fully tightened and secured to the box.
Thread Amp Wiring
Grab the wiring from the amplifier and thread them through one of the holes in the box. On the top side of the button, pull the wiring all the way through.
Thread JST Power
Do the same wire thread for the JST male connector. I pushed the connector through a different hole.
Install TRS Jack
Insert the TRS jack through the side hole on the inside of the 3D printed box enclosure. While holding it in place, screw the included hex nut to panel mount the jack to the side of the box. Fasten the hex nut until it's fully tightened and secured to the box.
Thread TRS Wires
Insert and thread the wiring from the TRS jack to one of the holes. Here I shared the same hole as the button wiring, for consistency purposes. They're both going to connect to the same pins on the CPX.
Installed TRS/Button Wires
Here's what the inside of the 3D printed box enclosure looks like. Try to keep the wires nice and tidy, so lots of heat shrink tubing is ideal.
Installed USB Cable
Now we can insert the wires from the USB cable through the TOP of the button housing (not the inside). The male USB connector should be on the outside of the 3D printed box enclosure.
Threaded Wiring
And now we have all of the wires threaded through the holes! Up next we'll connect all of these wires to the CPX.
CPX Wiring
CPX Wiring Setup
OK, now it's time to wire up the CPX. Before we get started, its good idea to note which pads will be used. If you did use different colored wires for all of the components, it should be fairly easy to tell the connections apart from each other.
Tinning Pads on CPX
To keep the wiring concealed and hidden, I ended up connecting the wired to the bottom of the board. However, it's a little more difficult because there are no labels on the bottom of the board. Either way, I tinned the pads with solder before attaching the wires.
Wired Connections
I soldered all of the wires oriented "inwards", again trying to keep the wiring nice and neat. The pads are quite large so it was possible to attach multiple wires to them – Like the TRS and micro switch connections, also one of the grounds.
Plug In Power and USB
With the wires now soldered to the pads on the CPX, we can plug in the male JST connector and the male micro USB.
Final Wire Check
Here's the underbelly. Good idea to double check all of the wiring to ensure we soldered everything to the correct pads. If you connected to the bottom side, you may want to flip back and forth and cross-reference the circuit diagram.
Connect USB Breakout
And the last thing to wire up is the cable from the male micro USB connector. We saved this for last because we would not be able to thread either the USB connector nor the breakout board through the holes in the top.
Mount USB Breakout
With the breakout now wired up, we can mount it to the bottom cover of the 3D printed box enclosure. Place the board over the two standoffs with the USB port facing the edge. Use two M2.5 x 5mm machine screws to secure the board to the standoffs.
Connect Battery
Now we can plug in the battery to the female JST connector that resides inside the box. Here I'm using a hefty 2000mAh lipo cell but you can use a AA battery pack. Plenty of room inside the box the AA pack.
Attach Battery
You'll need to attach the battery to the side walls of the 3D printed box enclosure. I used mounting tack but double-stick tape or hot glue will also work. Depends on if you want a more permanent solution or not. Either way, it’s no fun if the battery can jostle around in the box.
Closing Bottom Cover
With everything secured, we can now snap on the bottom cover to the 3D printed box enclosure. Line up the bottom cover so the micro USB breakout is lined up with the small cutout on the side of the box.
Closed Case
The little hole for the USB port should be large enough to allow even the chunkiest of microUSB cables because it's fairly close to the edge of the box.
Install Knob (Optional)
You can attach your own knob to the shaft of the potentiometer if you'd like. I opted in for that large machined knob to match the largeness of the arcade button. This knob has a set screw for securing to the shaft – You'll need an M2 sized Allen key to tighten the set screw.
Test Cover
It's up to you whether or not you want to cover up the CPX board. I didn't at first but I found the included button cover diffuses the neopixel LEDs quite nicely.
Install Dome
Either way, you'll want to reinstall the dome back onto the top of the button housing. Firmly press down on the dome to snap the outer edges to the housing. It may require a good amount of force so don't fret if you think you're applying too much pressure.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum