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:
Let's get started!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What You Will Need - Hardware
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.
- import RPi.GPIO as GPIO
- from time import sleep
- #Disable warnings (optional)
- #Select GPIO mode
- #Set buzzer - pin 23 as output
- #Run forever loop
- while True:
- ......print ("Beep")
- ......sleep(0.5) # Delay in seconds
- ......print ("No Beep")
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