Skip to content
Advertisements

How to use a buzzer/piezo speaker with Arduino

 

Hello everyone, in this tutorial we’ll use buzzer or piezo-speaker with its two version (3 pins module) and 2 pins version, It’s very simple to use, the buzzer convert an electrical signal to a sound by making vibration movements, it depends on the signal frequency we get a proportionnal sound. It’s a handy module for security systems and simple to wire or use.

Wirings:

Here are the wirings of different types and they works with the same codes

32

 Codes:

Here Are two version of codes:

-.Ino Codes: Download here

-Text codes:

Code 1:

//This code is to use with a buzzer if you're using the module with 3 pins wire the I/O with D8
//If you're using the other version wire the (+) pin with 8 // Refer to SurtrTech.com for more information
//Emitting 1KHz sound for 1s and then 2KHz sound for 1s

int buzzer = 8;
 
void setup() {
 
}
 
void loop() {

tone(buzzer, 1000); // tone() is the main function to use with a buzzer, it takes 2 or 3 parameteres (buzzer pin, sound frequency, duration)
 delay(1000);
 tone(buzzer, 2000); // You can also use noTone() to stop the sound it takes 1 parametere which is the buzzer pin
 delay(1000);
 
}

Code 2:

//This code is to use with a buzzer if you're using the module with 3 pins wire the I/O with D8
//If you're using the other version wire the (+) pin with 8 // Refer to SurtrTech.com for more information
//Increasing sound freqency from 100Hz to 2KHz then decreasing frequency from 2KHz to 100Hz

int buzzer = 8;
 
void setup() {
 
}
 
void loop() {
for(int i=100 ; i<2000 ; i++){ // loop for to increase the sound frequency
 tone(buzzer, i);
 delay(10); //You can change the delay if you want to make it longer or shorter
}
for(int i=2000 ; i>100 ; i--){
 tone(buzzer, i);
 delay(10);
} 
}

 

Code 3: Star Wars theme (I took it from here   https://gist.github.com/nicksort/4736535 )

 
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
 
const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
 
int counter = 0;
 
void setup()
{
 //Setup pin modes
 pinMode(buzzerPin, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
}
 
void loop()
{
 
 //Play first section
 firstSection();
 
 //Play second section
 secondSection();
 
 //Variant 1
 beep(f, 250); 
 beep(gS, 500); 
 beep(f, 350); 
 beep(a, 125);
 beep(cH, 500);
 beep(a, 375); 
 beep(cH, 125);
 beep(eH, 650);
 
 delay(500);
 
 //Repeat second section
 secondSection();
 
 //Variant 2
 beep(f, 250); 
 beep(gS, 500); 
 beep(f, 375); 
 beep(cH, 125);
 beep(a, 500); 
 beep(f, 375); 
 beep(cH, 125);
 beep(a, 650); 
 
 delay(650);
}
 
void beep(int note, int duration)
{
 //Play tone on buzzerPin
 tone(buzzerPin, note, duration);
 
 //Play different LED depending on value of 'counter'
 if(counter % 2 == 0)
 {
 digitalWrite(ledPin1, HIGH);
 delay(duration);
 digitalWrite(ledPin1, LOW);
 }else
 {
 digitalWrite(ledPin2, HIGH);
 delay(duration);
 digitalWrite(ledPin2, LOW);
 }
 
 //Stop tone on buzzerPin
 noTone(buzzerPin);
 
 delay(50);
 
 //Increment counter
 counter++;
}
 
void firstSection()
{
 beep(a, 500);
 beep(a, 500); 
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150); 
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);
 
 delay(500);
 
 beep(eH, 500);
 beep(eH, 500);
 beep(eH, 500); 
 beep(fH, 350);
 beep(cH, 150);
 beep(gS, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);
 
 delay(500);
}
 
void secondSection()
{
 beep(aH, 500);
 beep(a, 300);
 beep(a, 150);
 beep(aH, 500);
 beep(gSH, 325);
 beep(gH, 175);
 beep(fSH, 125);
 beep(fH, 125); 
 beep(fSH, 250);
 
 delay(325);
 
 beep(aS, 250);
 beep(dSH, 500);
 beep(dH, 325); 
 beep(cSH, 175); 
 beep(cH, 125); 
 beep(b, 125); 
 beep(cH, 250); 
 
 delay(350);
}
Advertisements

Yassine View All

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

One thought on “How to use a buzzer/piezo speaker with Arduino Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: