What is a Piezoelectric Buzzer?
A piezoelectric buzzer is a buzzer that is activated with an oscillating electric signal. This means to produce a tone the buzzer must be turned on and off many times per second. The faster the the buzzer is turned on and off the higher the pitch is produced. We can achieve this using a pulse width modulated signal (PWM). With a PWM signal we can specify the frequency in hertz (cycles per second).
NOTE: YOU MUST PROGRAM AND DEPLOY THE BUZZER IN NODE RED BEFORE YOU CONNECT THE BUZZER TO THE PI OTHERWISE THE BUZZER WILL OVER HEAT
Setting up Node-Red
Start Node-Red and navigate to 127.0.0.1:1880. Drag two inject nodes and a Raspberry Pi output node into the flow area.
Double click on the Raspberry Pi output node to open its configuration menu. Set the Pin to GPIO16. Set Type to PWM output. Set Frequency to 100 Hz. You can Name the node anything, I chose “buzzer”.
Double click one of the inject nodes. Use the dropdown menu to change the Payload data type number, type a 99 in the textbox. Name the inject node “on”.
Double click the unmodified inject node. Use the drop down menu to change the Payload data type to number, type a 100 in the text box. Name this node “off”.
The PWM mode of the GPIO node accepts a value from 0-100, our buzzer module is active low(0) meaning we want to give it a 100 to turn it off. The 99 for on sets the duty cycle to be are 99% off 1% on which makes a nice buzzer tone. We set the “inject once after” checkbox to make sure the buzzer starts in the off state.
Connect the inject nodes to the output node and then deploy the flow.
Now go ahead and connect the buzzer to the pi
Setting up the Hardware
- VCC -> 3.3V
- I/O -> PIN36 #16
- GND -> GND
If everything has worked correctly pressing the “on” button should cause a sound to come from the buzzer and the “off” button should stop that sound.