0
0
Raspberry Piprogramming~10 mins

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

Choose your learning style9 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.