Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

how to play music with passive buzzer

how to play music with passive buzzer

Experiment 10

In this tutorial, we will program the Arduino to control a passive buzzer, and then make it play some music. .

Hardware required

1 * Arduino UNO

1 * USB Cable

1 * NPN Transistor (8050)

1 * 1kΩ Resistor

1 * Passive Buzzer

1 * LED

1 * 220Ω Resistor

1 * Breadboard

Several jumper wires 

Principle of operation

As long as you send some square wave signals to a passive buzzer with different frequencies, the buzzer will make different sounds accordingly

Keyfunction :

●tone()

Generates a square wave of the specified frequency(and 50%duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones. Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency. Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin. 

Syntax:

tone(pin, frequency)

tone(pin, frequency, duration) 

Parameters :

pin: the pin on which to generate the tone

frequency: the frequency of the tone in hertz - unsigned int

duration: the duration of the tone in milliseconds (optional) - unsigned long

Returns:

nothing 
●noTone()

Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

Syntaxs :

noTone(pin)

Parameters: pin: the pin on which to stop generating the tone

Returns :

nothing

Experiment code

//File name:   Experiment 10

//Description:  LED Bar Graph Display  .

//Website: www.okuelectronics.com

//E-mail: support@okuelectronics.com

//Author: Okudzeto osei prince

//Date: 2019/06/08 

#define NTD0 -1                    //bass  1#   2#   3#   4#   5#   6#   7# 

#define NTD1 294                   // A    221  248  278  294  330  371  416 

#define NTD2 330                   // B    248  278  294  330  371  416  467

#define NTD3 350                   // C    131  147  165  175  196  221  248

#define NTD4 393                   // D    147  165  175  196  221  248  278

#define NTD5 441                   // E    165  175  196  221  248  278  312

#define NTD6 495                   // F    175  196  221  234  262  294  330

#define NTD7 556                   // G    196  221  234  262  294  330  371

                                   

#define NTDL1 147                  //Alto  1    2    3    4    5    6    7

#define NTDL2 165                  // A    441  495  556  589  661  742  833

#define NTDL3 175                  // B    495  556  624  661  742  833  935

#define NTDL4 196                  // C    262  294  330  350  393  441  495

#define NTDL5 221                  // D    294  330  350  393  441  495  556

#define NTDL6 248                  // E    330  350  393  441  495  556  624

#define NTDL7 278                  // F    350  393  441  495  556  624  661  

                                   // G    393  441  495  556  624  661  742

#define NTDH1 589

#define NTDH2 661                  //high pitch  1#   2#   3#   4#   5#   6#   7#  

#define NTDH3 700                  //    A       882  990  1112 1178 1322 1484 1665   

#define NTDH4 786                  //    B       990  1112 1178 1322 1484 1665 1869 

#define NTDH5 882                  //    C       525  589  661  700  786  882  990

#define NTDH6 990                  //    D       589  661  700  786  882  990  1112

#define NTDH7 112                  //    E       661  700  786  882  990  1112 1248

//c pinlv                          //    F       700  786  882  935  1049 1178 1322

#define WHOLE 1                    //    G       786  882  990  1049 1178 1322 1484

#define HALF 0.5

#define QUARTER 0.25

#define EIGHTH 0.25

#define SIXTEENTH 0.625


int tune[]=      //Music tones

{

  NTD3,NTD3,NTD4,NTD5,

  NTD5,NTD4,NTD3,NTD2,

  NTD1,NTD1,NTD2,NTD3,

  NTD3,NTD2,NTD2,

  NTD3,NTD3,NTD4,NTD5,

  NTD5,NTD4,NTD3,NTD2,

  NTD1,NTD1,NTD2,NTD3,

  NTD2,NTD1,NTD1,

  NTD2,NTD2,NTD3,NTD1,

  NTD2,NTD3,NTD4,NTD3,NTD1,

  NTD2,NTD3,NTD4,NTD3,NTD2,

  NTD1,NTD2,NTDL5,NTD0,

  NTD3,NTD3,NTD4,NTD5,

  NTD5,NTD4,NTD3,NTD4,NTD2,

  NTD1,NTD1,NTD2,NTD3,

  NTD2,NTD1,NTD1

};

float durt[]= //Each musical tone delay time

{

  1,1,1,1,

  1,1,1,1,

  1,1,1,1,

  1+0.5,0.5,1+1,

  1,1,1,1,

  1,1,1,1,

  1,1,1,1,

  1+0.5,0.5,1+1,

  1,1,1,1,

  1,0.5,0.5,1,1,

  1,0.5,0.5,1,1,

  1,1,1,1,

  1,1,1,1,

  1,1,1,0.5,0.5,

  1,1,1,1,

  1+0.5,0.5,1+1,

};

int length;

int tonepin=10;  //Buzzer connected digital pin 10

int ledp=2;     //LED connected digital pin 2

void setup()

{

  pinMode(tonepin,OUTPUT); //Digital pin 10 output mode

  pinMode(ledp,OUTPUT);    //Digital pin 2 output mode

  length=sizeof(tune)/sizeof(tune[0]);//Calculate the total number of musical tones

}

void loop()

  for(int x=0;x<length;x++)

  {

    tone(tonepin,tune[x]);    //Open buzzer

    digitalWrite(ledp, HIGH); //LED On

    delay(400*durt[x]);       //Tone Delay. Note:400 can be replaced

    digitalWrite(ledp, LOW);  //LED Off

    delay(100*durt[x]);       //Tone Delay. Note:100 can be replaced

    noTone(tonepin);          //Turn off the buzzer

  }  

  delay(2000);                //delay 2S

}

Compile the program and upload to Arduino UNO board Now, you can hear the passive buzzer play music, with the LED blinking.

Add Comment