蜂鳴器介紹
1205的標示為
外徑12mm ,工作電壓5V。
可以產生聲音的機構有蜂鳴器與揚聲器(喇叭),兩者工作模式不同,特色不同。
蜂鳴器的工作平寬較窄,適合在特定頻率使用。
揚聲器的工作平寬較大,適合音樂與語音輸出。
MCU使用而言
蜂鳴器>>壓電效應與電磁效應。
揚聲器>>電磁效應。
蜂鳴器(Buzzer)是產生聲音的信號裝置,有機械型、機電型及壓電型。蜂鳴器的典型應用包括警笛、報警裝置、火災警報器、防空警報器、防盜器、定時器等。<參考維基百科>
本次使用的小型蜂鳴器都是電磁式蜂鳴器,主要依靠金屬薄片與電磁線圈推斥來產生振動,發出聲音。 此類蜂鳴器一般分為自帶震盪源(自激式)或不帶震盪源(他激式)兩種。自激式的蜂鳴器時直接使用直流電即可發出固定聲音頻率;而他激式的蜂鳴器則需要提供聲音訊號才能發聲。
要能夠發出不同頻率的聲音,則必須注意選用他激式,選用時仍需要注意蜂鳴器上的工電壓。111
聲音的原理
聲音是一種波動,聲音的振動會引起空氣分子有節奏的振動,使周圍的空氣產 生疏密變化,形成疏密相間的縱波,因而產生了聲波。人耳可以聽到的聲音頻率範 圍在 20Hz~20kHz 之間。
Arduino 利用 tone()函式產生如圖所示方波 聲音信號來模擬真實的聲音,一次只能輸出一個音調。
簡易蜂鳴器電路(直接單晶片輸出)<檔案位置>
A點 為直接驅動蜂鳴器 (圖為speaker)
B點 則經電晶體放大後的接線圖等
電路上若已經接好 七段顯示器 GPIO腳位不同,可不用拆除,
電路未清楚標示避免混亂 刪除七段顯示器接線。1
電話鈴聲程式
#include <ESP32Servo.h>
#define speaker 19 //數位接腳19 連接至喇叭。
void setup(){
pinMode(speaker, OUTPUT);
}
void loop(){
for(int i=0;i<10;i++){ //振鈴10 次。
tone(speaker,1000); //輸出1000Hz 音調。
delay(50); //延遲50ms。
tone(speaker,500); //輸出500Hz 音調。
delay(50); //延遲50ms。
} //END for(int i=0;i<10;i++){ //振鈴10 次
noTone(speaker); //靜音2 秒。
delay(2000);
} //END void loop(){
電話鈴聲程式的輸出波形
使用示波器或邏輯分析儀
發出音階
/* 陣 列 宣 告 與 練 習 第一部 聲音認識
2022.05.02 竹北高中 多元選修
指令部分
tone(Pin,f,t); //播音指令 f頻率 時間
聲音的產生是由於『振動』產生的效果,振動的頻率愈高,則音調愈高
音調 Do Do# Re Re# Mi Fa Fa# So So# La La# Si
低音 頻率 262 277 294 311 330 349 370 392 415 440 464 494
中音 頻率 523 554 587 622 659 698 740 784 831 880 932 988
高音 頻率 1046 1109 1175 1245 1318 1397 1480 1568 1661 1760 1865 1976
就以上頻率發現中音為低音的2倍頻率,高音為低音的4倍頻率。
*/
//請問 如何撥放出21個 低、中、高 音階
#include <ESP32Servo.h>
const int speed=48; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
#define duration 500
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
}
//==================================
void loop() {
tone(buzzerPin,262,duration); //播音 Do
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,294,duration); //播音 Re
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,330,duration); //播音 Mi
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,349,duration); //播音 Fa
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,392,duration); //播音 So
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,440,duration); //播音 La
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,494,duration); //播音 Si
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
delay(1000000000); //暫停5秒
}
發出音階 C調 低到高21個音階
//Ctone21.ino
#include <ESP32Servo.h>
const int speed=48; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
#define duration 500
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
Serial.begin(9600);
}
//==================================
void loop() {
int x;
for(int i=0;i<3;i++){
if(i==0) x=1;
if(i==1) x=2;
if(i==2) x=4;
Serial.print("i=");
Serial.print(i);
Serial.print(" x =");
Serial.println(x);
tone(buzzerPin,262*x,duration); //播音 Do
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,294*x,duration); //播音 Re
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,330*x,duration); //播音 Mi
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,349*x,duration); //播音 Fa
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,392*x,duration); //播音 So
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,440*x,duration); //播音 La
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,494*x,duration); //播音 Si
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
}
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
delay(1000000000); //暫停5秒
}
/* 陣 列 宣 告 與 練 習 第一部 聲音認識
2022.05.02 竹北高中 多元選修
指令部分
tone(Pin,f,t); //播音指令 f頻率 時間
聲音的產生是由於『振動』產生的效果,振動的頻率愈高,則音調愈高
音調 Do Do# Re Re# Mi Fa Fa# So So# La La# Si
低音 頻率 262 277 294 311 330 349 370 392 415 440 464 494
中音 頻率 523 554 587 622 659 698 740 784 831 880 932 988
高音 頻率 1046 1109 1175 1245 1318 1397 1480 1568 1661 1760 1865 1976
就以上頻率發現中音為低音的2倍頻率,高音為低音的4倍頻率。
*/
//請問 如何撥放出21個 低、中、高 音階
//Ctone22.ino
#include <ESP32Servo.h>
const int speed=48; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
#define duration 500
//宣告基本音符 //Do Re Mi Fa So La Si
const short sound[]={5000, 262, 294, 330, 349, 392, 440, 494,
523, 587, 659, 698, 784, 880, 988,
1046,1175,1318,1397,1568,1760,1976 };
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
Serial.begin(9600);
}
//==================================
void loop() {
for(int i=1;i<22;i++){
Serial.print(i);
Serial.print("=");
Serial.println(sound[i]);
tone(buzzerPin,sound[i],duration); //播音 Do
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
}
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
Serial.print("END wait 6000 S");
delay(1000000000); //暫停100萬秒
}
/* 陣 列 宣 告 與 練 習 第一部 聲音認識
2022.05.02 竹北高中 多元選修
指令部分
tone(Pin,f,t); //播音指令 f頻率 時間
聲音的產生是由於『振動』產生的效果,振動的頻率愈高,則音調愈高
音調 Do Do# Re Re# Mi Fa Fa# So So# La La# Si
低音 頻率 262 277 294 311 330 349 370 392 415 440 464 494
中音 頻率 523 554 587 622 659 698 740 784 831 880 932 988
高音 頻率 1046 1109 1175 1245 1318 1397 1480 1568 1661 1760 1865 1976
就以上頻率發現中音為低音的2倍頻率,高音為低音的4倍頻率。
*/
//請問 如何撥放出21個 低、中、高 音階
//請問 如何撥放出21個 低、中、高 音階
#include <ESP32Servo.h>
const int speed=48; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
#define duration 500
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
Serial.begin(9600);
}
//==================================
void loop() {
int x;
for(int i=0;i<3;i++){
if(i==0) x=1;
if(i==1) x=2;
if(i==2) x=4;
Serial.print("i=");
Serial.print(i);
Serial.print(" x =");
Serial.println(x);
tone(buzzerPin,262*x,duration); //播音 Do
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,294*x,duration); //播音 Re
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,330*x,duration); //播音 Mi
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,349*x,duration); //播音 Fa
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,392*x,duration); //播音 So
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,440*x,duration); //播音 La
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
tone(buzzerPin,494*x,duration); //播音 Si
delay(duration*1.3); //暫停
noTone(buzzerPin); //靜音
}
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0 以免燒毀
delay(1000000000); //暫停5秒
}
/* 陣 列 宣 告 與 練 習 第一部 聲音認識
2022.05.02 竹北高中 多元選修
指令部分
tone(Pin,f,t); //播音指令 f頻率 時間
聲音的產生是由於『振動』產生的效果,振動的頻率愈高,則音調愈高
音調 Do Do# Re Re# Mi Fa Fa# So So# La La# Si
低音 頻率 262 277 294 311 330 349 370 392 415 440 464 494
中音 頻率 523 554 587 622 659 698 740 784 831 880 932 988
高音 頻率 1046 1109 1175 1245 1318 1397 1480 1568 1661 1760 1865 1976
就以上頻率發現中音為低音的2倍頻率,高音為低音的4倍頻率。
*/
//請問 如何撥放出21個 低、中、高 音階
編輯歌曲
//ESP32的計時器 與 UNO不同
#include <ESP32Servo.h>
const int speed=60; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
//宣告基本音符
const int Note[]={ 494, 523,587,659,698,784,880,988, 1046,1175,1318};
//宣告望春風音階陣列
const int song[]={ 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 3, 12, 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 1, 12, 2, 3, 2, 5, 6, 5, 6, 8, 9, 8, 7, 6, 9, 5, 12,
6, 8, 5, 6, 3, 5, 6, 5, 3, 2, 3, 5, 3, 2, 1, 100 }; //100為結束符號
//宣告望春風節拍陣列
const int beat[]={ 8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
8, 4, 4, 16, 8, 4, 4, 16, 8, 4, 4, 8, 8, 24, 8,
8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 4, 4, 8, 8, 24, 8};
//happy brithday 25
unsigned char song1[]={1,1,2,1,4,03, 1,1,2,1,5, 4, 1,1,8,6,4,3,2, 7,7,6,4,5, 4,100};
unsigned char beat1[]={4,4,8,8,8,16, 4,4,8,8,8,16, 4,4,8,8,8,8,8, 4, 4,8,8,8,16 };
//littie bird 33
unsigned char song2[]={1,1,1,3,2,1, 3,3,3,5,4,3, 5,4,3, 2, 2, 1, 1, 2, 3, 4, 3, 2, 3, 4, 5, 5, 4, 3, 2, 1,100 };
unsigned char beat2[]={4,4,4,6,2,4, 4,4,4,6,2,4, 4,4,4,12, 8, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 2, 2, 4, 4, 8, };
//home
unsigned char song3[]={8,7,6,5,5, 6,8, 5,6,5, 3,2,5, 3,12, 8,7,6,5,5, 6,8, 5, 6,5,3, 2,5, 1, 12,2,3,2, 5, 6, 5, 6, 8, 9, 8, 7, 6, 9, 5, 12, 6, 8, 5, 6, 3, 5, 6, 5, 3, 2, 3, 5, 3, 2, 1, 100 };
unsigned char beat3[]={8,4,4,8,8, 8,8,16,8,4, 4,8,8,24, 8, 8,4,4,8,8, 8,8,16, 8,4,4, 8,8,24, 8,8,4,4,16, 8, 4, 4, 16, 8, 4, 4, 8, 8, 24, 8, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 4, 4, 8, 8, 24, 8,100 };
//Spring Breeze 50
unsigned char song4[]={ 2, 2, 3, 5, 6, 5, 6, 7, 9, 7, 7, 6, 5, 6, 7, 9, 9, 7, 9, 5, 6, 6, 2, 7, 7, 6, 5, 5, 6, 6, 7, 6, 5, 3, 2, 3, 5, 3, 5, 6, 7, 9, 9, 9, 10, 9, 7, 7, 6, 5, 3, 2, 7, 7, 6, 5, 5, 100};
unsigned char beat4[]={ 12, 4, 8, 8, 8, 4, 4, 16, 12, 4, 4, 4, 8, 32,12, 4, 8, 4, 4, 12, 4, 16, 12, 4, 8, 4, 4, 32, 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 8, 8, 8, 32, 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 4, 8, 4, 4, 32 };
//little star 42
unsigned char song5[] = {1,1,5,5,6,6,5, 4,4,3,3,2,2,1, 5,5,4,4,3,3,2, 5,5,4,4,3,3,2, 1,1,5,5,6,6,5, 4,4,3,3,2,2,1,100};
unsigned char beat5[] = {4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8};
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0
}
//==================================
void loop() {
int x=0; //宣告取音指標變數
while(song4[x]!=100){ //若非結束符號,則執行迴圈
int duration = speed*beat4[x]; //計算節拍
tone(buzzerPin,Note[song4[x]],duration); //播音
delay(duration*1.0); //暫停
x++; //下一個音
}
noTone(buzzerPin); //靜音
delay(100000); //暫停100秒
}
//ESP32的計時器 與 UNO不同
#include <ESP32Servo.h>
const int speed=36; //宣告播放速度
const int buzzerPin=17; //宣告buzzerPin信號連接17腳
//宣告基本音符
const int Note[]={ 494, 523,587,659,698,784,880,988, 1046,1175,1318};
//宣告望春風音階陣列
const int song[]={ 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 3, 12, 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 1, 12, 2, 3, 2, 5, 6, 5, 6, 8, 9, 8, 7, 6, 9, 5, 12,
6, 8, 5, 6, 3, 5, 6, 5, 3, 2, 3, 5, 3, 2, 1, 100 }; //100為結束符號
//宣告望春風節拍陣列
const int beat[]={ 8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
8, 4, 4, 16, 8, 4, 4, 16, 8, 4, 4, 8, 8, 24, 8,
8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 4, 4, 8, 8, 24, 8};
//happy brithday 25
unsigned char song1[]={1,1,2,1,4,03, 1,1,2,1,5, 4, 1,1,8,6,4,3,2, 7,7,6,4,5, 4,100};
unsigned char beat1[]={4,4,8,8,8,16, 4,4,8,8,8,16, 4,4,8,8,8,8,8, 4, 4,8,8,8,16 };
//littie bird 33
unsigned char song2[]={1,1,1,3,2,1, 3,3,3,5,4,3, 5,4,3, 2, 2, 1, 1, 2, 3, 4, 3, 2, 3, 4, 5, 5, 4, 3, 2, 1,100 };
unsigned char beat2[]={4,4,4,6,2,4, 4,4,4,6,2,4, 4,4,4,12, 8, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 2, 2, 4, 4, 8, };
//home
unsigned char song3[]={8,7,6,5,5, 6,8, 5,6,5, 3,2,5, 3,12, 8,7,6,5,5, 6,8, 5, 6,5,3, 2,5, 1, 12,2,3,2, 5, 6, 5, 6, 8, 9, 8, 7, 6, 9, 5, 12, 6, 8, 5, 6, 3, 5, 6, 5, 3, 2, 3, 5, 3, 2, 1, 100 };
unsigned char beat3[]={8,4,4,8,8, 8,8,16,8,4, 4,8,8,24, 8, 8,4,4,8,8, 8,8,16, 8,4,4, 8,8,24, 8,8,4,4,16, 8, 4, 4, 16, 8, 4, 4, 8, 8, 24, 8, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 4, 4, 8, 8, 24, 8,100 };
//Spring Breeze 50
unsigned char song4[]={ 2, 2, 3, 5, 6, 5, 6, 7, 9, 7, 7, 6, 5, 6, 7, 9, 9, 7, 9, 5, 6, 6, 2, 7, 7, 6, 5, 5, 6, 6, 7, 6, 5, 3, 2, 3, 5, 3, 5, 6, 7, 9, 9, 9, 10, 9, 7, 7, 6, 5, 3, 2, 7, 7, 6, 5, 5, 100};
unsigned char beat4[]={ 12, 4, 8, 8, 8, 4, 4, 16, 12, 4, 4, 4, 8, 32,12, 4, 8, 4, 4, 12, 4, 16, 12, 4, 8, 4, 4, 32, 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 8, 8, 8, 32, 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 4, 8, 4, 4, 32 };
//little star 42
unsigned char song5[] = {1,1,5,5,6,6,5, 4,4,3,3,2,2,1, 5,5,4,4,3,3,2, 5,5,4,4,3,3,2, 1,1,5,5,6,6,5, 4,4,3,3,2,2,1,100};
unsigned char beat5[] = {4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8, 4,4,4,4,4,4,8};
//==================================
void setup() {
pinMode(buzzerPin, OUTPUT); //將buzzerPin腳規畫為輸出埠
digitalWrite(buzzerPin, 0); //buzzerPin初值設定為0
}
//==================================
void loop() {
int x=0; //宣告取音指標變數
while(song4[x]!=100){ //若非結束符號,則執行迴圈
int duration = speed*beat4[x]; //計算節拍
tone(buzzerPin,Note[song4[x]],duration); //播音
delay(duration*1.0); //暫停
x++; //下一個音
}
noTone(buzzerPin); //靜音
delay(100000); //暫停100秒
}