software

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

To carry out projects with Arduino it is not necessary 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 the codes are pasted, that is, the instructions that you want them to run.

An excellent idea to complement some types of Arduino projects is to incorporate sounds into them through the use of a passive buzzer or a speaker . They are very easy to work with and you can reproduce all kinds of sounds or melodies .

With this in mind, throughout this post we will teach you how to reproduce sounds with Arduino and passive buzzer or speaker? , using a step-by-step guide. Added to that, you will know three interesting ideas for easy projects that you can do yourself and put your knowledge into practice.

What is a Buzzer and how does it work on Arduino?

A passive buzzer or speaker are nothing more than devices whose function is to convert an electrical signal into a sound wave . An important fact that should be mentioned is that they do not have internal electronics, so an electrical signal must be provided to achieve the desired sound.

In simpler words, the passive buzzer is responsible for producing a sound while it is supplied with current. It is ideal to integrate with Arduino because it allows you to create an alert or notification sound when a certain event is generated. Then, the user must program the microcontroller to send a signal to the buzzer when said event occurs, in order to warn with a sound.

A very practical example applies when you use a temperature sensor and you want to be warned when it exceeds 100 degrees Celsius . When the sensor detects these temperature levels, the buzzer will produce an alert sound. Other than that, there are many other applications for which this item can be useful with the Arduino. In this way, you can give room for imagination and develop all kinds of interesting projects .

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

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

As their name implies, they are in charge of generating or stopping the tone signal on a pin:

  • tone(pin, frecuencia): activates a tone of certain frequency on a given pin
  • noTone(pin): stops the pitch at the pin

It should be noted that thanks to the tone () function there is the possibility of specifying the duration of the generated sound:

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

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

  • Tone uses Timer 2, that is, while it is working you cannot use the PWM outputs on pins 3 and 11 on Arduino Nano and Arduino Uno (pins 9 and 10 on Arduino Mega).
  • You cannot use the tone () function on two pins simultaneously . So in these cases you have to proceed to turn off the tone with the noTone () function before using it on another pin.
  • The ranges that can be used in the tone function are 31 Hz to 65535 Hz .

So that you have a much clearer explanation, we share some simple code examples where the functions specified above are applied:

Example 1

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

Then it goes back to producing 1 second again and so on:

/ * Simple program to emit intermittent 1-second beeps * /

const int buzzer = 9; // The pin to which the buzzer is connected is 9

void setup () {

pinMode (buzzer, OUTPUT); // Pin 9 declared as output

}

void loop () {

tone (buzzer, 50); // Send 1Khz signal to the buzzer
delay (1000);
noTone (buzzer); // Stop the buzzer
delay (1000); // Wait a second and repeat the loop

}

Example 2

In this second case, the buzzer or speaker connected to Pin9 is used with the aim of generating a 440Hz function for a period of one second, stopping it for 500ms and, finally, producing a 523Hz tone for 300ms .

Then repeat the program after a 500 ms pause:

const int pinBuzzer = 9;

void setup ()
{
}

void loop ()
{
// generate 440Hz tone for 1000 ms
tone (pinBuzzer, 440);
delay (1000);

// stop tone for 500ms
noTone (pinBuzzer);
delay (500);

// generate 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 make 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 iTono = 0; iTono <countTonos; iTono ++)
{
tone (pinBuzzer, tones [iTono]);
delay (1000);
}
noTone (pinBuzzer);
}

List of the best Arduino projects with buzzer that you can do yourself to practice

The projects with Arduino offer plenty of room for the imagination of the people, because they allow them to be incorporated into the various plate elements for all kinds of creations. Such is the case of the incorporation of sensors, buzzer or other elements for certain purposes. In this sense, we will show you three main ideas that you can develop yourself to practice and become better acquainted with this environment.

In all cases, you will require at least an Arduino board and a buzzer:

Basic alarm or alarm clocks

Did you know that you can create your own alarm or alarm clock? It’s not as hard as it looks. With the help of an Arduino board, a buzzer and some other elements, you can create an alarm from your home that tells you when to wake up, or to program sounds at certain times of the day.

Sensors with sound

Another very interesting use that can be given to a buzzer with Arduino, is to incorporate it in devices that have certain sensors . For example, if you create a gas sensor and you want to be notified when it exceeds the established limit, with the application of a speaker you will achieve a sound when that happens.

Apart from that, there is another practical example for which a sensor with sound can be very useful . For example, if you want to detect the approach of objects or people and emit a sound when they are already within the range you set.

Melody player

Finally, with a passive buzzer and an Arduino, you can create a device that produces certain melodies known as “Star Wars” or “Pirates of the Caribbean . To achieve this, you must program it to be able to extract different intensities of sounds, frequencies and duration .

Consequently, you will be able to create the melodies you want . In these cases, you have to declare pin9 as an output (without having to name it) and then use the structure of the tone function (pin, frequency, duration). Through this structure , the three parameters are defined separated by commas, thereby constituting all kinds of notes at any time and any melody. Keep in mind that if you have musical knowledge, you can make your own melodies .

Leave a Reply

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

Back to top button