Learn
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 usingcpx.play_tone()
. - If
cpx.button_b
is pressed, play a tone by usingcpx.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!
Sign up to start coding
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.