How to play sounds with Arduino and a passive buzzer or speaker? Step-by-step guide

Last update: 14/09/2022
How to play sounds with Arduino and a passive buzzer or speaker? Step-by-step guide

To carry out projects with Arduino, you don't need to have experience in electronics ; you just have to get to work to familiarize yourself with that world and, especially, with the Arduino IDE , which is where you paste the code, that is, the instructions you want to be executed.

An excellent way to enhance some Arduino projects is to add sound using a passive buzzer or speaker . They are very easy to work with and you can play all kinds of sounds or melodies.

With this in mind, throughout this post we'll teach you how to play sounds with an Arduino and a passive buzzer or speaker , using a step-by-step guide. In addition, you'll discover three interesting and easy project ideas that you can do yourself and put your knowledge into practice.

What is a buzzer and how does it work in Arduino?

What is a buzzer and how does it work in Arduino?

A passive buzzer or speaker is simply a device that converts an electrical signal into a sound wave . It's important to note that they don't have internal electronics, so they must be supplied with an electrical signal to produce the desired sound.

In simpler terms, a passive buzzer produces a sound when it receives power. It's ideal for integration with Arduino because it allows you to create an alert or notification sound when a specific event occurs. The user then programs the microcontroller to send a signal to the buzzer when that event happens, so it can sound an alert.

A very practical example is when you use a temperature sensor and want to be notified when it exceeds 100 degrees Celsius . When the sensor detects this temperature level, the buzzer will sound an alert. Besides that, there are many other applications where this component can be useful with Arduino. This allows you to unleash your imagination and develop all sorts of interesting projects.

Learn step by step how to play sounds with a buzzer or speaker using Arduino

Learn step by step how to play sounds with a buzzer or speaker using Arduino

Playing sounds with a regular buzzer or a passive Arduino module is easier than it seems. You just need to connect it and write some simple code in the Arduino IDE (although keep in mind that the basic code will depend on what you want to achieve). Arduino has two main functions that help the user easily generate electrical signals to convert into sound, all through any of the available digital outputs. These functions are `tone()` and `noTone()`.

As their name indicates, they are responsible for generating or stopping the tone signal on a pin:

  • tone(pin, frecuencia): active a tone of a certain frequency on a given pin
  • noTone(pin): stop the tone on the pin

It is worth noting that, thanks to the tone() function, it is possible to specify the duration of the generated sound:

  • tone(pin, frequencia, duracion): activates a tone of frequency and duration determined on a given pin

However, you should consider that, due to its simplicity, when using the functions for generating the tone, there are certain important limitations that we indicate below:

  • Tone use the timer 2That is, while it is working PWM outputs cannot be used on pin 3 and 11 en Arduino Nano y Arduino Uno (pins 9 and 10 on Arduino Mega).
  • The tone() function cannot be used on two pins simultaneously.In these cases, you need to turn off the ringtone using the function. noTone () before using it on another pin.
  • The ranges that can be used in the tone function are of 31Hz to 65535Hz.

To make the explanation much clearer, we've included some simple code examples where the functions specified above are applied:

Example 1

With this code that we will show you, the buzzer will emit sound for 1 second and then stop.

Then, it produces 1 second again, and so on:

/* Simple program to emit intermittent 1-second beeps */ const int buzzer = 9; // The buzzer is connected to pin 9 void setup(){ pinMode(buzzer, OUTPUT); // Pin 9 declared as output } void loop(){ tone(buzzer, 50); // Sends a 1kHz signal to the buzzer delay(1000); noTone(buzzer); // Stops the buzzer delay(1000); // Waits one second and repeats the loop }

Example 2

In this second case , the buzzer or speaker connected to Pin 9 is used to generate a 440Hz function for a period of one second , stop it for 500ms and finally produce a 523Hz tone for 300ms.

Then repeat the program after a 500 ms pause:

const int pinBuzzer = 9; void setup() { } void loop() { //generate a 440Hz tone for 1000ms tone(pinBuzzer, 440); delay(1000); //stop the tone for 500ms noTone(pinBuzzer); delay(500); //generate a 523Hz tone for 500ms, and stop it for 500ms. tone(pinBuzzer, 523, 300); delay(500); }

Example 3

This last option uses an array with frequencies that are traversed sequentially to perform a sweep that approximates the different musical notes.

Let's see:

const int pinBuzzer = 9; const int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494}; const int countTones = 10; void setup() { } void loop() { for (int iTone = 0; iTone < countTones; iTone++) { tone(pinBuzzer, tones[iTone]); delay(1000); } noTone(pinBuzzer); }

List of the best Arduino projects with buzzers that you can do yourself for practice

Arduino projects offer ample room for imagination , allowing users to incorporate various components onto the board to create all sorts of things. This includes adding sensors, buzzers , and other elements for specific purposes. Here are three main ideas you can develop yourself to practice and become more familiar with this environment.

In all cases, you will need at least one Arduino board and a buzzer:

Basic alarm or alarm clocks

Basic alarm or alarm clocks with Arduino

Did you know you can create your own alarm clock? It's not as difficult as it seems. With the help of an Arduino board, a buzzer, and a few other components , you can create an alarm clock at home to tell you when to wake up, or to program sounds for specific times of the day.

Sensors with sound

Sound sensors with Arduino

Another very interesting use for a buzzer with Arduino is to incorporate it into devices that have certain sensors . For example, if you create a gas sensor and want to be notified when it exceeds a set limit, using a speaker will produce a sound when that happens.

Besides that, there's another practical example where a sound sensor can be very useful . For example, if you want to detect the approach of objects or people and emit a sound when they are within a set range.

Melody player

Melody player

Finally, with a passive buzzer and an Arduino, you can create a device that produces certain well-known melodies, such as those from "Star Wars" or "Pirates of the Caribbean . " To achieve this, you must program it to produce different sound intensities, frequencies, and durations.

Consequently, you can create any melodies you want . In these cases, you must declare pin 9 as an output (without having to name it) and then use the structure of the `tone (pin, frequency, duration)` function. This structure defines the three parameters separated by commas , thus creating all kinds of notes at any time and for any melody. Keep in mind that if you have musical knowledge, you can create your own melodies.

Ebooks of IPAP
Ebooks IPAP

🔥JOIN🔥 THE NEW IP@P COMMUNITY! SIGN UP HERE!

Themes

Author: Félix Albornoz

I have over 20 years of experience working in the technology sector, helping companies and users develop and train in this field. I'm always learning new things.

Related

Comments are closed.