0% found this document useful (0 votes)
12 views11 pages

CSCI373 - Week7 - Piezo

robotics notes for piezo Lebanese international university.

Uploaded by

husseinalayan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

CSCI373 - Week7 - Piezo

robotics notes for piezo Lebanese international university.

Uploaded by

husseinalayan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy