Arduino and Piezo buzzer example

In this example we will connect a Piezo buzzer to our arduino and play some basic sounds, this uses the PWM functionality of the Arduino. The Arduino PWM runs at 500Hz so will produce a decent tone.

You can try various settings to get the best tone out

Schematic

piezo schematic

Layout

arduino and piezo layout

Code

void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}

void loop()
{
beep(500);
}

void beep(unsigned char delayMS)
{
//write a value between 1 and 254 out
//delay, switch off and delay again
analogWrite(9, 220);
delay(delayMS);
analogWrite(9, 0);
delay(delayMS);
}

Links

 
Piezo Electric Buzzer at Amazon US
 

Piezo Electric Buzzer at Amazon UK

Related posts

VEML6070 ultraviolet light sensor on an Arduino

Programming an Attiny44 using an Arduino

Programming an Attiny85 using an Arduino