Table of Contents
Using a Buzzer
There are two main types of buzzer: active and passive.
A passive buzzer emits a tone when a voltage is applied across it. It also requires a specific signal to generate a variety of tones. We will be using a passive buzzer.
Connecting a buzzer
An buzzer can be connected just like an LED, but as they are a little more robust, you won't be needing a resistor to protect them.
Set up the circuit as shown below:
from gpiozero import...line:
from gpiozero import TonalBuzzer from gpiozero.tones import Tone from time import sleep
Add a line below your creation of
buttonto add a
buzzer = TonalBuzzer(17)
In GPIO Zero, a
TonalBuzzeremits the tone of your choice using play method.
The complete program will be:
from gpiozero import TonalBuzzer from gpiozero.tones import Tone from time import sleep while True: buzzer.play(Tone("A4")) # Musical note sleep(1) buzzer.play(Tone(220)) # Hz sleep(1) buzzer.stop()
Did you catch what is happening with the use of Tone() in the play method?