Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Buzzer and TonalBuzzer in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Buzzer and TonalBuzzer
Start Program
↓
Initialize Buzzer Pin
↓
Create Buzzer Object
↓
Play Tone or Beep
↓
Wait or Delay
↓
Stop Buzzer
↓
End Program
The program starts by setting up the buzzer pin, then creates a buzzer object. It plays a tone or beep, waits, stops the buzzer, and ends.
Execution Sample
Raspberry Pi
from gpiozero import Buzzer, TonalBuzzer
from time import sleep

buzzer = Buzzer(17)
ton_buzzer = TonalBuzzer(18)

buzzer.on()
sleep(1)
buzzer.off()
This code turns on a simple buzzer connected to pin 17 for 1 second, then turns it off.
Execution Table
StepActionPin StateBuzzer StateOutput Sound
1Initialize Buzzer on pin 17Pin 17 set as outputOffNo sound
2Call buzzer.on()Pin 17 HIGHOnBeep sound starts
3Sleep for 1 secondPin 17 HIGHOnBeep sound continues
4Call buzzer.off()Pin 17 LOWOffSound stops
5Program endsPin 17 LOWOffNo sound
💡 Program ends after turning buzzer off and completing sleep
Variable Tracker
VariableStartAfter Step 2After Step 4Final
buzzerNot createdCreated (On)Created (Off)Created (Off)
Pin 17 stateUndefinedHIGHLOWLOW
Buzzer soundSilentBeepingSilentSilent
Key Moments - 3 Insights
Why does the buzzer make sound only after calling buzzer.on()?
Because before buzzer.on(), the pin is LOW (off). Step 2 in execution_table shows the pin goes HIGH, turning the buzzer on.
What happens if we skip buzzer.off()?
The buzzer stays on because the pin remains HIGH. Step 4 shows buzzer.off() sets pin LOW to stop sound.
How does TonalBuzzer differ from Buzzer?
TonalBuzzer can play different tones by frequency, while Buzzer only turns on/off. This is not shown in the sample but is key to their difference.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the buzzer state at Step 3?
AOff and silent
BOn and beeping
CTurning on
DTurning off
💡 Hint
Check Step 3 row in execution_table where buzzer is ON and sound continues
At which step does the buzzer pin change from HIGH to LOW?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at Pin State column in execution_table; LOW appears at Step 4
If we replace buzzer.on() with ton_buzzer.play('A4') at Step 2, what changes in the output sound?
ANo sound will play
BA beep sound plays
CA tone at frequency A4 plays
DBuzzer will turn off
💡 Hint
TonalBuzzer plays tones by frequency, unlike simple buzzer on/off
Concept Snapshot
Buzzer and TonalBuzzer control sound on Raspberry Pi GPIO pins.
Use Buzzer(pin) to create a simple beep on/off.
Use TonalBuzzer(pin) to play tones by frequency.
Call .on()/.off() for Buzzer, .play(frequency) for TonalBuzzer.
Remember to stop sound with .off() or .stop().
Full Transcript
This visual execution trace shows how to use Buzzer and TonalBuzzer on Raspberry Pi. The program starts by initializing the buzzer pin as output. Then it creates a buzzer object. When buzzer.on() is called, the pin goes HIGH and the buzzer sounds a beep. The program waits for 1 second while the buzzer is on. Then buzzer.off() sets the pin LOW and stops the sound. The program ends with the buzzer silent. Key points include that the buzzer only sounds when the pin is HIGH, and TonalBuzzer can play different tones by frequency unlike the simple Buzzer. The quiz questions check understanding of buzzer states and pin changes during execution.

Practice

(1/5)
1.

What is the main difference between a Buzzer and a TonalBuzzer in the gpiozero library?

easy
A. Buzzer makes a simple beep; TonalBuzzer can play different tones by frequency.
B. Buzzer can play music; TonalBuzzer only beeps once.
C. Buzzer requires more power than TonalBuzzer.
D. Buzzer is software only; TonalBuzzer is hardware only.

