• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

Buzzer

Introduction

Buzzer or beeper is an acoustic signaling device. It can be mechanical, electromechanical or piezoelectric. An example of the mechanical one is a "joy buzzer" which is used in the old alarm clocks, electromechanical is the one connected to the door bell, and you will find out more about piezo buzzers in this tutorial.

DSC_8597

Piezo effect

Piezo effect is the ability of the material to generate an electrical charge on the given mechanical load. The name itself derives from a greek word piezo which means to press, and it was discovered by brothers Pierre and Jacques Currie. When a piezoelectric material is exposed to the mechanical load (pressure), positive and negative charges dislocate, resulting in an external electric field. A great example of piezoelectric material is quartz, which drives quartz watches. Besides quartz watches, the newer appliance of this technology is speech recognition. So the microphone you speak into when you want the computer to write down your speech actually uses piezo effect in order to turn the voice energy into an electrical signal which the computer can process.

ezgif.com-gif-maker
As for the buzzers, there is a small coil and a magnet. When the current flows through the coil, it magnetizes and moves to the magnet. From the previously explained piezo effect, it is clear to us that it will cause a small "click". As this is repeated thousands of times, the "click" becomes a sound.

How to connect the buzzer?

Paying attention to polarity, we will connect the negative pole of the buzzer to the Croduino gnd, and the positive one to the Croduino's 9th digital pin. You can connect one pole through some type of resistor, for example that of 100Ω. Note: the buzzer must be connected to the PWM pin, if you do not want humdrum sounds. Do not forget to take the sticker off the buzzer! 

buzz

 

The code and Tone library

Tone library is located in the Arduino IDE and does not require special installation. It uses the tone()  function on one of the PWM pins.
The syntax is next:
tone(pin, frequency);
tone(pin, frequency, duration);

where the:
pin - represents a number of PWM pins
frekvencija - a frequency of the tone measured in Hz (check the frequencies)
trajanje - the duration of the tone in milliseconds (not necessary)

An example can be downloaded here. The code reproduces a song by Hladno Pivo - Nije sve tako sivo. Feel free to send yours to us, if you make some!

/*
  The code plays a song by Hladno Pivo - Nije sve tako sivo and these are the tones used 
  for it:
  tone  frequency
  a     220 Hz
  (a)#  233 Hz 
  c     262 Hz
  d     294 Hz
  f     349 Hz
  A     440 Hz

 More information on http://peabody.sapp.org/class/st2/lab/notehz/
 Written by e-radionica.com

*/

const int buzzerPin = 9;
const int duljinaPjesme = 32;  // it needs to be as many as notes and pauses

char nota[] = "ddfd fda##d# d#affAf Afdaaa# #cc"; // the space stands for pause

// rythm is array of values of every note and pause 
// "1" represents quarter note, 2 half note etc.

int ritam[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};

// tempo is the speed of performing, reduce the tempo in order to speed up the song
int tempo = 80;


void setup() 
{
  pinMode(buzzerPin, OUTPUT);
}


void loop() 
{
  int i, trajanje;
  for (i = 0; i < duljinaPjesme; i++) // steps through the song array 
  {
    trajanje = ritam[i] * tempo; 
    if (nota[i] == ' ')           
    {
      delay(trajanje);            
    }
    else                          
    {
      tone(buzzerPin, frekvencija(nota[i]), trajanje); // for more check http://e-radionica.hr/blog/?p=513
      delay(trajanje);            
    }
    delay(tempo/10);              
  }
  
  while(1){} // if you want it to constantly play this melody, put comments (//) in front of the while function 
}


int frekvencija(char nota) 
{

  int i;
  const int brojnota = 6;  // how many notes we save

  // by using following arrays we give names and frequencies to the notes we want to use
  char name[] = {            'c', 'd', 'f', 'a', '#', 'A' };
  int vratiFrekvenciju[] = {262, 294, 349, 220, 233, 440};

  // we go through the array and search for tone,i.e. its frequency

  for (i = 0; i < brojnota; i++)  
  {
    if (ime[i] == nota)         // if that is the tone we are looking for
    {
      return(vratiFrekvenciju[i]);     // return to its frequency 
    }
  }
  return(0);
              
}
Leave a Reply