How to make buzzer sound in Arduino | Step by step instructions

 How to make buzzer sound in Arduino

            Hello guys, welcome back. Today we are going to talk about how to make a buzzer sound in Arduino. Also, with this Arduino, you can create any tone very simply and cheaply. It does not require an amplifier. So, All you need is an Arduino board and a piezo buzzer or a normal buzzer. Also, we can use the frequency on the Arduino board for this. We can use this knowledge for making security systems, alarm projects, etc. Finally, by the end of this tutorial, you will have the knowledge to create a piece of music through Arduino.

Well, first we will learn step by step how to output a tone using the Arduino board. The necessary components are given below.

Step 1

Firstly, identify these components.

Arduino UNO board.

5v Buzzer.

Jumper wires.

Breadboard.

Step 2

Secondly, connect these components. For that, use the circuit diagram below. We cannot use analog write pins 3 and 11 for this.
How to make buzzer sound in Arduino

Step 3

Thirdly, we will create the required program for this. It is as follows.
#define buzzer 8
void setup() {
  pinMode(buzzer, OUTPUT);
}
void loop() {
  tone(buzzer, 10000,500);
  delay(1000);
  noTone(buzzer);
  delay(1000);
}


Code explanation

Firstly, the buzzer-connected PIN is defined.
#define buzzer 8
 
Secondly, a buzzer pin is created as the output pin in the void setup.
void setup() {
  pinMode(buzzer, OUTPUT);
}
Lastly, the tone function is created in the void loop.
void loop() {
  tone(buzzer, 10000,500);
  delay(1000);
  noTone(buzzer);
  delay(1000);
}
tone (buzzer, 10000); —- For this function include, firstly the buzzer pin and secondly, the frequency of the tone thirdly the tone duration. The value of playing these tones can range from 31 to 65000 hertz. Change this value to your liking and try again.
noTone (buzzer); — This will stop the tone.

Step 4

Well now let’s upload this program. For that, select the correct board and port. Afterward, upload this code.
How to make buzzer sound in Arduino
Now you can hear the tone as entered in the above program. Try changing these values. Well, now we will learn how to use this knowledge to create pieces of music. For this, we can use the same circuit prepared above.
Next, we will create the required program for this. It is as follows.
 /*tone generate.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creations.
*/
#include “pitches.h”//tone library
void setup() {
  //Don’t need setup function
}
void loop() {
  Toneone();
  delay(100);
  Tonetwo();
  delay(100);
  Tonethree();
  delay(100);
  Tonefour();//uncomment this tone
  delay(100);
}
void Toneone() {
  int melody[] = { //tone array
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_G3, 0
  };
  int noteDurations[] = {4, 8, 8, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 8, 4, 4};
  for (int thisNote = 0; thisNote < 23; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration); //play tone
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);//delay
    noTone(8);//tone off
  }
}
void Tonetwo() {
  int melody2[] = {NOTE_GS7, NOTE_DS8, NOTE_GS7, 0, NOTE_DS8, NOTE_DS8, 0, NOTE_GS7, NOTE_GS7};
  int noteDurations[] = {4, 8, 8, 4, 8, 8, 4, 4, 4};
  for (int thisNote = 0; thisNote < 9; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody2[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}
void Tonethree() {
  int melody3[] = {NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, NOTE_C7,
                   NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7 };
  int noteDurations[] = {4, 8, 8, 4, 8, 8, 4, 4, 4};
  for (int thisNote = 0; thisNote < 9; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody3[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

 

void Tonefour() {
  int melody4[] = {  NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
                     NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
                     NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
                     NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
                     NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
                     NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
                  };
  int noteDurations[] = {4, 8, 4, 8, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 4, 8, 8, 8, 4, 8, 8, 2, 4, 8, 4, 8, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 2};
  for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody4[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

Code explanation

Here first the “pitches.h” tone library is included. Download the library from this link and upload it to the Arduino IDE. This library contains tone melodies. Download the melody note list from this link.
#include “pitches.h”//tone library
 
There is nothing in the void setup here.
void setup() {
  //Don’t need setup function
}
Four tone functions are called in the void loop. They are created separately below. You can play these ones by one if you want. For that, upload the unwanted ones using forward-slash (//) in front of them. If you have some knowledge of music you can create beautiful tone patterns.
void loop() {
  Toneone();
  delay(100);
  Tonetwo();
  delay(100);
  Tonethree();
  delay(100);
  Tonefour();
  delay(100);
}
Well, let’s take an example of one of the tone patterns created in this program and see how it is created.
void Toneone() {
  int melody[] = { //tone array
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G30,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_G30
  };
  int noteDurations[] = {48844488884488884448844};
  for (int thisNote = 0; thisNote < 23; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration); //play tone
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);//delay
    noTone(8);//tone off
  }
}
Here the melody that should be played first is put in an array. These notes are included in the melody list provided for you to download above.
int melody[] = { //tone array
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G30,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
    NOTE_C4, NOTE_G3, NOTE_G3, NOTE_G30
  };

 

Then the time for these notes to be played is entered into an array.
int noteDurations[] = {4, 8, 8, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 8, 4, 4};
The for loop increases the number of notes here one by one.
for (int thisNote = 0; thisNote < 23; thisNote++) {
 
This takes data from the noteDurations [] array.
int noteDuration = 1000 / noteDurations[thisNote];
 
Firstly, the PIN attached to the buzzer is included. Then take notes from the melody [] array.
tone(8, melody[thisNote], noteDuration); //play tone
 
This is a delay.
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);//delay
 
This will stop the tone from playing.
noTone(8);//tone off
 
 
Okay, now let’s upload this program into the Arduino board.
Now you can hear four tones. You better monitor this tutorial and try creating different tones. Then your knowledge will expand. The full video guide is given below. So, we will meet in the next tutorial.

 

Leave a Reply

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