How to use a buzzer/piezo speaker with Arduino
Hello there, in this tutorial we’ll use a buzzer or piezo-speaker with its two versions (3 pins module and 2 pins component), 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 proportional sound. It’s a handy module for security systems or used as an alarm for many sensors.
Both buzzers I used are passive buzzers, passive means they require an external AC signal to be able to produce a sound, for our example we gonna use the Arduino “Tone” function to generate an adequate signal, for the active ones all they need is a DC signal… the passive is the most usual, that’s why I used it.


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


Codes:
There are few important functions to be able to use the buzzer which are:
1 |
tone(pin, frequency) |
“tone” function can have two versions: one with only the pin and its frequency, then the other where you add also the duration.
1 |
tone(pin, frequency, duration) |
to shut the buzzer you must use the function below, otherwise it will keep going.
1 |
noTone(pin) |
Ino Codes: Download here or:
Code 1: Simple buzzing sound with a tone change
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//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 https://surtrtech.com/ for more information //Emitting 1KHz sound for 1s and then 2KHz sound for 1s int buzzer = 8; void setup() { pinMode(buzzer,OUTPUT); } 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: Gradually sweeping between a frequency range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//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 https://surtrtech.com/ for more information //Increasing sound freqency from 100Hz to 2KHz then decreasing frequency from 2KHz to 100Hz int buzzer = 8; void setup() { pinMode(buzzer,OUTPUT); } 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 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
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); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
Loved readinng this thank you