1.2.1 Active Buzzer

Introduction

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

Components

../_images/list_1.2.1.png

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.

../_images/image332.png

Experimental Procedures

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

../_images/image104.png

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.

Code

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

setInterval(() => {
  active.digitalWrite(!active.digitalRead());
}, 500);

process.on('SIGINT',function(){
  active.digitalWrite(1);
  process.exit();
});

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(() => {
  active.digitalWrite(!active.digitalRead());
}, 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() {
    /* DO SOME STUFF HERE */

    process.exit()
})

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

Process - NodeJS

Phenomenon Picture

../_images/image105.jpeg