Play Buzzer on Arduino  Step by Step Guide ▷ 2020

For do projects with Arduino no electronics experience required, you just have to get down 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 to be executed.

An excellent idea to complement some types of projects in Arduino, it is incorporate sounds into them by 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 will teach you How to play sounds with Arduino and passive buzzer or speaker?, through a step-by-step guide. Added to that, you will know three cool easy project ideas that you can do yourself and put your knowledge into practice.

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

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

A Passive buzzer or speaker they 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 supplying current. It is ideal to integrate with Arduino because it allows creating an alert or notification sound when a certain event is generated. So the user must program the microcontroller to send a signal to the buzzer when said event occurs, with the purpose of warning with a sound.

A very practical example applies when you use a sensor temperature and you want to be notified when it exceeds the 100 degree centigrade. When the sensor detects these temperature levels, the buzzer will produce an alert sound. Apart from that, there are many other applications for which this item can be useful with 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

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

Play sounds with a normal buzzer or with a passive module for Arduino It is easier than it seems to be. You only have to connect it and write in Arduino IDE a simple code (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): active a certain frequency tone on a given pin
  • noTone(pin): stops the tone on 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): activate a tone frequency and duration determined on a given pin

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

  • Tone uses the Timer 2, that is, while it is working you cannot use the PWM outputs on pins 3 and 11 in Arduino Nano Y Arduino uno (pins 9 and 10 on Arduino Mega).
  • You cannot use the tone () function on two pins simultaneously. Then in these cases you have to proceed to turn off the tone with the function noTone () 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:

/* Programa simple para emitir pitidos de 1 segundo intermitentes */

const int buzzer = 9; //El pin al que se conecta el buzzer es el 9

void setup(){

pinMode(buzzer, OUTPUT); // Pin 9 declarado como salida

}

void loop(){

tone(buzzer, 50); // Envía señal de 1Khz al zumbador
delay(1000);
noTone(buzzer); // Detiene el zumbador
delay(1000); //Espera un segundo y vuelve a repetir el bucle

}

Example 2

In this second case, the buzzer or speaker is used connected to Pin9 in order to generate a 440Hz function during 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()
{
//generar tono de 440Hz durante 1000 ms
tone(pinBuzzer, 440);
delay(1000);

//detener tono durante 500ms
noTone(pinBuzzer);
delay(500);

//generar tono de 523Hz durante 500ms, y detenerlo durante 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 tonos[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494};
const int countTonos = 10;

void setup()
{
}

void loop()
{
for (int iTono = 0; iTono < countTonos; iTono++)
{
tone(pinBuzzer, tonos[iTono]);
delay(1000);
}
noTone(pinBuzzer);
}

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

The Arduino projects offer plenty of room for people’s imaginations, because they allow them incorporate different elements into the plate to make 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 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

Basic alarm or alarm clocks with Arduino

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 a Arduino board, a buzzer and some other itemsYou 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

Sensors with sound with Arduino

Another very interesting use that can be given to a buzzer with Arduino, it is 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.

Other than 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

Melody player

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

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

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will surely be of great help to more members of the community. Thank you! 😉

You may be interested:

Rate this post

Leave a Comment