Lesson 3: Making sounds with a Buzzer

Introduction:

Hello and Welcome to Circuit Academy! In this tutorial, we’ll learn how to connect a piezo buzzer to an Arduino and generate different sounds using code. We’ll start by understanding how a buzzer works, then move on to wiring it to the Arduino. Next, we’ll explore basic ON/OFF control, generate tones using the tone() function, and even create a simple melody. By the end, you’ll have a solid grasp of using buzzers in your Arduino projects. Let’s get started!

What is a Buzzer?

A buzzer is an electronic component that converts electrical signals from an Arduino (or any other source) into audible sound. The type of buzzer we’re using is called a piezo buzzer because it relies on a piezoelectric element to function.

How Does a Piezo Buzzer Work?

The main component of a piezo buzzer is the piezo electric element. A piezo-electric element is a material that changes shape when an electric current is applied to it. In our buzzer, this element is attached to a thin metallic sheet and is electrically connected to a small oscillator circuit that provides an oscillating electric current. When we apply the rated voltage across the buzzer leads, the oscillator circuit generates an alternating electrical signal, causing the piezoelectric element to vibrate rapidly. These vibrations produce sound waves, which we hear as the buzzer’s output.

The rate of the bending motion of the metal sheet is called the frequency, with higher frequencies we will hear sounds with sharper pitches. Thus, the more rapid the bending of the metallic sheet, the higher the pitch of the resulting sound.


The Circuit Academy Arduino Kit

Here we will like to introduce our own custom Arduino Kit, which includes all the essential electronic components you need to follow along with this complete Arduino lesson series. This kit includes:

🔸Arduino Uno – The brain of our projects.
🔸Breadboard – A tool for quickly building circuits without soldering.
🔸LEDs – For various lighting and indicator projects.
🔸Resistors – To control current and protect components.
🔸Jumper Wires – For making electrical connections.
🔸Buzzer, Sensors, Buttons, Motors, Keypad and More!

If you want to purchase this kit click on this Link. Now, Let’s jump right back to the project! 🚀


Wiring The Buzzer To Arduino UNO:

Connecting a buzzer to an Arduino is similar to wiring an LED. Which we did in our previous lesson. Note that the buzzer has polarity markings, so we must connect it correctly:

  • Connect the positive leg of the buzzer to digital pin 8 on the Arduino .
  • Connect the negative leg to GND pin on the Arduino.

Writing the Code:

Now that we have connected the buzzer with our Arduino. Lets transitioning to the software aspect, we will use a simple Arduino sketch to control the buzzer. This sketch is almost similar to the one we used in the previous lesson.

void setup() {
    pinMode(8, OUTPUT); // Set pin 8 as an output
}
void loop() {
    digitalWrite(8, HIGH); // Turn buzzer ON
    delay(1000);           // Wait for 1 second
    digitalWrite(8, LOW);  // Turn buzzer OFF
    delay(1000);           // Wait for 1 second
}

Here In the setup() function, we set pin 8 as an output pin, since our buzzer is connected to this pin. In the loop() function, we turn the buzzer on by sending 5V to the pin using the digitalWrite(8, HIGH), then we wait for 1000 milliseconds or 1 second before turning the buzzer off with digitalWrite(8, LOW). After another 1-second delay, the cycle repeats itself. Upon successful upload of the code to the Arduino board, you’ll hear a distinct, high-pitched beeping sound characterized by intervals of approximately one second.

Generating Tones with Arduino

The piercing high-pitched sound emitted by the buzzer is typically associated with alarms. But we can change the sound of the buzzer by adjusting the frequency of vibration of the piezoelectric element. To achieve this transformation, we’ll leverage the versatile tone() function.

Understanding the tone( ) Function:

The tone() function is a built in an Arduino function that generates precise tones by producing square waves of a specified frequency (and 50% duty cycle) on a specified pin. This pin can then be connected to a piezo buzzer or other speaker to play tones.

Syntax:

Its syntax is shown below, It can take two or three parameters as shown.

tone(pin, frequency);
tone(pin, frequency, duration);
  • pin: The Arduino pin responsible for generating the tone.
  • frequency: The tone’s frequency (31 Hz to 65,535 Hz).
  • duration (optional): Duration of the tone in milliseconds. If omitted, the tone continues indefinitely until stopped.

If we dont want to specify the tone duration separately we can use another function called the “noTone()” function To stop the tone, the syntax of the no tone function is simple and takes a single parameter which is the Arduino pin on which we want the tone generation to be stopped.

noTone(pin);

Example-1: Playing Tones Of Different Frequencies

Now let’s see the tone() function in action by experimenting with different frequencies.

void setup() 
{
    pinMode(8, OUTPUT); // Set pin 8 as an output
}
void loop() 
{
    tone(8, 100); // Play a 100 Hz tone
    delay(500);  // Wait 1 second
    noTone(8);    // Stop the tone
    delay(100);
    tone(8, 1000); // Play a 1000 Hz tone
    delay(500);  // Wait 1 second
    noTone(8);    // Stop the tone
    delay(100);
    tone(8, 10000); // Play a 10,000 Hz tone
    delay(500);
    noTone(8);
    delay(100);
}

After uploading this code to our Arduino, listen to the sound it produces at different frequencies.

Example-2: Playing a Melody with the tone( ) Function

As mentioned earlier, we can also produce melodies using the tone() function. We’ve crafted the code with eight distinct frequencies, each corresponding to a musical note.

void setup() {
    pinMode(8, OUTPUT);
}
void loop() {
    tone(8, 262, 850); // Play note C (262 Hz)
    delay(850);
    tone(8, 294, 850); // Play note D (294 Hz)
    delay(850);
    tone(8, 330, 850); // Play note E (330 Hz)
    delay(850);
    tone(8, 349, 850); // Play note F (349 Hz)
    delay(850);
    tone(8, 392, 850); // Play note G (392 Hz)
    delay(850);
    tone(8, 440, 850); // Play note A (440 Hz)
    delay(850);
    tone(8, 494, 850); // Play note B (494 Hz)
    delay(850);
    tone(8, 523, 850); // Play note C (523 Hz)
    delay(850);
}

Upload this sketch to your Arduino board and try to recognize the melody it plays! You can modify the frequencies to create your own tunes. Or add as many tone statements you want.

Limitations of the tone( ) Function:

While the tone() function is powerful, it has some limitations:

  1. Single-tone limitation: The tone() function cannot generate multiple tones on different pins simultaneously. You must stop one tone before playing another.
  2. Lower frequency limit: Frequencies below 31 Hz cannot be generated using the tone() function.
  3. Conflicts with analogWrite() function: The tone() interferes with another Arduino function named analogWrite() which we will learn in an upcoming lesson. basically when we use the tone function we cannot implement analog write function on pins 3 and 11. because both functions share the same timer.

Conclusion:

That’s it for this tutorial! You have learned how to:
✅ Connect a buzzer to an Arduino.
✅ Control it using digitalWrite().
✅ Generate different frequencies with the tone() function.
✅ Create a melody with the tone() function.

In the next lesson, we’ll explore variables—what they are and how to use them in your code. If you want to learn how to blink an LED, be sure to check out the previous lesson!

For more tutorials, visit our website and YouTube channel. And Don’t forget to share it with your friends—and we will see you in the next lesson!

Leave a Comment

Your email address will not be published. Required fields are marked *