The Circuit Playground Express and Bluefruit have a built-in speaker above the music note printed on the board. It is the grey box with a + on it, below button A, to the left of the slide switch. Though the image is of the Circuit Playground Express, the speaker is in the same location on the Bluefruit. This speaker is capable of multiple things including the ability to play tones.

Add the following code to your Remember, if you need help with this, check here.

"""This example plays two tones for 1 second each. Note that the tones are not in a loop - this is
to prevent them from playing indefinitely!"""
from adafruit_circuitplayground import cp

cp.play_tone(262, 1)
cp.play_tone(294, 1)

When you save the code, you'll have two tones!

First we import cp. Then, we play one tone, followed by another with cp.play_tone(262, 1) and cp.play_tone(294, 1).

Note that we did not include a loop in this code. This is because if the code is in a loop, it will continue playing indefinitely. This is not always desirable, so we've designed the code to play each tone once.

cp.play_tone() requires two things from you: a frequency in hertz and a length of time in seconds. So anytime you want to use it, you'll add cp.play_tone(frequency, seconds) to your code, where frequency is the hertz of the tone you'd like to play, and seconds is the length of time you'd like it to play.

There are many tone generators available on the internet that will give you the hertz of a specific tone. The two tones we've added to the current code are middle C and the D above middle C. Try adding another tone. Have fun with it!

Two Tone Buttons

You can use any of the inputs that we've talked about to play tones. Let's try using the buttons. Add the following code to your

"""This example plays a different tone for a duration of 1 second for each button pressed."""
from adafruit_circuitplayground import cp

while True:
    if cp.button_a:
        cp.play_tone(262, 1)
    if cp.button_b:
        cp.play_tone(294, 1)

Now, press each button. Each one plays a tone for one second!

This code is the same as previous code using the buttons. Inside the loop, it checks to see if each button is pressed. This time, if button A is pressed, it plays a 262Hz tone for 1 second, and if button b is pressed, it plays a 294Hz tone for 1 second.

You can use any of the inputs we've discussed in this guide to trigger a tone. Try replacing the button presses with touch pads. Have fun with it!

This guide was first published on Jun 05, 2018. It was last updated on Jun 05, 2018.

This page (Play Tone) was last updated on Nov 06, 2020.