0
0
Raspberry Piprogramming~30 mins

Buzzer and TonalBuzzer in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Controlling a Buzzer and TonalBuzzer on Raspberry Pi
📖 Scenario: You have a Raspberry Pi connected to a buzzer and a tonal buzzer. You want to control them using Python to make sounds and tones.
🎯 Goal: Build a Python program that sets up a buzzer and a tonal buzzer, plays a simple beep sound, then plays a tone at a specific frequency.
📋 What You'll Learn
Create a Buzzer object on GPIO pin 17
Create a TonalBuzzer object on GPIO pin 18
Play a beep sound on the buzzer
Play a 440 Hz tone on the tonal buzzer
Print messages indicating each action
💡 Why This Matters
🌍 Real World
Buzzers are used in alarms, timers, and notifications in many devices. Controlling them with a Raspberry Pi helps you build interactive electronics projects.
💼 Career
Understanding how to control hardware components like buzzers is useful for jobs in embedded systems, IoT development, and hardware prototyping.
Progress0 / 4 steps
1
Setup Buzzer and TonalBuzzer objects
Import Buzzer and TonalBuzzer from gpiozero. Create a buzzer object on GPIO pin 17 and a tonal_buzzer object on GPIO pin 18.
Raspberry Pi
Need a hint?

Use from gpiozero import Buzzer, TonalBuzzer to import the classes. Then create buzzer = Buzzer(17) and tonal_buzzer = TonalBuzzer(18).

2
Create a beep duration variable
Create a variable called beep_duration and set it to 1 (one second).
Raspberry Pi
Need a hint?

Just write beep_duration = 1 to set the beep duration to one second.

3
Play beep and tone
Use buzzer.on() and buzzer.off() with sleep(beep_duration) to play a beep. Then use tonal_buzzer.play(440) to play a 440 Hz tone for beep_duration seconds, then stop it with tonal_buzzer.stop(). Import sleep from time.
Raspberry Pi
Need a hint?

Turn buzzer on, wait, then off. Then play tone 440 Hz, wait, then stop.

4
Print status messages
Print "Beep sound played" after the buzzer beep, and print "440 Hz tone played" after the tonal buzzer tone.
Raspberry Pi
Need a hint?

Use print("Beep sound played") and print("440 Hz tone played") after each sound.