Buzzer Physics

Piezoelectric Buzzer
Facebook
Twitter
LinkedIn

Subjects:

Length:

1 hours

Introduction

Whilst playing around with Buzzers in my after school Arduino club, I noticed an awesome teachable moment in the code that I found here. Check it out:

As the interconnected worlds of physics and computer science continue to converge, we are granted new opportunities to explore and understand the fundamental principles of the universe. This article delves into an enriching illustration of this fusion, where we examine the intriguing intersection of sound waves, a fundamental concept in physics, and code, the language of computer science. By engaging with a simple coding project designed to control a buzzer, we will not only learn about the underpinnings of the physics of sound but also understand how these principles can be represented and manipulated in the digital realm. Whether you’re a seasoned coder with an interest in physics, a physics enthusiast curious about coding, or a teacher looking for a practical way to demonstrate sound wave principles, this hands-on exploration offers a unique and enlightening journey into the world of sound. So, let’s dive in, explore, learn, and most importantly, listen!

Make

Here’s a breakdown of each line in the code and what it does. This should be helpful for novices trying to understand and write this program.

Line 1: void setup() { This function is called when a sketch starts. Here, we initialize variables, input and output pin modes. It is executed only once when the program starts or the microcontroller is reset.

Line 2: pinMode(8, OUTPUT); This line sets the digital pin 8 as an output pin. This pin will be used for buzzer output. OUTPUT mode allows the pin to emit signals.

Line 3: } This line marks the end of the setup function. All the instructions inside these brackets {} are the body of the setup() function.

Line 4: void loop() { This function does precisely what its name suggests – it loops consecutively, allowing your program to change and respond. It’s used to actively control the Arduino board.

Line 5: buzz(8, 440, 500); This line calls a function named “buzz” that we define later in the program. It sends three values to the function: “8” for the pin number, “440” for the frequency in Hz (this frequency corresponds to the musical note A4), and “500” for the duration in milliseconds.

Line 6: delay(1000); This line causes the program to pause for 1000 milliseconds (or 1 second) before moving to the next line. This creates a pause between each buzz.

Line 7: } End of the loop function.

Line 8: void buzz(int targetPin, long frequency, long length) { This line defines the buzz function, which has three parameters: “targetPin” is the output pin for the buzzer, “frequency” is the frequency of the sound the buzzer will produce, and “length” is how long the sound will last.

Line 9: long delayValue = 1000000/frequency/2; This line calculates the amount of delay between each transition of the sound wave, which determines the frequency of the sound. It’s dividing a second’s worth of microseconds by the frequency to calculate the length of one wave, and then dividing it by two since we need to account for the two phases (high and low) of each cycle.

Line 10: long numCycles = frequency * length/ 1000; This line calculates the number of cycles that the buzzer should produce based on the frequency and the duration of the sound.

Lines 11-17: for (long i=0; i < numCycles; i++){…} These lines create a loop that will run as many times as there are cycles to produce.

Inside this loop:

Line 12: digitalWrite(targetPin,HIGH); This line sends a high signal (electrically ON) to the buzzer, causing it to push out its diaphragm.

Line 13: delayMicroseconds(delayValue); This line causes the program to pause for the calculated delay time. This is half the time of one full wave of the sound.

Line 14: digitalWrite(targetPin,LOW); This line sends a low signal (electrically OFF) to the buzzer, causing it to pull its diaphragm back.

Line 15: delayMicroseconds(delayValue); This line again causes the program to pause for the calculated delay time. This is the second half of one full wave of the sound.

Line 16 and 17: } These lines mark the end of the for loop and the buzz function.

In this way, by alternating pushing and pulling the diaphragm of the buzzer at the specified frequency and for the specified number of cycles, the program creates a sound wave with the desired frequency and duration.

To write and understand this program, a novice programmer should first familiarize themselves with the basic structure of an Arduino program, which consists of the setup() and loop() functions. Then, they can study the syntax of the pinMode(), digitalWrite(), and delayMicroseconds() functions, as well as how to define and call a custom function like the buzz() function in this program.

It’s also important to understand the basics of sound physics, specifically how sound frequency determines the pitch of the sound, and how sound is produced by vibrations, which in this case are caused by pushing and pulling the diaphragm of the buzzer.

Moreover, understanding the logic behind converting frequency into delay value between the transitions and the calculation of the number of cycles will give the novice an idea of how the code is producing the required sound wave.

By mastering these concepts and practices, a novice can not only understand this program but also modify it to produce different sounds or even melodies with the buzzer.

Learn

Here’s how it ties with the physics of sound:

  1. Sound Waves: Sound is a longitudinal wave that travels through a medium, like air or water. This wave is produced by a vibrating source, in this case, the buzzer diaphragm. When the buzzer is activated, it generates pressure variations (compressions and rarefactions) in the surrounding medium. These pressure variations propagate away from the source as sound waves.
  2. Frequency and Pitch: The frequency parameter in the buzz function corresponds to the frequency of the sound wave the buzzer will produce. The frequency of a wave refers to how often the particles of the medium vibrate when a wave passes through it, measured in Hertz (Hz). In the context of sound waves, frequency is directly related to pitch – a high-frequency sound wave is perceived as a high-pitched sound and vice versa. In your code, the frequency of 440Hz corresponds to the musical note A4, the A above middle C on a standard piano.
  3. Sound Wave Generation: The buzz function is used to generate a sound wave with a square waveform, as represented by the alternating HIGH and LOW commands. The digitalWrite commands alternatively push and pull the buzzer’s diaphragm, creating pressure waves in the surrounding air. These pressure waves are what we perceive as sound. The delay between these transitions is determined by the delayValue to match the desired frequency.
  4. Wave Cycle and Duration: The number of cycles (numCycles) the sound wave will complete is calculated using the frequency and length parameters. One cycle of a wave includes a compression and a rarefaction, or in terms of the buzzer, a push and pull of the diaphragm. length is the duration of the sound in milliseconds, while frequency is how many cycles per second (Hz) the sound should have. So the total number of cycles is given by (frequency * length/ 1000). Each cycle lasts for delayValue microseconds.

Teaching Resources

If resources have been provided for this project, they are downloadable through the ‘Download’ button below.

More to Make and Learn

Anthony Copeland

Serving with Servos

Using Arduino’s with students offers a fantastic opportunity to teach some fundamental principles of computer programming. One of the earliest projects I introduce to students

Read More »