1.2.1 Active Buzzer¶
Introduction¶
In this project, we will learn how to drive an active buzzer to beep with a PNP transistor.
Required Components¶
In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Schematic Diagram¶
In this experiment, an active buzzer, a PNP transistor and a 1k resistor are used between the base of the transistor and GPIO to protect the transistor. When the GPIO17 of Raspberry Pi output is supplied with low level (0V) by programming, the transistor will conduct because of current saturation and the buzzer will make sounds. But when high level is supplied to the IO of Raspberry Pi, the transistor will be cut off and the buzzer will not make sounds.

Experimental Procedures¶
Step 1: Build the circuit. (The active buzzer has a white table sticker on the surface and a black back.)

Step 2: Open the code file.
cd ~/raphael-kit/c/1.2.1/
Step 3: Compile the code.
gcc 1.2.1_ActiveBuzzer.c -lwiringPi
Step 4: Run the executable file above.
sudo ./a.out
The code run, the buzzer beeps.
Note
If it does not work after running, or there is an error prompt: "wiringPi.h: No such file or directory", please refer to Install and Check the WiringPi.
Code
#include <wiringPi.h>
#include <stdio.h>
#define BeepPin 0
int main(void){
if(wiringPiSetup() == -1){ //when initialize wiring failed, print messageto screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(BeepPin, OUTPUT); //set GPIO0 output
while(1){
//beep on
printf("Buzzer on\n");
digitalWrite(BeepPin, LOW);
delay(100);
printf("Buzzer off\n");
//beep off
digitalWrite(BeepPin, HIGH);
delay(100);
}
return 0;
}
Code Explanation
digitalWrite(BeepPin, LOW);
We use an active buzzer in this experiment, so it will make sound automatically when connecting to the direct current. This sketch is to set the I/O port as low level (0V), thus to manage the transistor and make the buzzer beep.
digitalWrite(BeepPin, HIGH);
To set the I/O port as high level(3.3V), thus the transistor is not energized and the buzzer doesn’t beep.
Phenomenon Picture¶
