Another illustration that demonstrate how to use the buzzer both passive and active. The difference between an active and passive is. The active buzzer has a built in oscillating source that will make a sound when amplifying a power compare to passive buzzer does not have such a source so it means that no beep or sound will generate when it plug to the power source on this case you need to use a square wave frequency to make a sound between 2k and 5k. As you can see diagram below there are two option to wire up the buzzer.
The active buzzer has onboard oscillating source it will beep as long as it is wired up but it can only beep with fixed frequency.
Required Component
- Raspberry Pi / Banana Pi / Orange Pi (If your using Banana Pi or Orange Pi See first the GPIO Pins)
- Buzzer Active / Passive Module
- Solder Less Breadboard
- Jumper Wire / DuPont Wire
Wiring Manually
- 10k Resistor /
- NPN Transistor (2N222)
- External Battery (5~9v)
Wiring Diagram
Uploading the Code
WiringPi Code for Active Buzzer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <wiringPi.h> #include <stdio.h> #define BuzzPin 7 int main(void) { if(wiringPiSetup() == -1){ // When wiringpi initialization failed printf "WiringPi Failed" printf("wiringPi initialization failed !"); return 1; } // printf("14CORE | RPI: GPIO %d(wiringPi pin)\n",SoundPin); pinMode(BuzzPin, OUTPUT); while(1){ digitalWrite(BuzzPin, HIGH); delay(100); digitalWrite(BuzzPin, LOW); delay(100); } return 0; } |
Python Code for Active Buzzer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#!/usr/bin/env python import RPi.GPIO as GPIO import time BuzzPin = 11 # Raspberry Pi Pin 17-GPIO 17 def setup(pin): global BuzzerPin BuzzerPin = pin GPIO.setmode(GPIO.BOARD) # Set GPIO Pin As Numbering GPIO.setup(BuzzerPin, GPIO.OUT) GPIO.output(BuzzerPin, GPIO.HIGH) def on(): GPIO.output(BuzzerPin, GPIO.LOW) def off(): GPIO.output(BuzzerPin, GPIO.HIGH) def beep(x): on() time.sleep(x) off() time.sleep(x) def loop(): while True: beep(0.5) def destroy(): GPIO.output(BuzzerPin, GPIO.HIGH) GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup(BuzzPin) try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() |
WiringPi Code for Passive Buzzer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <wiringPi.h> #include <softTone.h> #include <stdio.h> #define BuzzPin 0 #define CL1 131 #define CL2 147 #define CL3 165 #define CL4 175 #define CL5 196 #define CL6 221 #define CL7 248 #define CM1 262 #define CM2 294 #define CM3 330 #define CM4 350 #define CM5 393 #define CM6 441 #define CM7 495 #define CH1 525 #define CH2 589 #define CH3 661 #define CH4 700 #define CH5 786 #define CH6 882 #define CH7 990 int song_1[] = {CM3,CM5,CM6,CM3,CM2,CM3,CM5,CM6,CH1,CM6,CM5,CM1,CM3,CM2, CM2,CM3,CM5,CM2,CM3,CM3,CL6,CL6,CL6,CM1,CM2,CM3,CM2,CL7, CL6,CM1,CL5}; int beat_1[] = {1,1,3,1,1,3,1,1,1,1,1,1,1,1,3,1,1,3,1,1,1,1,1,1,1,2,1,1, 1,1,1,1,1,1,3}; int song_2[] = {CM1,CM1,CM1,CL5,CM3,CM3,CM3,CM1,CM1,CM3,CM5,CM5,CM4,CM3,CM2, CM2,CM3,CM4,CM4,CM3,CM2,CM3,CM1,CM1,CM3,CM2,CL5,CL7,CM2,CM1 }; int beat_2[] = {1,1,1,3,1,1,1,3,1,1,1,1,1,1,3,1,1,1,2,1,1,1,3,1,1,1,3,3,2,3}; int main(void) { int i, j; if(wiringPiSetup() == -1){ When wiringpi initialization failed printf "WiringPi Failed" printf("14CORE | WiringPi initialization failed !"); return 1; } if(softToneCreate(BuzzPin) == -1){ printf("14CORE| Soft Tone Failed !"); return 1; } while(1){ printf("14CORE| Sound is generated...\n"); for(i=0;i<sizeof(song_1)/4;i++){ softToneWrite(BuzzPin, song_1[i]); delay(beat_1[i] * 500); } for(i=0;i<sizeof(song_2)/4;i++){ softToneWrite(BuzzPin, song_2[i]); delay(beat_2[i] * 500); } } return 0; } |
Python Code for Passive Buzzer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#!/usr/bin/env python import RPi.GPIO as GPIO import time BuzzerPin = 11 CL = [0, 131, 147, 165, 175, 196, 211, 248] # Low C Note Frequency CM = [0, 262, 294, 330, 350, 393, 441, 495] # Middle C Note Frequency CH = [0, 525, 589, 661, 700, 786, 882, 990] # High C Note Frequency song_1 = [ CM[3], CM[5], CM[6], CM[3], CM[2], CM[3], CM[5], CM[6], # Sound Notes 1 CH[1], CM[6], CM[5], CM[1], CM[3], CM[2], CM[2], CM[3], CM[5], CM[2], CM[3], CM[3], CL[6], CL[6], CL[6], CM[1], CM[2], CM[3], CM[2], CL[7], CL[6], CM[1], CL[5] ] beat_1 = [ 1, 1, 3, 1, 1, 3, 1, 1, # Beats of song 1, 1 means 1/8 beats 1, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 3 ] song_2 = [ CM[1], CM[1], CM[1], CL[5], CM[3], CM[3], CM[3], CM[1], # Sound Notes 2 CM[1], CM[3], CM[5], CM[5], CM[4], CM[3], CM[2], CM[2], CM[3], CM[4], CM[4], CM[3], CM[2], CM[3], CM[1], CM[1], CM[3], CM[2], CL[5], CL[7], CM[2], CM[1] ] beat_2 = [ 1, 1, 2, 2, 1, 1, 2, 2, # Beats of song 2, 1 means 1/8 beats 1, 1, 2, 2, 1, 1, 3, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 3 ] def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(BuzzerPin, GPIO.OUT) # Set pins' mode is output global Buzz # Assign a global variable to replace GPIO.PWM Buzz = GPIO.PWM(BuzzerPin, 440) # 440 is initial frequency. Buzz.start(50) # Start BuzzerPin pin with 50% duty ration def loop(): while True: print '\n Playing song 1...' for i in range(1, len(song_1)): # Play song 1 Buzz.ChangeFrequency(song_1[i]) # Change the frequency along the song note time.sleep(beat_1[i] * 0.5) # delay a note for beat * 0.5s time.sleep(1) # Wait a second for next song. print '\n\n Playing song 2...' for i in range(1, len(song_2)): # Play song 1 Buzz.ChangeFrequency(song_2[i]) # Change the frequency along the song note time.sleep(beat_2[i] * 0.5) # delay a note for beat * 0.5s def destory(): Buzz.stop() # Stop the BuzzerPin GPIO.output(BuzzerPin, 1) # Set BuzzerPin pin to High GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destory() |
File “/home/pi/SmartHome/passive_buzzer.py”, line 34, in
GPIO.setup(BuzzerPin, GPIO.OUT) # Set pins’ mode is output
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Python: had to copy setup and setmode commands before the “setup()”. Absolutely no indentation for some reason.