5. Chapter Buzzer

In this chapter, we will learn about buzzers and the sounds they make. And in our next project, we will use an active buzzer to make a doorbell and a passive buzzer to make an alarm.

5.1. Project Doorbell

We will make a doorbell with this functionality: when the Push Button Switch is pressed the buzzer sounds and when the button is released, the buzzer stops. This is a momentary switch function.

Raspberry Pi (with 40 GPIO) x1

GPIO Extension Board & Ribbon Cable x1

Breadboard x1

Jumper Wire

jumper-wire

Active buzzer x1

Active-buzzer

NPN transistorx1

NPN-transistor

Resistor 10kΩ x2

Resistor-10kΩ

5.1.1. Circuit

Schematic diagram with RPi GPIO Extension Shield

pr_Buzzer_Sc

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

pr_Buzzer_Fr

Note

In this circuit, the power supply for the buzzer is 5V, and pull-up resistor of the push button switch is connected to the 3.3V power feed. Actually, the buzzer can work when connected to the 3.3V power feed but this will produce a weak sound from the buzzer (not very loud).

5.1.2. Sketch

5.1.2.1. Sketch ActiveBuzzer

First, observe the result after running the sketch, and then learn about the code in detail.

  1. Use Processing to open the file Sketch_05_1_1_ActiveBuzzer.

$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_05_1_1_ActiveBuzzer/Sketch_05_1_1_ActiveBuzzer.pde
  1. Click on “RUN” to run the code.

After the program is executed, use the mouse to click on any position of the Display Window, then Active Buzzer begins to sound and arc graphics (Schematic of sounding) will appear next to the buzzer pattern on Display Window. Click the mouse again, then Active Buzzer stops sounding and arc graphics disappear.

../../../_images/Active_Buzzer.png

The following is program code:

 1/*****************************************************
 2 * Filename    : Sketch_05_1_1_Activebuzzer
 3 * Description : Use the mouse to control the Active buzzer ON or OFF
 4 * auther      : www.freenove.com
 5 * modification: 2024/09/04
 6 *****************************************************/
 7import freenove.processing.io.*;
 8
 9int buzzerPin = 17;
10boolean buzzerState = false;
11void setup() {
12  size(640, 360);
13  GPIO.pinMode(buzzerPin, GPIO.OUTPUT);
14}
15
16void draw() {
17  background(255);
18  titleAndSiteInfo();    //title and site infomation
19  drawBuzzer();       //buzzer img
20  if (buzzerState) {
21    GPIO.digitalWrite(buzzerPin, GPIO.HIGH);
22    drawArc();      //Sounds waves img
23  } else {
24    GPIO.digitalWrite(buzzerPin, GPIO.LOW);
25  }
26}
27
28void mouseClicked() { //if the mouse Clicked
29  buzzerState = !buzzerState;  //Change the buzzer State
30}
31void drawBuzzer() {
32  strokeWeight(1);
33  fill(0);
34  ellipse(width/2, height/2, 50, 50);  
35  fill(255);
36  ellipse(width/2, height/2, 10, 10);
37}
38void drawArc() {
39  noFill();
40  strokeWeight(8);
41  for (int i=0; i<3; i++) {
42    arc(width/2, height/2, 100*(1+i), 100*(1+i), -PI/4, PI/4, OPEN);
43  }
44}
45void titleAndSiteInfo() {
46  fill(0);
47  textAlign(CENTER);    //set the text centered
48  textSize(40);        //set text size
49  text("Active Buzzer", width / 2, 40);    //title
50  textSize(16);
51  text("www.freenove.com", width / 2, height - 20);    //site
52}

Code in this project is logically the same as previous “MouseLED” project. And the difference is that this project needs to draw the buzzer pattern and arc graphics after the buzzer sounding.