A piezo buzzer is an electronic device that is commonly used to produce sound. The working principle behind it is the inverse effect of piezoelectricity. Piezo electricity, or the piezo electric effect is the ability of certain materials to generate electric charge when mechanical pressure is applied on them. This effect was discovered by the French scientist brothers Jacques and Pierre Curie. The word Piezoelectric is derived from the Greek piezein, which means to squeeze or press, and piezo, which is Greek for push.
One of the notable features of the piezo electric effect is that it is reversible, meaning if an electrical field is applied to the object made of piezo electric material, mechanical stress is generated, i.e. it either stretches or compresses. These deformations leads to the generation of audible sound.
evive has an inbuilt piezo buzzer. The piezo buzzer is connected to digital pin 46 of Arduino Mega and is controlled via PWM.
Controlling Piezo Buzzer
To generate the electric signal from evive to control the buzzer, we use the tone() function. It generates a square wave of the specific frequency with a 50% duty cycle on digital pin 46. A different duty cycle can be specified otherwise. The wave continues until the noTone() function is called.
The frequency range is from 31 Hz to 65535 Hz.
tone(pin, frequency, duration)
- pin: the pin on which to generate the tone
- frequency: the frequency of the tone in hertz – unsigned int
- duration: the duration of the tone in milliseconds (optional) – unsigned long
Below are the Arduino IDE sketch showing how to use the piezo buzzer and play specific tones on Buzzer:
First example shows how to use tone() function. In this example different frequencies are played on buzzer.
In this example song of specific length is played on evive buzzer.