Engineers Gallery

Arduino – Peizo Buzzer

This tutorial of EngineersGallery is on Peizo Buzzer. Here two examples are included one plays beep sound and other one plays a song.

Detailed Tutorial

1. Introduction:

2. Required Hardware

3. Building Circuit

4. Programming 1 – Playing a beep sound.

5. Programming 2 – Playing a song:

 6. Output:

  1. Introduction:
    • This tutorial of EngineersGallery explains how to use a Peizo Buzzer on Arduino. When voltages are supplied to its terminal it generates beep sound. This buzzer is used to give beep sound to various embedded systems.
    • Buzzers can be found in alarm devices, computers, timers and confirmation of user input such as a mouse click or keystroke.

      You will also learn how to use tone() and noTone() function.

      So, let’s get started!

  2. Required Hardware or What you will need
    1. Arduino Uno Microcontroller Board
    2. Bread Board
    3. Jumper Wire
    4. 9v Battery
    5. 5v Buzzer
    6. 100 Ohm Resistor
  3. Building Circuit
    • Make following circuit with the help of above mentioned components. We have included two examples here both of them use the following circuit.
  4. Programming 1 – Playing a beep sound.
    1. /*
      Beep sound generation Tutorial 
      By EngineersGallery
      engineersgallery.com
      */
      
      
      const int Buzzer = 9;
      
      void setup() 
      {
        pinMode(Buzzer, OUTPUT);
      }
      
      void loop() 
      {
        digitalWrite(Buzzer, HIGH);
        delay(400);
        digitalWrite(Buzzer, LOW);
        delay(2000);
      }
  5. Programming 2 – Playing a song:
    1. /*
      Tone generation Tutorial 
      By EngineersGallery 
      engineersgallery.com
        note 	frequency
        c     262 Hz
        d     294 Hz
        e     330 Hz
        f     349 Hz
        g     392 Hz
        a     440 Hz
        b     494 Hz
        C     523 Hz
      */
        
      const int Buzzer = 9;
      const int songLength = 18;
      
      char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
      int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
      int tempo = 150;  // Speed of tempo
      
      void setup() 
      {
        pinMode(Buzzer, OUTPUT);
      }
      
      void loop() 
      {
        int i, duration;
        
        for (i = 0; i < songLength; i++) // step through the song arrays
        {
          duration = beats[i] * tempo;  // length of note/rest in ms
          
          if (notes[i] == ' ')          // is this a rest? 
          {
            delay(duration);            // then pause for a moment
          }
          else                          // otherwise, play the note
          {
            tone(Buzzer, frequency(notes[i]), duration);
            delay(duration);            // wait for tone to finish
          }
          delay(tempo/10);              // brief pause between notes
        }
        
       
        while(true){}     // Remove this line if you want to play this song for ever.
      }
      
      
      int frequency(char note) 
      { 
        int i;
        const int numNotes = 8;      // number of notes we're storing
        char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
        int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
      
        for (i = 0; i < numNotes; i++)   
        {
          if (names[i] == note)          
          {
            return(frequencies[i]);      
          }
        }
        return(0); 
      }
Exit mobile version