Lesson 5 Buzzer

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

In this lesson, we will learn how to drive an active buzzer with a PNP transistor to make sounds.

Experimental Conditions

– 1*Raspberry Pi

– 1*Breadboard

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

– 1*Buzzer (Active)

– 1*NPN transistor (8050)

– 1*Resistor (1KΩ)

– Jumper wires

Experimental Principle

As a type of electronic buzzer with integrated structure, buzzers, which use DC power supply, are widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronic equipments, telephones, timers and other electronic products for voice devices. Buzzers can be categorized as piezoelectric and magnetic buzzers. A piezoelectric buzzer is mainly composed of multivibrator, piezoelectric buzzer slice, impedance matcher, resonance chamber, shell, etc. A magnetic buzzer is mainly composed of oscillator, electromagnetic coil, magnet, vibrating diaphragm, shell, etc. Buzzers can also be categorized as active and passive buzzers (See the following pictures). When we place the pins of two buzzers upwards, we can see the one with green circuit board is a passive buzzer, while the one without circuit board instead of enclosing with black tape is an active buzzer.

The difference between an active buzzer and a passive buzzer is:

The active buzzer has built-in oscillating source, so it will make sounds as long as it is electrified. While the passive buzzer does not have oscillating source, so it will not tweet if you use DC signals, instead you must use square waves whose frequencies are between 2K and 5K to drive it. The active buzzer is often more expensive than the passive because multiple built-in oscillating circuits exist.

In this experiment, we use an active buzzer. When we make the GPIO of Raspberry Pi output low level (0V) by programming, the transistor will conduct because of current saturation and the buzzer will make sounds. But when we supply high level to the IO of Raspberry Pi, the transistor will cut-off and the buzzer will not make sounds.

Experimental Procedures

Step 1Connect the circuit as shown in the following diagram (Pay attention to the positive and negative poles of the buzzer)

Step 2Edit and save the code with vim (see path/Rpi_UniversalStartKit/05_beep/beep.c)

Step 3Compile the code

            gcc  beep.c  -lwiringPi

Step 4: Run the program

            ./a.out

Now, you should hear the buzzer make sounds.

Further Exploration

If you have a passive buzzer in hand, you can replace the active buzzer with it. So you can make it sound “do re mi fa so la si do” through programming as long as you have some programming foundation and enough patience.

beep.c

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

#define    BEEP    0

int main(void)
{
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1; 
	}

	pinMode(BEEP,  OUTPUT);
	
	while(1){
		digitalWrite(BEEP, !digitalRead(BEEP));	
		delay(1000);
	}

	return 0;
}

Python Code

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

BeepPin = 11    # pin11

def setup():
	GPIO.setmode(GPIO.BOARD)        # Numbers GPIOs by physical location
	GPIO.setup(BeepPin, GPIO.OUT)   # Set BeepPin's mode is output
	GPIO.output(BeepPin, GPIO.HIGH) # Set BeepPin high(+3.3V) to off beep

def loop():
	while True:
		GPIO.output(BeepPin, GPIO.LOW)
		time.sleep(0.1)
		GPIO.output(BeepPin, GPIO.HIGH)
		time.sleep(0.1)

def destroy():
	GPIO.output(BeepPin, GPIO.HIGH)    # beep off
	GPIO.cleanup()                     # Release resource

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