Site Tools


Sidebar

Content

pub:passive_buzzer

Passive buzzer

In this lesson, learn how to play music on a passive buzzer
What is a passive buzzer?
The difference between an active buzzer and a passive one radically lies in the requirement for input signals. The ideal signals for active buzzers are direct currents, usually marked with VCC or VDD. Inside them there are a simple oscillation circuit that can convert constant direct currents into pulse signal of a certain frequency, causing magnetic fields alternation and then Mo sheet vibrating and making sounds. On the other hand, there is no driving circuit in a passive buzzer. So the ideal signal for passive buzzer is square wave. If DC is given, it will not respond since the magnetic field is unchanged, the Mo sheet cannot vibrate and produce sounds.

S Digital input
+ VCC
- GND

microbit

Components

1 * microbit
1 * microbit expansion board
1 * USB cable
1 * Passive buzzer
- Several Jumper wires

arduino

Components
1 * Adeept Arduino UNO R3 Board
1 * Passive Buzzer Module
1 * USB Cable
1 * 3-Pin Wires

Build the circuit


arduino code
<cdoe c>

/***********************************************************
File name: _11_PassiveBuzzerModule.ino
Description:  you can hear the passive buzzer playing music。
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/03/14 
***********************************************************/
#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=6;  //passive buzzer connected digital pin 6
void setup()
{
  pinMode(tonepin,OUTPUT); //Digital pin 6 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
    delay(400*durt[x]);       //Tone Delay. Note:400 can be replaced
    delay(100*durt[x]);       //Tone Delay. Note:100 can be replaced
    noTone(tonepin);          //Turn off the buzzer
  }  
  delay(2000);                //delay 2S
}

Then select the development board and COM port and download the code to arduino UNO

{{:pub:passive_buzzer:2.png?400|}}

Now you can hear the passive buzzer play music

Raspberry

Components
1 * Raspberry Pi
1 * GPIO Extension Board
1 * 40-Pin GPIO Cable
1 * Breadboard
1 * Passive Buzzer Module
1 * 3-Pin Wires

Build the circuit

code
c code:

  /*
* File name   : passiveBuzzer.c
* Description : Make an passive buzzer sound with different frequency.
* Website     : www.adeept.com
* E-mail      : support@adeept.com
* Author      : Jason
* Date        : 2015/06/22
*/
#include <softTone.h>
#include <stdio.h>
 
#define  BZRPin  0
 
int main(void)
{
	if(wiringPiSetup() < 0){
		printf("setup failed !\n");
		return -1;
	}
 
	if(softToneCreate(BZRPin) < 0){
		printf("SoftTone setup failed!\n");
		return -1;
	}
 
	while(1){
		softToneWrite(BZRPin, 200);	
		delay(200);
		softToneWrite(BZRPin, 400);	
		delay(200);
		softToneWrite(BZRPin, 600);	
		delay(200);
		softToneWrite(BZRPin, 800);	
		delay(200);
		softToneWrite(BZRPin, 900);	
		delay(200);
		softToneWrite(BZRPin, 1000);	
		delay(200);
		softToneWrite(BZRPin, 1100);	
		delay(200);
 
		softToneWrite(BZRPin, 1100);	
		delay(200);
		softToneWrite(BZRPin, 1000);	
		delay(200);
		softToneWrite(BZRPin, 900);	
		delay(200);
		softToneWrite(BZRPin, 800);	
		delay(200);
		softToneWrite(BZRPin, 600);	
		delay(200);
		softToneWrite(BZRPin, 400);	
		delay(200);
		softToneWrite(BZRPin, 200);	
		delay(200);
	}
 
	return 0;	
}
  #!/usr/bin/env python
import RPi.GPIO as GPIO
import time
 
BZRPin = 11
 
GPIO.setmode(GPIO.BOARD)       # Numbers pins by physical location
GPIO.setup(BZRPin, GPIO.OUT)   # Set pin mode as output
GPIO.output(BZRPin, GPIO.LOW)
 
p = GPIO.PWM(BZRPin, 50) # init frequency: 50HZ
p.start(50)  # Duty cycle: 50%
 
try:
	while True:
		for f in range(100, 2000, 100):
			p.ChangeFrequency(f)
			time.sleep(0.2)
		for f in range(2000, 100, -100):
			p.ChangeFrequency(f)
			time.sleep(0.2)
except KeyboardInterrupt:
	p.stop()
	GPIO.cleanup()

For C language users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/05_passiveBuzzer/passiveBuzzer.c)

Step 3: Compile

$ sudo gcc passiveBuzzer.c -o passiveBuzzer -lwiringPi -lpthread

Step 4: Run

$ sudo ./passiveBuzzer

For Python users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/05_passiveBuzzer.py)

Step 3: Run

$ sudo ./05_passiveBuzzer.py

Now you can hear the passive buzzer play music.

Reference material:http://www.adeept.com/learn/
Video:http://www.adeept.com/video/

pub/passive_buzzer.txt · Last modified: 2019/01/08 16:04 by Adeept