Buzzer interfacing with arduino sound code examples

Active Buzzer and Passive buzzer Modules:Buzzers are an integrated structure of DC power supply and electronic transducers. They are widely used in timers, alarms, electronics toys, computers, telephones and other products where generation of sound is required.Buzzers are of two types; active buzzer and passive buzzer. Before getting into the detail, we will first see the difference between the two.

An active buzzer generates the sound itself. You just have to connect the pins and turn the logic high and low similar to like you glow an LED. However, some buzzer requires more current then the Arduino digital pin gives, then you will have to connect the mosfet to amplify the current. While for the passive buzzer, you will have to send the sound signal to control the sound. You will have to use the pwm pin to send the sound signal. It has various applications. You can use it to play music.




Active Buzzer

The active buzzer will only generate sound when it will be electrified. It generates sound at only one frequency. This buzzer operates at an audible frequency of 2 KHz.

Specifications

The specifications of the active buzzer are as follows




  • It operates at a voltage range of 3.3 – 5V
  • It operates at a frequency of near 2 KHz
  • It has small size: 3.3 x 1.3 x 1.2 cm

Pin Out

The module has only three pins. The pin out from left to right is as followsbuzzer type active buzzer

S: Signal pin

5V: input pin

Ground: Ground pin

active buzzer interfacing with Arduino

The connection scheme is very easier. Just connect the wires as shown in the figureactive buzzer interfacing arduino

Code

// This code is for testing the active buzzer

int buzzerPin = 11;                                                           // initializing the buzzer pin at pin 11 of Arduino

void setup() {                                                                     // Code written in it will only run once

  pinMode(buzzerPin, OUTPUT);                               // This will set the pin 11 as output

  beep(50);                                                                          // This will make a beep sound Beep

  beep(50);                                                                         

  delay(1000);                                                                     //Adding a delay of 1 sec.

}

void loop() {                                                                       // Code written in it will run continuously

  beep(50);                                                                          // This will make a beep sound after every 500 milliseconds

  delay(1000);                                                                     // Adding a delay of one second.

}

void beep(unsigned char delayms) {                       // Created a function for beep

  analogWrite(buzzerPin, 20);                                      // This will set pin 11 to high

  delay(delayms);                                                             // Giving a delay

  analogWrite(buzzerPin ,0);                                        // This will set pin 11 to LOW

  delay(delayms);                                                             // Giving a delay

}

Passive Buzzer

The passive buzzers need a sound signal to generate a tone. This can be done by either giving a pwm signal to the buzzer or by turning it ON and OFF at different frequencies. It can generate a range of sound signals depending on the input frequency. It can generate tones of frequencies between 1.5 to 2.5 KHz.

Specification

The specifications of the passive buzzer are as follows.

Operating Voltage                                                                           1.5 – 15V DC

Tone Generating Range                                                                                1.5 – 2.5 KHz

Dimensions                                                                                        18.5mm X 15mm [0.728in X 0.591in]

Pin Out

The module has only three pins. The pin out from left to right is as followspassive buzzer

S: Signal pin

5V: input pin

Ground: Ground pin

Passive Buzzer interfacing with Arduino

The connection scheme is very easier. Just connect the wires as shown in the figurepassive buzzer interfacing with arduino

Code 

// This code is for testing the passive buzzer

int out =11;                                                                                         // initializing pin 11 as buzzer pin

void setup ()                                                                                      // Code written in it will only run once.
 {
   pinMode(out, OUTPUT);                                                            // Setting pin 11 as output pin
 }

void loop ()                                                                                         // Code written in it will run repeatedly
 {
   unsigned char i, j ;                                                                          // Declaring variables
   while (1){
     for (i = 0; i <80; i++){                                                                     // 100 cycles of sound
       digitalWrite (out, HIGH);                                                         // This will turn the buzzer ON
       delay (1) ;                                                                                      // Giving a Delay of 1ms will set frequency 1
       digitalWrite (out, LOW);                                                          // This will turn the buzzer OFF
       delay (1) ;                                                                                      // Giving a delay ms
     }
     for (i = 0; i <100; i++){                                                                 // 100 cycles of sound
       digitalWrite (out, HIGH);                                                         // This will turn the buzzer ON
       delay (2) ;                                                                                      // Giving a delay of 2ms will set frequency 2
       digitalWrite (out, LOW);                                                          // This will turn the buzzer OFF
       delay (2) ;                                                                                      // Giving a delay of 2ms
     }
   }
 }

Playing a Melody using passive buzzer

In this example, we will play a melody using the passive buzzer. We will use the capability of Arduino to produce the pwm signal through which the buzzer will generate tone at different frequencies. The connections are same as we did for the passive buzzer. Just upload the code and the buzzer will play a melody.

Melody game Code

// This code is for playing a melody

int buzzerpin = 11;
 int DEBUG = 1;   
 
 void setup() { 
   pinMode(buzzerpin, OUTPUT);                                                                               // Setting pin 11 as output
   if (DEBUG) { 
     Serial.begin(9600);                                                                                      // Setting baud rate at 9600  
   } 
 }
 
 
 int melody[] = {  C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };                         // initializing variables for playing melody
 int beats[]  = { 16, 16, 16,  8,  8,  16, 32, 16, 16, 16, 8, 8 };   // initializing the beat values 
 int MAX_COUNT = sizeof(melody) 
 
 
 long tempo = 10000;                                                                       // This will Set overall tempo
 
 int pause = 1000;                                                              // initializing the variable for pause between tones
 
 int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
 
 // Initialize core variables
 int tone_ = 0;
 int beat = 0;
 long duration  = 0;
 
 // PLAY TONE  ==============================================
 // Pulse the speaker to play a tone for a particular duration
 void playTone() {
   long elapsed_time = 0;
   if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 
     //  played less long than 'duration', pulse speaker HIGH and LOW
     while (elapsed_time < duration) {
 
       digitalWrite(buzzerpin,HIGH);
       delayMicroseconds(tone_ / 2);
 
       // DOWN
       digitalWrite(buzzerpin, LOW);
       delayMicroseconds(tone_ / 2);
 
       // Keep track of how long we pulsed
       elapsed_time += (tone_);
     } 
   }
   else { // Rest beat; loop times delay
     for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
       delayMicroseconds(duration);  
     }                                
   }                                 
 }
 
 // LET THE WILD RUMPUS BEGIN =============================
 void loop() {
   // Set up a counter to pull from melody[] and beats[]
   for (int i=0; i<MAX_COUNT; i++) {
     tone_ = melody[i];
     beat = beats[i];
 
     duration = beat * tempo; // Set up timing
 
     playTone(); 
     // A pause between notes...
     delayMicroseconds(pause);
 
     if (DEBUG) { // If debugging, report loop, tone, beat, and duration
       Serial.print(i);
       Serial.print(":");
       Serial.print(beat);
       Serial.print(" ");    
       Serial.print(tone_);
       Serial.print(" ");
       Serial.println(duration);
     }
   }
 }

So this is all about this tutorial. if you have any issue after reading this article, feel free to comment on this post. thanks

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 706 other subscribers