There's more...

The technique presented in this chapter is a very straightforward way of generating a beeping sound from the speakers on the board. However, under the hood, a lot is happening. When you specify the frequency and duration of a sound, it will build the sound wave programmatically, and then feed the audio data into the speaker to play the sound. The audio data is created by building a sine wave in Python code.

The code that builds this audio data is part of the Circuit Playground Express library, which was imported in this recipe. You can download the code and read it to learn how this is done. It's a great way to understand the mathematics of sound waves and how they can be created through software. The following screenshot shows what a computer-generated tone playing at 500 Hz looks like:

You can see clearly from the preceding screenshot that this looks just like a sine waveform. The screenshot was taken when we zoomed in to see an individual sound cycle. Since the sound is playing at 500 Hz, we would expect one cycle to be 1/500 of a second long. Here, we can see that this is where the first wave ends—at exactly 0.002 seconds.