A buzzer is a small device that makes sound when powered. A TonalBuzzer lets you play different tones or notes, like a simple music player.
Buzzer and TonalBuzzer in Raspberry Pi
from gpiozero import Buzzer, TonalBuzzer buzzer = Buzzer(pin_number) buzzer.on() # turns buzzer on buzzer.off() # turns buzzer off # For TonalBuzzer from gpiozero import TonalBuzzer tonal_buzzer = TonalBuzzer(pin_number) tonal_buzzer.play(frequency) # plays a tone # To stop tonal_buzzer.stop()
Replace pin_number with the GPIO pin connected to the buzzer.
TonalBuzzer can play different frequencies (tones), while Buzzer just turns on/off.
from gpiozero import Buzzer import time buzzer = Buzzer(17) buzzer.on() time.sleep(1) buzzer.off()
from gpiozero import TonalBuzzer import time buzzer = TonalBuzzer(18) buzzer.play(440) # plays A4 note time.sleep(2) buzzer.stop()
from gpiozero import TonalBuzzer import time buzzer = TonalBuzzer(18) notes = [262, 294, 330, 349, 392] # C4, D4, E4, F4, G4 for note in notes: buzzer.play(note) time.sleep(0.5) buzzer.stop()
This program plays five musical notes one after another on the buzzer connected to GPIO pin 18.
from gpiozero import TonalBuzzer import time buzzer = TonalBuzzer(18) # Play a short melody melody = [440, 494, 523, 587, 659] # A4, B4, C5, D5, E5 for tone in melody: buzzer.play(tone) time.sleep(0.4) buzzer.stop()
Make sure your buzzer supports tones if you use TonalBuzzer.
Use a resistor if needed to protect your Raspberry Pi and buzzer.
GPIO pins must be set correctly to avoid damage.
Buzzer makes simple on/off sounds.
TonalBuzzer plays different tones by frequency.
Use these to add sound feedback or simple music to your Raspberry Pi projects.
Practice
What is the main difference between a Buzzer and a TonalBuzzer in the gpiozero library?
Solution
Step 1: Understand Buzzer functionality
ABuzzerin gpiozero produces a simple on/off beep sound without tone variation.Step 2: Understand TonalBuzzer functionality
ATonalBuzzercan play different tones by changing the frequency, allowing melodies or varied sounds.Final Answer:
Buzzer makes a simple beep; TonalBuzzer can play different tones by frequency. -> Option AQuick Check:
Buzzer simple beep, TonalBuzzer frequency tones = C [OK]
- Thinking Buzzer can play melodies
- Confusing hardware and software roles
- Assuming TonalBuzzer only beeps once
Which of the following is the correct way to import and create a TonalBuzzer on GPIO pin 17 using gpiozero?
from gpiozero import TonalBuzzer buzzer = ?
Solution
Step 1: Check gpiozero TonalBuzzer constructor
The constructor takes the GPIO pin number as an integer, e.g., 17.Step 2: Validate syntax
UsingTonalBuzzer(17)is correct; keyword argumentpin=17is not standard, and strings like 'GPIO17' are invalid.Final Answer:
TonalBuzzer(17) -> Option AQuick Check:
Use integer pin number directly = A [OK]
- Passing pin as string instead of integer
- Using keyword argument pin=17 incorrectly
- Adding quotes around pin number
What will be the output behavior of this code snippet on a Raspberry Pi with a buzzer connected to GPIO 18?
from gpiozero import TonalBuzzer from time import sleep buzzer = TonalBuzzer(18) buzzer.play(440) sleep(1) buzzer.stop()
Solution
Step 1: Understand TonalBuzzer play() method
Theplay(frequency)method plays a tone at the given frequency in Hz, here 440 Hz (A note).Step 2: Analyze code timing
The tone plays for 1 second due tosleep(1), thenbuzzer.stop()stops the sound.Final Answer:
The buzzer plays a 440 Hz tone for 1 second, then stops. -> Option DQuick Check:
play(440) + sleep(1) + stop() = D [OK]
- Thinking play() is not a valid method
- Assuming beep instead of tone
- Ignoring sleep duration effect
Identify the error in this code that tries to make a buzzer beep for 2 seconds:
from gpiozero import Buzzer from time import sleep buzzer = Buzzer(22) buzzer.play() sleep(2) buzzer.stop()
Solution
Step 1: Check Buzzer class methods
TheBuzzerclass useson()andoff()to control buzzing, notplay().Step 2: Validate other code parts
sleep(2)correctly waits 2 seconds; GPIO 22 is valid;stop()is not a method forBuzzer.Final Answer:
Buzzer has no play() method; use on() instead. -> Option BQuick Check:
Buzzer uses on()/off(), not play() = B [OK]
- Using play() with Buzzer instead of on()
- Confusing sleep units (seconds vs milliseconds)
- Calling stop() on Buzzer which lacks that method
You want to play a simple melody using TonalBuzzer on GPIO 13. Which code snippet correctly plays two tones (440 Hz and 880 Hz) each for 0.5 seconds in sequence?
from gpiozero import TonalBuzzer from time import sleep buzzer = TonalBuzzer(13) # Which code plays the melody correctly? A) buzzer.play(440) sleep(0.5) buzzer.play(880) sleep(0.5) buzzer.stop() B) buzzer.play(440) sleep(0.5) buzzer.stop() buzzer.play(880) sleep(0.5) buzzer.stop() C) buzzer.play(440) buzzer.play(880) sleep(1) buzzer.stop() D) buzzer.on() sleep(0.5) buzzer.off() buzzer.on() sleep(0.5) buzzer.off()
Solution
Step 1: Understand sequential tone playing
To play tones in sequence, callplay(frequency)for each; subsequent calls change the frequency seamlessly without needingstop()between.Step 2: Analyze each option
Play 440 Hz, sleep 0.5s, play 880 Hz (switches seamlessly), sleep 0.5s, stop correctly sequences tones without gaps. Play 440 Hz, sleep 0.5s, stop, play 880 Hz, sleep 0.5s, stop introduces silence between tones. Play 440 Hz immediately followed by 880 Hz, sleep 1s, stop effectively plays mostly 880 Hz. Turn buzzer on and off twice without tone control produces simple beeps, no specific frequencies.Final Answer:
Play 440 Hz, sleep, then play 880 Hz, sleep, then stop buzzer. -> Option CQuick Check:
play() changes frequency seamlessly = A [OK]
- Stopping buzzer unnecessarily between tones
- Trying to play multiple tones at once
- Using on()/off() instead of play() for tones
Ready to prove it?
Pass the test and this pattern moves unknown
