In this article, we connect an KY-012 active piezo buzzer to a Raspberry Pi 4
We will use Python for the example
The KY-012 active piezo buzzer is a 3-pin module that creates an audible sound at 2.5 kHz without the need for pulse width modulation. The only requirement is to set the signal pin to HIGH.
You only need to connect 2 of the pins.
- Min/Max Operating Voltage +3.3V to +5V
- Maximum Current: 30mA
- Resonance Frequency: 2500Hz ± 300Hz continous
- Minimum Sound Output 85Db @ 4in (10cm)
- Storage Temperature: -22°F to 221°F (-30°C to 105°C)
- Operating Temperature: -4°F to 158°F (-20°C to 70°C)
The sensor looks like this
Parts Required
You can connect to the module using dupont style jumper wire. There is no reason this device will not work with any Raspberry Pi hardware that you have – in this example a Raspberry Pi 4 but you can use a Zero if you want a lower cost solution
Name | Link | |
Raspberry Pi 4 | ||
37 in one sensor kit | ||
Connecting cables |
Schematic/Connection
Pico | SENSOR |
---|---|
Pin 18 | S |
GND | – |
Code Examples
Basic example in thonny, running on the Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # Here the input pin is declared, to which the buzzer is connected. Buzzer_PIN = 24 GPIO.setup(Buzzer_PIN, GPIO.OUT, initial= GPIO.LOW) print("Buzzer test [press CTRL+C to exit test]") # main program loop try: while True: print("Buzzer 4 seconds on") GPIO.output(Buzzer_PIN,GPIO.HIGH) #buzzer is switched on time.sleep(4)#wait for 4 seconds print("Buzzer 2 seconds off") GPIO.output(Buzzer_PIN,GPIO.LOW) #buzzer is switched off time.sleep(2)#wait two seconds except KeyboardInterrupt: GPIO.cleanup()
REPL Output
n/a
Links
https://github.com/getelectronics/PIBits/tree/master/Sensor%20Kit/KY-012%20active%20piezo%20buzzer