Gambar 1. Piezo Buzzer dan Cara kerjanya
Piezo Buzzer memiliki dua kabel yaitu warna merah dan hitam, dimana polarity adalah penting (tidak bisa terbalik), dengan kebel hitam dihubungkan ke ground, dan buzzer bekerja pada tegangan 3~16 volt. Agar Buzzer dapat menghasilkan suara, maka kabel merah adalah dihubungkan ke pin board NodeMCU yang mendukung sinyal PWM (bukan hanya HIGH dan LOW).
Gambar 2. Pin pada board NodeMCU yang mendukung PWM
Kebetulan Buzzer yang saya miliki tidak memiliki kabel warna merah dan hitam, tetapi memiliki kaki pin, dimana pin yang lebih panjang adalah Anoda (+) atau ada tanda + pada tutupnya dan yang pendek adalah Cathoda (-).
Gambar 3. Buzzer dengan pin (panjang Anoda, pendek Cathoda)
Langkah-langkah:
1. Hubungkan pin Cathoda (-) ke GND pada board NodeMCU
2. Hubungkan pin Anoda(+) ke pin D7 pada board NodeMCU
3. Lakukan koding sebagai berikut ini yang diadaptasi dari Arduino untuk mensimulasikan suara sirene:
/*
Siren
//Copyright (c) 2012 Jeremy Fonte
//This code is released under the MIT license
//http://opensource.org/licenses/MIT
*/
const int pitchLow = 200;
const int pitchHigh = 1000;
int pitchStep = 10;
int currentPitch;
int delayTime;
const int speakerPin = D7;
void setup() {
currentPitch = pitchLow;
delayTime = 10;
}
void loop() {
tone(speakerPin, currentPitch, 10);
currentPitch += pitchStep;
if(currentPitch >= pitchHigh) {
pitchStep = -pitchStep;
}
else if(currentPitch <= pitchLow) {
pitchStep = -pitchStep;
}
delay(delayTime);
}
Siren
//Copyright (c) 2012 Jeremy Fonte
//This code is released under the MIT license
//http://opensource.org/licenses/MIT
*/
const int pitchLow = 200;
const int pitchHigh = 1000;
int pitchStep = 10;
int currentPitch;
int delayTime;
const int speakerPin = D7;
void setup() {
currentPitch = pitchLow;
delayTime = 10;
}
void loop() {
tone(speakerPin, currentPitch, 10);
currentPitch += pitchStep;
if(currentPitch >= pitchHigh) {
pitchStep = -pitchStep;
}
else if(currentPitch <= pitchLow) {
pitchStep = -pitchStep;
}
delay(delayTime);
}
4. Lakukan verify dan Upload.
Anda dapat juga menggunakan analogWrite untuk menghasilkan suara pada buzzer seperti yang ditunjukan pada koding berikut ini:
/*
Piezo
This example shows how to run a Piezo Buzzer on pin D7
using the analogWrite() function.
It beeps 3 times fast at startup, waits a second then beeps continuously
at a slower pace
*/
const int speakerPin = D7;
void setup() {
// declare pin D7 to be an output:
pinMode(speakerPin, OUTPUT);
beep(50);
beep(50);
beep(50);
delay(1000);
}
void loop() {
beep(200);
}
void beep(unsigned char delayms){
analogWrite(speakerPin, 20); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(speakerPin, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
Piezo
This example shows how to run a Piezo Buzzer on pin D7
using the analogWrite() function.
It beeps 3 times fast at startup, waits a second then beeps continuously
at a slower pace
*/
const int speakerPin = D7;
void setup() {
// declare pin D7 to be an output:
pinMode(speakerPin, OUTPUT);
beep(50);
beep(50);
beep(50);
delay(1000);
}
void loop() {
beep(200);
}
void beep(unsigned char delayms){
analogWrite(speakerPin, 20); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(speakerPin, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
Kalau yang ini mensimulasikan suara jangrik:
/*
Crickets
Adaptation from http://misharabinovich.com/blog/?p=246
*/
const int speakerPin = D7;
int number_of_crickets;
int chirp = 50;
void setup() {
number_of_crickets = 1000 * (5 / 1023.0);
}
void loop() {
analogWrite(speakerPin,number_of_crickets);
delayMicroseconds(chirp);
analogWrite(speakerPin,0);
delayMicroseconds(chirp);
}
Crickets
Adaptation from http://misharabinovich.com/blog/?p=246
*/
const int speakerPin = D7;
int number_of_crickets;
int chirp = 50;
void setup() {
number_of_crickets = 1000 * (5 / 1023.0);
}
void loop() {
analogWrite(speakerPin,number_of_crickets);
delayMicroseconds(chirp);
analogWrite(speakerPin,0);
delayMicroseconds(chirp);
}
Salam,
Hendra.
Tidak ada komentar:
Posting Komentar