Today we will have a look at how to connect a piezo buzzer to an Arduino and how to generate different audio signals with it. A piezo buzzer is a audio signalling device, it is the most basic electronic component by which to generate sounds at different frequencies.
A piezo buzzer has 2 connection terminals, one is connected to GND and the other to a PWM digital pin, for this example we will use pin 3.
We will use the tone() function to generate tones at different frequencies, for an Arduino Uno the frequency rage is between 31 and 65535 Hz. Please note that the possible min and max frequencies differ between different models of Arduino boards. The tone() function takes 3 parameters, firstly the pin to use, then the frequency to use and lastly the duration to generate the tone in milliseconds.
Here is the code used:
#define PIEZO_PIN 3 void setup() { } void loop() { tone(PIEZO_PIN, 31, 1000); delay(500); // Pause between Tones tone(PIEZO_PIN, 15000, 1000); delay(500); // Pause between Tones tone(PIEZO_PIN, 30000, 1000); delay(500); // Pause between Tones tone(PIEZO_PIN, 45000, 1000); delay(500); // Pause between Tones tone(PIEZO_PIN, 65535, 1000); delay(500); // Pause between Tones noTone(PIEZO_PIN); // Silence Tones delay(500); }