Sound

Sound

Sound is an important component in electronics. Sound can be used for a number of things, from playing a tune to sounding an alarm. Sounds generated can be as simple as just a beep or as complex as a full medley every time we press a button.

Sound produces simple tones by generating a square wave at specific frequencies. Those square waves become sound using a buzzer element. BrainPad Pulse is equipped with a buzzer and can generate sounds on its own. You can also generate sound using an external buzzer, like the ones found in accessories.

We use the System.Beep() function to generate sound/tone. This function works with any pin. IT requires three arguments, the pin, frequency, and duration.

This function requires 3 arguments: first the pin number you’ll be using, followed by the frequency and duration.

Python

System.Beep(pin, 500, 100)

C#

System.Beep(pin, 500, 100);

JS

await System.Beep(pin, 500, 100);

Below is an image showing one octave of notes and their frequencies. More information about notes and their frequencies can be found on the Internet.

While any pin can be used with Beep(), there is a special pin that lets you use Beep with the onboard buzzer found on BrainPad Pulse. The pin number is ASCII ‘P’. We have a definition built into the libraries to make access easier.

Python

BrainPad.System.Beep(BrainPad.Pin.Piezo, 2061, 100)

C#

BrainPad.System.Beep(BrainPad.Pin.Piezo, 2061, 100);

JS

await BrainPad.System.Beep(BrainPad.Pin.Piezo, 2061, 100);

Beep() is a blocking function, which blocks the system until it is done. If Beep() is making a sound for 100ms then the system will block for 100ms until it is done. No need to add delays when generating notes, unless it is desired to have a period of silence.

Python

BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100)
BrainPad.System.Wait(500)
BrainPad.System.Beep(BrainPad.Pin.Piezo, 440, 100)

C#

BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
BrainPad.System.Wait(500);
BrainPad.System.Beep(BrainPad.Pin.Piezo, 440, 100);

JS

await BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
await BrainPad.System.Wait(500);
await BrainPad.System.Beep(BrainPad.Pin.Piezo, 440, 100);

Now we hear two beeps, one is a lower pitch sound and the other a higher pitch. The lower the frequency number in our argument is, the lower the pitch of the sound. The higher the number, the higher the pitch.

Let’s add a while loop so the sounds will repeat forever. To do this we’ll add the while(true) function and put our Out() functions inside of it. We also need to add an additional Wait() function at the end so there will be a pause before repeating.

Python

while True:
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100)
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100)
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100)
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100)
    BrainPad.System.Wait(500)

C#

while(true){
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100);
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
    BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100);
    BrainPad.System.Wait(500);
}

JS

while(true){
    await BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
    await BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100);
    await BrainPad.System.Beep(BrainPad.Pin.Piezo, 261, 100);
    await BrainPad.System.Beep(BrainPad.Pin.Piezo, 294, 100);
    await BrainPad.System.Wait(500)
}

BrainStorm

Let’s play around with the frequency to play either a really high-pitch note or a really low-pitch note. Everyone’s hearing is different, so some people will hear certain frequencies while others cannot. What’s the highest frequency you can still hear?

Now that we understand how electronic devices play sounds, we can create a song. Trying to change the code to create a simple song.

Content Licensing
Newsletter

Twitter Feed
Hot off the press!