1. Introduction
A buzzer is a simple and effective output device used to generate sound alerts. By interfacing a buzzer with Raspberry Pi, you can create alarms, notifications, and warning systems in your projects.
There are two types:
- Active Buzzer (easy – just ON/OFF)
- Passive Buzzer (needs PWM for tones)
2. Components
- Raspberry Pi 4 Model B
- Buzzer (Active or Passive)
- Resistor (100–220Ω recommended)
- Jumper wires
- Breadboard (optional)
3. Circuit and Connections

Pinout Diagram of Buzzer
Connection Table
| Buzzer Pin | Raspberry Pi |
|---|---|
| Positive (+) | GPIO18 |
| Negative (–) | GND |
👉 Place a resistor in series with the buzzer for safety.
4. Detailed Step By Step Circuit Working
- Raspberry Pi sends HIGH signal to GPIO pin.
- Current flows through buzzer → produces sound.
- LOW signal stops sound.
- For passive buzzer, PWM signal generates tones.
5. Libraries to be Included
pip3 install RPi.GPIO
6. Code (Active Buzzer)
import RPi.GPIO as GPIO
import time
buzzer = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer, GPIO.OUT)
try:
while True:
GPIO.output(buzzer, GPIO.HIGH)
print("Buzzer ON")
time.sleep(1)
GPIO.output(buzzer, GPIO.LOW)
print("Buzzer OFF")
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
7. Detailed Step By Step Code Working
GPIO.setup()→ Sets buzzer pin as outputGPIO.output(HIGH)→ Turns buzzer ONGPIO.output(LOW)→ Turns buzzer OFF- Loop creates beeping sound
For Passive Buzzer (Tone Generation)
import RPi.GPIO as GPIO
import time
buzzer = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer, GPIO.OUT)
pwm = GPIO.PWM(buzzer, 1000) # 1kHz frequency
pwm.start(50)
time.sleep(2)
pwm.stop()
GPIO.cleanup()
8. Tips
- Use active buzzer for simple alerts
- Use passive buzzer for music/tones
- Avoid direct high current
- Always use resistor
- Test GPIO pin before connecting
9. Uses
- Alarm systems
- Notification alerts
- Security systems
- Timer alerts
- IoT warning systems
10. Conclusion
Interfacing a buzzer with Raspberry Pi is one of the simplest ways to add sound output to your project. It is widely used in automation, robotics, and IoT systems.