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.
(adsbygoogle = window.adsbygoogle || []).push({});Well, first we will learn step by step how to output a tone using the Arduino board. The necessary components are given below.
- Arduino UNO board x 1 – Amazon / Our Store
- 5v Buzzer x 1 — Amazon / Our Store
- Jumper wires — Amazon / Our Store
- Breadboard x 1 — Amazon / Our Store
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Arduino UNO board.

5v Buzzer.

Jumper wires.

Breadboard.

Step 2

Step 3
#define buzzer 8
void setup() {
pinMode(buzzer, OUTPUT);
}
void loop() {
tone(buzzer, 10000,500);
delay(1000);
noTone(buzzer);
delay(1000);
}
Code explanation
Step 4



/*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



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