In this Raspberry Pi tutorial, I will show how to connect a buzzer with Raspberry Pi 4(Model B) and sound it. This can be useful if you want to incorporate sound in projects. Here I will show how to drive a high-current buzzer using the Raspberry Pi 4. Since the Pi’s GPIO pins can only source a small amount of current, we’ll use a transistor as a switch to safely control the buzzer.
The Raspberry Pi’s GPIO pins typically provide only a few milliamps of current. That’s fine for LEDs, but not nearly enough for a buzzer that requires higher current. Directly connecting the buzzer could damage the Pi. So, we need a driver circuit.
The circuit diagram below shows how to interface buzzer with Raspberry Pi 4.
Circuit Overview
In this setup, we’re using GPIO pin 18 as the control signal. The pin connects through a 1kΩ resistor to the base of a 2N2222A NPN transistor. The buzzer is powered from a 5V supply, connected through the transistor’s collector. The emitter goes to ground. When GPIO pin 18 outputs a HIGH signal, the transistor switches on, allowing current to flow through the buzzer.
Step-by-Step Operation
- GPIO pin 18 sends a control signal from the Raspberry Pi.
This signal passes through the resistor, protecting the GPIO and limiting base current.
The transistor switches on, completing the circuit between the buzzer and ground.
The buzzer sounds, powered directly from the 5V supply—not from the Pi’s GPIO pin.
Python Script/Program Code
import RPi.GPIO as GPIO
import time
# Pin 18 corresponds to physical Pin 12 on the Pi 4
BUZZER_PIN = 18
# 1. Setup the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUZZER_PIN, GPIO.OUT)
print("Simulation Starting...")
try:
while True:
# 2. Drive the pin HIGH (Red square in Proteus)
print("Buzzer ON")
GPIO.output(BUZZER_PIN, GPIO.HIGH)
time.sleep(0.5)
# 3. Drive the pin LOW (Blue square in Proteus)
print("Buzzer OFF")
GPIO.output(BUZZER_PIN, GPIO.LOW)
time.sleep(0.5)
except KeyboardInterrupt:
print("Simulation Stopped")
finally:
GPIO.cleanup()
Video Demonstration
Key Takeaways
This method allows the Raspberry Pi to control devices that need more current than the GPIO can provide. The transistor acts like a gatekeeper, safely handling the higher load while the Pi only provides a small control signal.
And that’s how you drive a high-current buzzer with a Raspberry Pi 4 using GPIO pin 18 and a transistor. This same principle can be applied to motors, relays, or other components that require more power.
Thanks for reading, and don’t forget to subscribe for more electronics tutorials!
📥 Download Free Proteus Raspberry Pi 4 Library/Model
Download Raspberry Pi 4 Library for Proteus
