Piezo Buzzer

Overview

The Piezo Buzzer [Product Link] is used in applications that need an alarm or as an indication of an event.

Features

Piezo Buzzer
Piezo Buzzer
  • 2 pins for easy mounting on breadboards.
  • Operating Voltage: 1.5-12V

Description

The Piezo Buzzer is excellent for use in alarms, is also able to play tones and can be powered directly from the Chineduino Uno Rev3 [Product Link].

// Code by Jasmeet Singh for http://Wiki.JMoon.co
// This code is used to play tones with the Buzzer
// The positive pin of the buzzer should be connected to any PWM pin (shown with a ~) of the Arduino
int speakerPin = 9;

void setup()
{
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
playTone(350, 150);
delay(2000);
playTone(0, 0);
delay(3000);
}

void playTone(long duration, int freq) // duration in milli-secs, frequency in hertz
{
duration = duration*1000;
int period = (1.0 / freq)*1000000;
long timeElapsed=0;
while (timeElapsed < duration)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(period / 2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(period / 2);
timeElapsed = timeElapsed + period;
}
}

The new version of Arduino IDE support the tone() function which can be used in the following way:

//This code is written by Jasmeet Singh for http://Wiki.JMoon.co
// This code is used to control the output tone of the buzzer from a potentiometer
// Human hearing range is from 20-20000 Hz
// The tone function however supports frequency values from 31-65535.
// Connect a potentiometer on Analog pin A0.
// Use of the tone() function will interfere with PWM output on pins 3 and 11 (except on Mega)

unsigned long dur = 10;  // 10ms duration of each tone

void setup() {
}

void loop() {
unsigned int freq=analogRead(A0); // this returns a value from 0 to 1023
freq=map(freq,0,1023,31,20000);
tone(8, freq,dur);
}

Downloads

PIR Sensor Alarm System [Video Link] [Code Link]
Play Melody [Arduino Tutorial]
Play Tone [Arduino Reference]

Possible Application Areas

1. Motion Detection Security [Link]

Did you create a project using this product? Leave details/links below!

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar