Posted on Leave a comment

How to connect a buzzer to a Raspberry Pi Pico

Raspberry Pi Pico buzzer

Learn with this tutorial how to connect a buzzer to a Raspberry Pi Pico board. We’ll program the Pico with a MicroPython script to let the buzzer generate a beep tone when a push-button is pressed.

After having completed this tutorial, you will be able to connect a buzzer and a push-button to your Raspberry Pi Pico. You’ll also have a basic script to let the buzzer produce a beep when you the push-button is pressed.

  1. Prepare the hardware

    – First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.

    – Next you need an USB cable with micro-USB plug.
    – You also need a Raspberry Pi Pico of course. For this tutorial you need pin headers soldered to the GPIO-pins of your board.

    And finally you’ll need some extra components :
    – a breadboard (we are using a 400 points breadboard)
    – a 5V active buzzer
    – a push-button (we use 6x6x5mm 4 pin)

    Visit our shop if you miss any components.Raspberry Pi Pico breadboard buzzer

  2. Get to know the 5V 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.

    active buzzer 5V
    Ideally, the buzzer operates at 5V. As the output voltage of the GPIO pins of our Raspberry Pi Pico 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.

    Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.

  3. Setup the hardware part

    Raspberry Pi Pico breadboard buzzer pinout– connect the longer end (+) of the buzzer to GP11 (=GPIO 11)
    – connect the shorter end (-) of the buzzer to a GND (ground) pin
    – connect one end of the push-button to GP3 (=GPIO 3)
    – connect the other end of the push-button to a GND (ground) pin

    Pay attention to put the pushbutton in the right direction !

    Raspberry Pi Pico breadboard buzzer

  4. Write the code

    The aim here is to write a very basic script to let the buzzer beep for 1 second when the the push-button is pressed.

    Now, open Thonny and write or paste following code in the IDE:

    from machine import Pin
    import time
     
    button = Pin(3, Pin.IN, Pin.PULL_UP)
    buzzer = Pin(11, Pin.OUT)
     
    while True:
        if button.value() == 0:
            print("button pressed")
            buzzer.value(1)
            time.sleep(1)
        else:
            buzzer.value(0)


    Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tabs”.

    MicroPython Pico buzzer beeps
    Some explanations about the code :

    from machine import Pin : to partially import the machine module to have access to the GPIO pins.
    import time : to import the time module. This will allow us to use time-related tasks.
    button = Pin(3, Pin.IN, Pin.PULL_UP): here we define the push-button pin (=GP3) as an input pin. To avoid a ‘floating’ input, we use an internal pull-up resistor.
    buzzer = Pin(11, Pin.OUT): we define the buzzer pin (=GP11) as an output pin .
    while True: is an infinitive loop (until we stop the program).
    Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tab” before the next lines of code
     if button.value() == 0: : this reads the value of the push-button pin, when the pin is connected to GND it gives a ‘0’ value.
     print("button pressed"): to print a short message in the Thonny shell when the push-button is pressed.
    buzzer.value(1): let the buzzer beep
    time.sleep(1): wait for 1 second

  5. Run your script

    Now, it’s time to save your script. You can either save it on your computer or on your Pico board.

    Then, click on the Run button of the Thonny IDE. When you press the push-button, the buzzer should beep for 1 second.
    Raspberry Pi Pico buzzer

Congratulations! With this setup you can now produce a beep tone with your Raspberry Pi Pico. 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 4.2 / 5. Vote count: 17

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 4.2 / 5. Vote count: 17

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.