The Circuit Playground Express has a real speaker! It’s not going to compete with your HiFi stereo, but it can play simple songs and tones.
In this exercise, we will add two sound clips to the board.
To access the speaker, we will need to import the module:
from adafruit_circuitplayground.express import cpx
In the adafruit_circuitplayground.express
file, we can play audio using the cpx.play_file()
function. The name of the audio file will go inside the parentheses like so:
cpx.play_file("just_do_it.wav")
In this exercise, let’s output some sound by clicking button A and button B.
Here are some sound clips from Super Mario Bros:
Note: To preview the sound clips, right-click the links above and choose to open the link in a new tab or window.
Instructions
Download two of the sound files above (right-click and press “Save Link As…”).
And store them in the CIRCUITPY drive:
Inside the while
loop:
- If
cpx.button_a
is pressed, play 1st clip by usingcpx.play_file()
. - If
cpx.button_b
is pressed, play 2nd clip by usingcpx.play_file()
.
Click the Download button to download the code.py file.
Drag and drop the downloaded file into the CIRCUITPY drive on your computer.
Press button A and then press button B. Are there any sounds?
Press Run again when you are ready to move on.