In this project, we introduce the sound functions and use them to play notes through the speaker.
The assembled CREATOR Kit has an attached speaker and audio cable, and also a set of functions that allow you to play notes, music and sound effects.
If your audio cable was removed, reattach it now:
Insert the Audio Cable into the audio jack on the Raspberry Pi.
Connect the other end of the Audio Cable to the Speaker Cable (the cable attached to the Speaker Assembly). Note: It doesn't matter which way the three-pronged Audio Cable is oriented.
With the speaker hardware connected, we can start making noise with just a few lines of code...
In this project, we're just going to use the basic functionality of the sound functions to play notes of different durations. Let's start with this code:
If you run this code, you'll hear a single tone play for one second. Its a simply 4 line program that works by:
On line 1, we import the Note()
function
On line 2, we create an "A" Note
, that we'll store in a new
variable named note
On line 3, we start to play()
the note (by default, notes
play for one second)
On line 4, we wait()
for the note to finish playing
If you want to change the duration of the note, you can set duration in
the play()
function, like this:
What happens if you don't wait()
for the sound to finish
playing? Why?
If you play two of the same notes in a row for 1 second each, does it sound the same as if you play the one note for 2 seconds?
Can you play a rhythm, like the "Shave and a haircut" door knock?