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.

Components

– 1*Raspberry Pi

– 1*Breadboard

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

– 1*Buzzer (Active)

– 1*PNP transistor (8550)

– 1*Resistor (1KΩ)

– Jumper wires

Experimental Principle

As a type of electronic buzzer with integrated structure, buzzers, which are supplied by DC power, are widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronic devices, telephones, timers and other electronic products for voice devices. Buzzers can be categorized as active and passive ones (see the following picture). Turn the pins of two buzzers face up, and the one with a green circuit board is a passive buzzer, while the other enclosed with a black tape is an active one.

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

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.

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 1: Connect 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_ultraKit /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.

Code

#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;
}