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).
Setting up the Hardware
Using jumper wires connect the I/O pin of the buzzer to GPIO16, the GND pin to GND (ground), and the VCC pin to VCC.
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 1 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 0 in the text box. Name this node “off”.
1 and 0 correspond for to values of a binary digit(bit) true and false, high and low, on and off respectively. These are called Boolean values and are a fundamental data type in Computer Science.
Connect the inject nodes to the output node and then deploy the flow.
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.