Skip to Content
Learn
Circuit Playground: Part 2
Audio: Play Tone

The built-in speaker is not only capable of playing sound files; it can also play basic tones with the cpx.play_tone() function.

For example, the code below would play the middle C note for 1.0 second:

cpx.play_tone(262, 1.0)

The cpx.play_tone() requires two things:

  • The frequency of the tone (Hz)
  • The duration of the tone (sec)

Let’s try it out.

Instructions

1.

Inside the while loop:

  • If cpx.button_a is pressed, play a tone by using cpx.play_tone().
  • If cpx.button_b is pressed, play a tone by using cpx.play_tone().

You can choose any frequencies between 0 and 1000, each for as many seconds as you like.

2.

Click the Download button to download the code.py file.

Drag and drop the downloaded file into the CIRCUITPY drive on your computer.

Now, press each button. Each one should play a tone!

Folder Icon

Sign up to start coding

Already have an account?