How to Interface Buzzer Raspberry Pi

 1. Introduction

In our previous tutorials, we used LEDs for visual output. However, in many real-world applications like alarms or timers, we need an Audible alert. Today, we will learn how to connect and control a Buzzer using the Raspberry Pi. This will allow your projects to make sound notifications or "beep" alerts.

2. Components

  • Raspberry Pi (Any model)

  • Active Buzzer (5V or 3.3V)

  • 220 ohm Resistor (Optional, for volume control)

  • Breadboard

  • Jumper Wires (M-F)

3. Circuit and connections

Raspbery Pi
Raspberry Pi Pinout Diagram
Pinout Diagram of Buzzer
Pinout Diagram of Buzzer

  • Positive Terminal (+): Connect the longer leg of the buzzer to GPIO 18 (Pin 12).
  • Negative Terminal (-): Connect the shorter leg of the buzzer to a GND pin (e.g., Pin 14).

  • Note: If using an active buzzer, ensure the polarity is correct (the top usually has a '+' mark).

4. Detailed Step By Step Circuit working

  1. An Active Buzzer has an internal oscillator; it only needs a DC voltage to produce sound.

  2. When the Python script sets GPIO 18 to High (3.3V), current flows through the buzzer's internal coil.

  3. This creates a magnetic field that vibrates a metal diaphragm, creating the "beep" sound.

  4. When the pin is set to Low (0V), the vibration stops, and the sound turns off.

5. Libraries to be included

We will use the Buzzer class from GPIO Zero, which includes built-in functions for beeping.

  • from gpiozero import Buzzer

  • from time import sleep

6. Code

Python
from gpiozero import Buzzer
from time import sleep

# Define the buzzer on GPIO 18
alarm = Buzzer(18)

print("Buzzer Test Initialized...")

try:
    while True:
        # Beep every 1 second
        print("Buzzer ON")
        alarm.on()
        sleep(1)
        
        print("Buzzer OFF")
        alarm.off()
        sleep(1)
        
except KeyboardInterrupt:
    print("Program Stopped")

7. Detailed Step By Step Code working

  1. Buzzer(18): This command initializes the buzzer on the specific GPIO pin.

  2. alarm.on(): This sends a 3.3V signal to the pin to start the sound.

  3. sleep(1): This pauses the code for 1 second, allowing the buzzer to ring for that duration.

  4. alarm.off(): This stops the signal, turning the sound off.

  5. KeyboardInterrupt: This is a "clean-up" step. If you press Ctrl+C, the program stops gracefully without leaving the buzzer ringing.

8. Tips

  • Active vs. Passive: Make sure you are using an Active Buzzer for this simple code. Passive buzzers require a PWM signal (frequencies) to make sounds.

  • Sticker Removal: Most new buzzers have a "Remove after washing" sticker on top. Remove it to make the sound louder!

  • Voltage: Even though the Pi is 3.3V, most 5V active buzzers will work fine but might be slightly quieter.

9. Uses

  • Security Alarms: Combining this with the PIR sensor from the last post to sound an alarm.

  • Low Battery Alert: Beeping when a battery-powered Pi project is running low on juice.

  • Input Feedback: Making a short "beep" every time a button is pressed.

10. Conclusion

Adding sound expands the possibilities of your electronics projects. You now know how to trigger both light (LED) and sound (Buzzer) outputs. 

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.