Learn
Circuit Playground: Part 2
Audio: Play File

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.

Speaker

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:

Description Size
Coin 42 KB
Jump 27 KB
Oops 118 KB
Game Over 164 KB

Note: To preview the sound clips, right-click the links above and choose to open the link in a new tab or window.

Instructions

1.

Download two of the sound files above (right-click and press “Save Link As…”).

And store them in the CIRCUITPY drive:

File Icons

Inside the while loop:

  • If cpx.button_a is pressed, play 1st clip by using cpx.play_file().
  • If cpx.button_b is pressed, play 2nd clip by using cpx.play_file().
2.

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.

Folder Icon

Sign up to start coding

Already have an account?