Applications in our daily life: In daily life, many electronic devices will alarm when they encounter malfunctions, and the sound is often easier to draw people’s attention than the indicator light, the buzzer can help us to achieve this function.
There are two types of buzzers, they are active buzzers and passive buzzers. Active buzzers are very simple to use, I just need to connect it to the power supply, it will sound, and it will stop sounding when I disconnect it from the power supply. A passive buzzer, on the other hand, is required to give a specified frequency to sound, and the tone of the sound made by the buzzer can be changed by changing the frequency.

Common command for Buzzer:
Buzzer.on() #Turns the buzzer on.
Buzzer.off() #Turns the buzzer off.
Buzzer.toggle() #Reverse the state of the buzzer. If it’s on, turn it off; if it’s off, turn it on.
Buzzer.is_active() #Returns True if the buzzer is currently active and False otherwise.
Make Sound Using Buzzer
Aim: Let the buzzer make a sound regularly to achieve the effect of alarm.
Main idea:
- Import the needed Module
- Initialize the Buzzer and set it to the mode when low to turn it on
- Use a loop to turn the buzzer on and off to let it beep

Code:
#Import module that is needed in the code
from gpiozero import Buzzer
from time import sleep
bz = Buzzer(16,active_high = False)
#First Method:Let the Buzzer make sound repetitively
while True:
bz.on()
sleep(0.5)
bz.off()
sleep(0.5)
#Second Method:
#while True:
#bz.toggle()
#sleep(1)
What it looks like when it’s done:
It then occurred to me that since keys can be used to control LEDs on and off, wouldn’t a buzzer be just as well controlled by a key? So I proceeded to implement this function.
Buzz when Pressed
Here’s my thinking: By using the concept of interrupt, when the system detects an external interrupt caused by the key, execute the Buzzer state reversal code immediately.
Steps:
- Import the Button, Buzzer and Sleep Module
- Initialize the Buzzer and Button
- Define a function to let the buzzer toggle its state
- Set key interrupt
Code:
from gpiozero import Buzzer
from gpiozero import Button
from time import sleep
bz = Buzzer(16, active_high = False)
key = Button(12)
def make_sound():
bz.toggle()
key.when_pressed = make_sound
while True:
pass