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 Sound() element function to generate sound. This function requires a special feature called PWM. This is only available on specific pins, which are labeled in the device pinout.

The Sound() function requires 3 arguments: first the pin number you’ll be using, followed by the duration, and finally the volume level.

Sound(pin,playtime,volume)

To play sound using the onboard buzzer, you simply replace the pin number with the keyword Buzzer.

Let’s start by creating our sound object, let’s call it speaker. We’ll demonstrate using the onboard buzzer.

var speaker = Sound(Buzzer,0.1,100);
speaker = Sound(Buzzer,0.1,100)

The first line of code just creates the sound object we named speaker. We won’t hear a sound until we use the Out() method to play it. The Out() requires 2 arguments: our sound object we named speaker and the frequency of the note we want to play.

Out(element, note)

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. Note that the frequencies used in code are rounded to the nearest whole number.

var speaker = Sound(Buzzer,0.1,100);
Out(speaker, 261);
speaker = Sound(Buzzer,0.1,100)
Out(speaker, 261)

When we run the code example, we hear just one quick beep. Let’s add another note to the code. We need to add a pause in between, so we’ll use the Wait() function to do this. 

var speaker = Sound(Buzzer,0.1,100);
Out(speaker, 261);
Wait(0.5);
Out(speaker, 294);
speaker = Sound(Buzzer,0.1,100)
Out(speaker, 261)
Wait(0.5)
Out(speaker, 294)

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 added 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.

var speaker = Sound(Buzzer,0.1,100);

while(true){
    Out(speaker, 261);
    Wait(0.5);
    Out(speaker, 294);
    Wait(0.5);
}
speaker = Sound(Buzzer,0.1,100)
 
while True:
    Out(speaker, 261)
    Wait(0.5)
    Out(speaker, 294)
    Wait(0.5)

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 changing the code to create a simple song.