Posted on Leave a comment

Buzzer on Raspberry Pi generating beeps

raspberry pi buzzer beep

In this tutorial we use a 5V active buzzer. With a Python script running on your Raspberry Pi, we make the buzzer produce a repeated beep. When you have completed this tutorial, you will be able to connect an active buzzer to your Pi via the GPIO pins. You’ll also have the basic code to produce a repeated beep.

Prepare your Pi

First, you need to have a Raspberry Pi running on the latest version of Raspberry Pi OS. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “How to write your first Python program on the Raspberry Pi” and/or “How to use the Raspberry Pi GPIO pins” to have a quick introduction.

Prepare the extra hardware

Next, you’ll need some extra hardware:

  • a breadboard (we are using a 400 points breadboard)
  • a 5V active buzzer
  • Dupont jumper wires
  • NPN transistor (optional)
  • a T-cobbler (optional)
  • a 40 pin GPIO cable (optional)

If you miss any hardware, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start. Raspberry Pi GPIO kit

Get to know the 5V active buzzer

active buzzer 5V

There are 2 kind of buzzers : passive and active. Active buzzers are easier to use and allow us to use them on their own, even when you just apply steady DC power. That’s what we want to do in this tutorial. So, we’ll use an active buzzer. With a continuous DC voltage, it will buzz at a predefined frequency of about 2300Hz. Ideally, the buzzer operates at 5V. As the output voltage of the GPIO pins of our Raspberry Pi are only 3.3V, it looks a little too low for our 5V buzzer. But the buzzer is functioning at 3.3V too. Nevertheless, at 3.3V the volume of the produced sound is less strong. So, if you have a NPN-transistor, you’ll be able to power the buzzer with 5V and it’s preferable to use it.

In this tutorial we’ll illustrate both hardware configurations: 1st option: using 5V (with transistor) and 2nd option: using 3.3V

Set up the hardware part

Be careful ! Before starting to connect wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable from the board!

1st option: using 5V with a NPN transistor

Raspberry Pi active buzzer
  • connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
  • plug the cobbler onto the breadboard as shown in the figure above or below
  • plug the other end of the 40 pin cable in the T-cobbler
  • place the buzzer on the breadboard, the long leg is the positive leg, make sure to place it near the border of the breadboard
  • place the NPN transistor: on our image, the current flows from the right pin (3) to left pin (1), the middle pin (2) is the gate.
  • connect transistor pin (3) to a 5V pin of the Pi (red wire)
  • connect transistor pin (2) to pin 23 (yellow wire)
  • make sure the long leg (+) of the buzzer is placed in the same breadboard row as transistor pin (1)
  • connect the short leg (-) of the buzzer to a GPIO GND pin (black wire)
Raspberry Pi active buzzer

2nd option: using 3.3V

Raspberry Pi active buzzer
  • connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
  • plug the cobbler onto the breadboard as shown in the figure above or below
  • plug the other end of the 40 pin cable in the T-cobbler
  • place the buzzer on the breadboard, the long leg is the positive leg, make sure to place it near the border of the breadboard
  • connect the long leg (+) of the buzzer to pin 23 (yellow wire)
  • connect the short leg (-) of the buzzer to a GPIO GND pin (black wire)
Raspberry Pi active buzzer

Write the code

The aim of this tutorial is to write a very simple Python program that allows us to produce a repetitive beep. To write the code, we use the Thonny IDE. You can find Thonny under the application menu of your Raspberry Pi.

Write or paste following code in the IDE:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
BUZZER= 23
buzzState = False
GPIO.setup(BUZZER, GPIO.OUT)
while True:
    buzzState = not buzzState
    GPIO.output(BUZZER, buzzState)
    time.sleep(1)

Some explanations about the code:

  • GPIO.setmode(GPIO.BCM): The GPIO.BCM option means that we are referring to the pins by the “Broadcom SOC channel” number, these are the numbers after “GPIO”
  • GPIO.setwarnings(False): We use this line of code to avoid warning messages because we don’t end the GPIO connection properly while interrupting the program
  • GPIO.setup(BUZZER, GPIO.OUT): We define the BUZZER pin (=23) as an output pin
  • while True: is an infinitive loop (until we stop the program)
  • Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the next lines of code
  • GPIO.output(BUZZER, buzzState): Assign the value of the variable “buzzState” to the pin. True = 3.3V and False = 0V
  • time.sleep(1): wait for 1 second

Run the Python script

Before running the program you’ll have to give it a name and save it.

Python script buzzer

Once saved, click on the Run button. You will hear the buzzer producing a beep tone every second. To stop the beeps, just click on the STOP button when there is no beep sound.

Congratulations! With this setup you can now produce a beep tone with your Raspberry Pi. You can use the script to integrate it in an application where you want to be alerted by a beep sound for example. Have fun with it!

How useful was this post?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.