Raspberry Pi Tutorial: How to Use a Buzzer

Introduction: Raspberry Pi Tutorial: How to Use a Buzzer

In this tutorial you will learn how to use a buzzer (or piezo speaker) with Raspberry Pi. Buzzers can be found in alarm devices, computers, timers and confirmation of user input such as a mouse click or keystroke.

Tutorial updates and more Raspberry Pi tutorials can be found here:

http://www.ardumotive.com/how-to-use-a-buzzer-en1....

Let's get started!

Step 1: What You Will Need - Hardware

For this tutorial you will need:

And of course any Raspberry Pi board with Raspbian OS installed.

Step 2: The Circuit

The connections are pretty easy, see the image above with breadboard circuit schematic.

Step 3: Python Code

In the program below, the first thing you do is to import the library for GPIO and sleep. The next step is to initialize pin 23 as an output pin with GPIO.setup( ) function. The While True loop runs over and over again, forever. In the main loop, you make a beep sound with GPIO.output( ) function and "pause" the program for 0.5 second with sleep( ) function.

Download the code from here and open it with Thonny Python IDE or run it from terminal.

  1. #Libraries
  2. import RPi.GPIO as GPIO
  3. from time import sleep
  4. #Disable warnings (optional)
  5. GPIO.setwarnings(False)
  6. #Select GPIO mode
  7. GPIO.setmode(GPIO.BCM)
  8. #Set buzzer - pin 23 as output
  9. buzzer=23
  10. GPIO.setup(buzzer,GPIO.OUT)
  11. #Run forever loop
  12. while True:
  13. ......GPIO.output(buzzer,GPIO.HIGH)
  14. ......print ("Beep")
  15. ......sleep(0.5) # Delay in seconds
  16. ......GPIO.output(buzzer,GPIO.LOW)
  17. ......print ("No Beep")
  18. ......sleep(0.5)

Step 4: Well Done

You have successfully completed one more Raspberry Pi "How to" tutorial and you learned how to use a buzzer!
I hope you liked this, let me know in the comments below.

Video in Greek language

Be the First to Share

    Recommendations

    • Cookie Speed Challenge

      Cookie Speed Challenge
    • Edible Art Challenge

      Edible Art Challenge
    • 3D Printed Student Design Challenge

      3D Printed Student Design Challenge

    5 Comments

    0
    kmicb5
    kmicb5

    1 year ago on Step 3

    What do we do with this .py file? Where on the sd card does it get copied to??

    0
    ShortsKing
    ShortsKing

    Question 3 months ago

    Are you aware that you are running 3.3 volts through a five volt buzzer? if yes, is it just not a problem?

    0
    joedunn
    joedunn

    1 year ago

    easiest programming I've done. Thanks heaps for making it clear

    0
    wnorman
    wnorman

    Question 3 years ago on Step 4

    this is a good Tutorial I am wondering if you could do one about speakers as this does not quite cover them