1.2.1 Active Buzzer


In this project, we will learn how to drive an active buzzer to beep with a PNP transistor.



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: Go to the folder of the code.

cd ~/davinci-kit-for-raspberry-pi/nodejs/

Step 3: Run.

sudo node active_buzzer.js

The code run, the buzzer beeps.


const Gpio = require('pigpio').Gpio;
const active = new Gpio(17,{mode: Gpio.OUTPUT});

setInterval(() => {
}, 500);


Code Explanation

const Gpio = require('pigpio').Gpio;
const active = new Gpio(17,{mode: Gpio.OUTPUT});

Import the pigpio module, and instantiate an object active to control the IO port Gpio17, and the mode is set to output mode.

setInterval(() => {
}, 500);

The active buzzer is similar to the LED in usage and can be controlled with digitalWrite(), and digitalRead() is used to read the current pin level. Here we make the active buzzer change its working state every 500ms.

process.on('SIGINT', function() {


Handle Ctrl+C, here is used to stop the buzzer sounding when exiting the program.

Process - NodeJS

Phenomenon Picture