active passive buzzer 有/无源蜂鸣器 树莓派3

蜂鸣器模块原理图

Active vs. Passive Buzzer 有源蜂鸣器内置振荡器,通电就会发出声音。无源蜂鸣器相反,直流电并不能让它发声。无源蜂鸣器,需要使用频率在2k到5k之间的方波来驱动。文章内容有模块原理图,树莓派3接线图,相关代码。

面包板接线图:有源蜂鸣器和无源蜂鸣器一样.

active passive buzzer

有源蜂鸣器SIG接地三极管1和3脚导通,蜂鸣器发出蜂鸣声,声音单一.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <wiringPi.h>
#include <stdio.h>

#define BuzzerPin 0

int main(void)
{
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}

pinMode(BuzzerPin, OUTPUT);

while(1){
digitalWrite(BuzzerPin, HIGH);
delay(100);
digitalWrite(BuzzerPin, LOW);
delay(100);
}

return 0;
}

无源蜂鸣器的SIG脚,根据树莓派3产生的方波,发出蜂鸣声,可发出不同声音.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <wiringPi.h>
#include <softTone.h>
#include <stdio.h>

#define BuzPin 0


#define CL1 131 //低音
#define CL2 147
#define CL3 165
#define CL4 175
#define CL5 196
#define CL6 221
#define CL7 248

#define CM1 262 //中音
#define CM2 294
#define CM3 330
#define CM4 350
#define CM5 393
#define CM6 441
#define CM7 495

#define CH1 525 //高音
#define CH2 589
#define CH3 661
#define CH4 700
#define CH5 786
#define CH6 882
#define CH7 990

int song_1[] = {CM3,CM5,CM6,CM3,CM2,CM3,CM5,CM6,CH1,CM6,CM5,CM1,CM3,CM2,
CM2,CM3,CM5,CM2,CM3,CM3,CL6,CL6,CL6,CM1,CM2,CM3,CM2,CL7,
CL6,CM1,CL5};

int beat_1[] = {1,1,3,1,1,3,1,1,1,1,1,1,1,1,3,1,1,3,1,1,1,1,1,1,1,2,1,1,
1,1,1,1,1,1,3};


int song_2[] = {CM1,CM1,CM1,CL5,CM3,CM3,CM3,CM1,CM1,CM3,CM5,CM5,CM4,CM3,CM2,
CM2,CM3,CM4,CM4,CM3,CM2,CM3,CM1,CM1,CM3,CM2,CL5,CL7,CM2,CM1
};

int beat_2[] = {1,1,1,3,1,1,1,3,1,1,1,1,1,1,3,1,1,1,2,1,1,1,3,1,1,1,3,3,2,3};

int main(void)
{
int i, j;

if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}

if(softToneCreate(BuzPin) == -1){
printf("setup softTone failed !");
return 1;
}

while(1){
printf("music is being played...\n");

for(i=0;i<sizeof(song_1)/4;i++){
printf("1\n");
softToneWrite(BuzPin, song_1[i]);
printf("2\n");
delay(beat_1[i] * 500);
printf("3\n");
}

for(i=0;i<sizeof(song_2)/4;i++){
printf("a\n");
softToneWrite(BuzPin, song_2[i]);
printf("b\n");
delay(beat_2[i] * 500);
printf("c\n");
}
}

return 0;
}

树莓派3更多模块请点击链接

从入门到放弃的学习RASPBERRYPI