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

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

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. For that, 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 know how to create a piece of music through Arduino.

OK, let’s do it step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Step 2

Secondly, connect these components. For that, use the circuit diagram below. We can’t use analog pins, 3 and 11 for this.

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

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 PIN is defined.

#define buzzer 8

Secondly, the buzzer pin is set 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); —- In this line, 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 as you link and try again.
  • noTone (buzzer); — This will stop the tone.

Step 4

OK, now let’s upload this program. For that, select the correct board and port. Afterward, upload this code.

Now you will hear the tone entered in the program above. Try changing these values. Well, now let’s learn how to make music using the above knowledge. For this, we can use the same circuit prepared above.

Next, we will create the required program for this. It is as follows.

  • The complete program of this project – Download
  • Piteches.h library — Download
/*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
}

In the void loop, four-tone functions are called. They are created separately below. You can play these one 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_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
  }
}

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_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
  };

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. The full video guide is below. So, we will meet in the next tutorial. Have a good day.

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

Leave a Comment

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

Shopping Cart
Select your currency
USD United States (US) dollar
EUR Euro
Scroll to Top