CSCI373 -Week7 - Piezo

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CSCI373

Robotic Design and Coding

The Piezoelectric Buzzers


The Piezoelectric Buzzers
A piezoelectric element (piezo for short), or buzzer, is a small, round
device that can be used to generate loud and annoying noises that are
perfect for alarms or for having fun.
The Piezoelectric Buzzers
#define PIEZO 9
#define del 100
void setup(){
pinMode(9,OUTPUT)
}
void loop(){
analogWrite(PIEZO, 128);
// 50 percent duty cycle tone to the piezo
delay(del);
digitalWrite(PIEZO, LOW);
// turn the piezo off
delay(del);
}
FUNCTION TONE()
tone( pin number, frequency in hertz);
• The pin number that you will use on the Arduino.
• The frequency specified in hertz. Hertz are cycles per second.
*/This code only generates a delay of 500 milliseconds between the
tone
tone( 9, 205, 500);
delay(1000);
PIEZO WITH 4 LED’S
PIEZO WITH 4 LED’S
#define PIEZO 9 void loop(){

#define led1 1 int Duration=250;

#define led2 3 tone(PIEZO, 242,Duration);

#define led3 10 digitalWrite(led1,HIGH);

#define led4 12 digitalWrite(led2,LOW);

void setup(){ digitalWrite(led3,LOW);

pinMode(PIEZO, OUTPUT); digitalWrite(led4,LOW);

pinMode(led1,OUTPUT); delay(500);

pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
}
PIEZO WITH 4 LED’S
Duration=125; digitalWrite(led3,HIGH);

tone(PIEZO, 192,Duration); digitalWrite(led4,LOW);

digitalWrite(led1,LOW); delay(500);

digitalWrite(led2,HIGH); Duration=250;

digitalWrite(led3,LOW); tone(PIEZO, 148,Duration);

digitalWrite(led4,LOW); digitalWrite(led1,LOW);

delay(500); digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
Duration=250;
digitalWrite(led4,HIGH);
tone(PIEZO, 128,Duration);
delay(500);
digitalWrite(led1,LOW);
noTone(PIEZO); }
digitalWrite(led2,LOW);
CHALLENGE

• Try to write the last code with a loop.


The Piezoelectric Buzzers
#define PIEZO 3 // pin 3 is capable of PWM
int del = 500;
int melody[] = {262, 196, 196, 220, 196, 0, 247, 262};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
// note durations: 4 = quarter note,
//8 = eighth note, etc.:
void setup(){
pinMode(PIEZO, OUTPUT);
for (int i = 0; i < 8; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(PIEZO, melody[i], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(PIEZO);
}
delay(1000);
}
Practice Exercise1:
We have a temperature sensor and a LED.
The serial monitor display the sensor value:
• If its between 5 and 15, the LED fade forward and
backward
• If its greater than 15, the LED blinks
Practice Exercise3:
using LED, piezo, Photo resistor, push buttons…

Write an Arduino program that reads the incident light


intensity from PhotoResistor:
• If the value is less than 50 the piezo should play a sound.
• The brightness of LED should be increased when the
incident light decreases, and vice versa.
• Nothing will happen before clicking on the push button.

You might also like