Posted on Leave a comment

Buzzer on Raspberry Pi generating beeps

Raspberry Pi buzzer

In this tutorial we use a 5V active buzzer. With a Python script running on your Raspberry Pi, we make the buzzer produce repetitive beeps. 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 repetitive beep.

What you’ll need for this tutorial

First, you need to have a Raspberry Pi 3 running on the latest version of Raspbian. 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 “First Python program on the Raspberry Pi” and/or “Raspberry Pi first program using GPIO” to have a quick introduction.

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)

Raspberry Pi GPIO discovery kit

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

 

About the 5V active buzzer

active buzzer

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

Setting up the hardware part

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

1st option: using 5V with a NPN transistor

buzzer_transistor
  • 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)

 

 

buzzer with transistor

2nd option: using 3.3V

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)

 

 

buzzer

Writing 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
Python script buzzer

 

When you are done with the code, 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!

Leave a comment

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

Subscribe to our Newsletter

By subscribing you agree with our privacy policy.