Lesson 11 Buzzer

Share for us

Introduction

Buzzers can be categorized as active buzzers and passive ones (as shown below).

Components

– 1 * Raspberry Pi

– 1 * Network cable (or USB wireless network adapter)

– 1 * Passive buzzer module

– 1 * Active buzzer module

– Several jumper wires

Experimental Principle

Place the pins of two buzzers face up and you can see the one with a green circuit board is a passive buzzer, while the other with a black tape, instead of a board, is an active buzzer, as shown below.

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not beep if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.

Experimental Procedures

Active Buzzer

Note: The active buzzer has built-in oscillating source, so it will make sounds as long as it is wired up. But it can only make sounds with a fixed frequency.

Step 1: Build the circuit

              Raspberry Pi                                   Active buzzer

                           GND ————————————–   –

                      GPIO0 ————————————-  S

Step 2: Edit and save the code (see path/Rpi_SensorKit_code/12_buzzer/01_activeBuzzer /activeBuzzer.c)

Step 3: Compile

              gcc  activeBuzzer.c  -lwiringPi

Step 4: Run

              ./a.out

Now you can hear the active buzzer beeps.

activeBuzzer.c

Buzzers can be categorized as active buzzers and passive ones (as shown below).

Components

– 1 * Raspberry Pi

– 1 * Network cable (or USB wireless network adapter)

– 1 * Passive buzzer module

– 1 * Active buzzer module

– Several jumper wires

Experimental Principle

Place the pins of two buzzers face up and you can see the one with a green circuit board is a passive buzzer, while the other with a black tape, instead of a board, is an active buzzer, as shown below.

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not beep if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.

Experimental Procedures

Active Buzzer

Note: The active buzzer has built-in oscillating source, so it will make sounds as long as it is wired up. But it can only make sounds with a fixed frequency.

Step 1: Build the circuit

              Raspberry Pi                                   Active buzzer

                           GND ————————————–   –

                      GPIO0 ————————————-  S

Step 2: Edit and save the code (see path/Rpi_SensorKit_code/12_buzzer/01_activeBuzzer /activeBuzzer.c)

Step 3: Compile

              gcc  activeBuzzer.c  -lwiringPi

Step 4: Run

              ./a.out

Now you can hear the active buzzer beeps.

activeBuzzer.c

#include <wiringPi.h>
#include <stdio.h>

#define BuzzerPin      0

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
//	printf("linker LedPin : GPIO %d(wiringPi pin)\n",VoicePin); //when initialize wiring successfully,print message to screen
	
	pinMode(BuzzerPin,  OUTPUT);

	while(1){
			digitalWrite(BuzzerPin, HIGH);
			delay(100);
			digitalWrite(BuzzerPin, LOW);
			delay(100);	
	}

	return 0;
}

Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

BuzzerPin = 11    # pin11

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(BuzzerPin, GPIO.OUT)
	GPIO.output(BuzzerPin, GPIO.LOW)

def loop():
	while True:
		GPIO.output(BuzzerPin, GPIO.HIGH)
		time.sleep(0.5)
		GPIO.output(BuzzerPin, GPIO.LOW)
		time.sleep(0.5)

def destroy():
	GPIO.output(BuzzerPin, GPIO.LOW)
	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.
		destroy()

Step 1: Build the circuitPassive Buzzer

                  Raspberry Pi                                   Passive Buzzer

                         3.3V ————————————–  – 

                  GPIO0 ————————————-  S

Step 2: Edit and save the code (see path/Rpi_SensorKit_code/12_buzzer/02_passiveBuzzer/passiveBuzzer.c)

Step 3: Compile

              gcc  passiveBuzzer.c  -lwiringPi

Step 4: Run

              ./a.out

Then you can hear the active buzzer beeping.

 passiveBuzzer.c

#include <wiringPi.h>
#include <softTone.h>
#include <stdio.h>

#define BuzPin    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 initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}

	if(softToneCreate(BuzPin) == -1){
		printf("setup softTone failed !");
		return 1; 
	}

	while(1){
		printf("music is being played...\n");

		for(i=0;i<sizeof(song_1)/4;i++){
			softToneWrite(BuzPin, song_1[i]);	
			delay(beat_1[i] * 500);
		}

		for(i=0;i<sizeof(song_2)/4;i++){
			softToneWrite(BuzPin, song_2[i]);	
			delay(beat_2[i] * 500);
		}	
	}

	return 0;
}

Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

BuzzerPin = 11    # pin11

SPEED = 1 

# List of tone-names with frequency
TONES = {"c6":1047,
	"b5":988,
	"a5":880,
	"g5":784,
	"f5":698,
	"e5":659,
	"eb5":622,
	"d5":587,
	"c5":523,
	"b4":494,
	"a4":440,
	"ab4":415,
	"g4":392,
	"f4":349,
	"e4":330,
	"d4":294,
	"c4":262}

# Song is a list of tones with name and 1/duration. 16 means 1/16
SONG =	[
	["e5",16],["eb5",16],
	["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
	["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
	["b4",8],["p",16],["e4",16],["ab4",16],["b4",16],
	["c5",8],["p",16],["e4",16],["e5",16],["eb5",16],
	["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
	["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
	["b4",8],["p",16],["e4",16],["c5",16],["b4",16],["a4",4]
	]

def setup():
	GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
	GPIO.setup(BuzzerPin, GPIO.OUT)

def playTone(p,tone):
        # calculate duration based on speed and tone-length
	duration = (1./(tone[1]*0.25*SPEED))

	if tone[0] == "p": # p => pause
		time.sleep(duration)
	else: # let's rock
		frequency = TONES[tone[0]]
		p.ChangeFrequency(frequency)
		p.start(0.5)
		time.sleep(duration)
		p.stop()

def run():
	p = GPIO.PWM(BuzzerPin, 440)
	p.start(0.5)
	for t in SONG:
		playTone(p,t)

def destroy():
	GPIO.output(BuzzerPin, GPIO.HIGH)
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		run()
                GPIO.cleanup()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()