Solution

  1. Step 1: Understand Buzzer functionality

    A Buzzer in gpiozero produces a simple on/off beep sound without tone variation.
  2. Step 2: Understand TonalBuzzer functionality

    A TonalBuzzer can play different tones by changing the frequency, allowing melodies or varied sounds.
  3. Final Answer:

    Buzzer makes a simple beep; TonalBuzzer can play different tones by frequency. -> Option A
  4. Quick Check:

    Buzzer simple beep, TonalBuzzer frequency tones = C [OK]
Hint: Remember: TonalBuzzer = tones, Buzzer = simple beep [OK]
Common Mistakes:
  • Thinking Buzzer can play melodies
  • Confusing hardware and software roles
  • Assuming TonalBuzzer only beeps once
2.

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 = ?
easy
A. TonalBuzzer(17)
B. TonalBuzzer(pin=17)
C. TonalBuzzer('GPIO17')
D. TonalBuzzer('pin17')

Solution

  1. Step 1: Check gpiozero TonalBuzzer constructor

    The constructor takes the GPIO pin number as an integer, e.g., 17.
  2. Step 2: Validate syntax

    Using TonalBuzzer(17) is correct; keyword argument pin=17 is not standard, and strings like 'GPIO17' are invalid.
  3. Final Answer:

    TonalBuzzer(17) -> Option A
  4. Quick Check:

    Use integer pin number directly = A [OK]
Hint: Use integer GPIO pin number without quotes [OK]
Common Mistakes:
  • Passing pin as string instead of integer
  • Using keyword argument pin=17 incorrectly
  • Adding quotes around pin number
3.

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()
medium
A. The buzzer plays a random tone for 1 second.
B. The buzzer beeps once immediately and stops.
C. The code raises an error because play() is not a method.
D. The buzzer plays a 440 Hz tone for 1 second, then stops.

Solution

  1. Step 1: Understand TonalBuzzer play() method

    The play(frequency) method plays a tone at the given frequency in Hz, here 440 Hz (A note).
  2. Step 2: Analyze code timing

    The tone plays for 1 second due to sleep(1), then buzzer.stop() stops the sound.
  3. Final Answer:

    The buzzer plays a 440 Hz tone for 1 second, then stops. -> Option D
  4. Quick Check:

    play(440) + sleep(1) + stop() = D [OK]
Hint: play(frequency) sounds tone; sleep controls duration [OK]
Common Mistakes:
  • Thinking play() is not a valid method
  • Assuming beep instead of tone
  • Ignoring sleep duration effect
4.

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()
medium
A. sleep(2) should be sleep(2000) for milliseconds.
B. Buzzer has no play() method; use on() instead.
C. GPIO pin 22 is invalid for buzzer use.
D. buzzer.stop() should be buzzer.off().

Solution

  1. Step 1: Check Buzzer class methods

    The Buzzer class uses on() and off() to control buzzing, not play().
  2. Step 2: Validate other code parts

    sleep(2) correctly waits 2 seconds; GPIO 22 is valid; stop() is not a method for Buzzer.
  3. Final Answer:

    Buzzer has no play() method; use on() instead. -> Option B
  4. Quick Check:

    Buzzer uses on()/off(), not play() = B [OK]
Hint: Buzzer uses on()/off(), TonalBuzzer uses play() [OK]
Common Mistakes:
  • Using play() with Buzzer instead of on()
  • Confusing sleep units (seconds vs milliseconds)
  • Calling stop() on Buzzer which lacks that method
5.

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()
hard
A. Play 440 Hz and 880 Hz simultaneously, sleep, then stop.
B. Play 440 Hz, sleep, stop, then play 880 Hz, sleep, stop again.
C. Play 440 Hz, sleep, then play 880 Hz, sleep, then stop buzzer.
D. Turn buzzer on and off twice without tone control.

Solution

  1. Step 1: Understand sequential tone playing

    To play tones in sequence, call play(frequency) for each; subsequent calls change the frequency seamlessly without needing stop() between.
  2. 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.
  3. Final Answer:

    Play 440 Hz, sleep, then play 880 Hz, sleep, then stop buzzer. -> Option C
  4. Quick Check:

    play() changes frequency seamlessly = A [OK]
Hint: play(new_freq) switches tones seamlessly; no stop needed between [OK]
Common Mistakes:
  • Stopping buzzer unnecessarily between tones
  • Trying to play multiple tones at once
  • Using on()/off() instead of play() for tones
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard