라즈베리파이에서 GPIO 파이썬을 이용해서 피에조 부저(buzzer) 제어하기

안녕하세요.

이번에 소개할 내용은 라즈베리파이에서 GPIO 파이썬을 이용해서 피에조 부저(buzzer) 제어하기 입니다.

피에조 부저는 간단한 소리를 낼수있는 스피커와 비슷한 역할로

주로 경보음을 내거나 간단한 소리를 낼경우에 많이 사용하고 있습니다.

그래서 이번에는 오픈소스를 이용해 슈퍼마리오 테마송을 피에조 부저를 이용해서 출력을 해보도록 하겠습니다.

우선 작업에 앞서 준비물을 준비해 주시기 바랍니다.

준비물
  • 라즈베리파이 OS가 설치된 라즈베리파이
  • 점퍼케이블 암컷으로 2줄
  • 피에조 부저(buzzer) 1개

점퍼케이블을 아래의 표를 보시고 해당하는 위치에 연결을 해주시기 바랍니다.

image

image

Color Pi GPIO Pin Notes
Orange 11 17
Yellow 39 GND

GPIO핀에 대한 자세한 내용을 원하시는 분은 아래의 링크를 들어가셔서 참고해 주세요.

라즈베리파이에서 GPIO를 사용하기전 알아두어야 할 사항

오픈소스를 이용해서 깃허브에서 다운로드후 실행을 하려고 했습니다만

파이썬3.x버전대를 사용하시는 분들은 실행시 에러가 발생을 하기에

소스코드를 깃허브에서 복사해와 오류를 수정후 사용하도록 하겠습니다.

깃허브 소스는 아래의 링크를 누르시면 받으실수 있습니다.

https://github.com/gumslone/raspi_buzzer_player

깃허브를 이용안하시고 수정된 소스를 사용하실 분들은 아래의 방식을 따라서 해주세요.

Visual Studio Code(비주얼 스튜디오 코드)를 실행해 주시기 바랍니다.

image

실행을 하셨으면 파일명을 gpio_buzzer.py로 추가를 해주시기 바랍니다.

image

깃허브에서 가져온 소스를 수정한 소스는 아래의 코드를 참고해 주세요.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/env python
#---------------------------------------------------
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org>
#---------------------------------------------------
#
# Passive buzzer Pi
# VCC ----------------- 3.3V
# GND ------------------ GND
# SIG ---------------- Pin Gpio27
#
# some notes for melodies were taken from:
# http://www.astlessons.com/pianoforkids1.html
# http://www.astlessons.com/pianoforkids2.html
# where you can get more notes
#
#---------------------------------------------------
import RPi.GPIO as GPIO
import time

buzzer_pin = 27

notes = {
'B0' : 31,
'C1' : 33, 'CS1' : 35,
'D1' : 37, 'DS1' : 39,
'EB1' : 39,
'E1' : 41,
'F1' : 44, 'FS1' : 46,
'G1' : 49, 'GS1' : 52,
'A1' : 55, 'AS1' : 58,
'BB1' : 58,
'B1' : 62,
'C2' : 65, 'CS2' : 69,
'D2' : 73, 'DS2' : 78,
'EB2' : 78,
'E2' : 82,
'F2' : 87, 'FS2' : 93,
'G2' : 98, 'GS2' : 104,
'A2' : 110, 'AS2' : 117,
'BB2' : 123,
'B2' : 123,
'C3' : 131, 'CS3' : 139,
'D3' : 147, 'DS3' : 156,
'EB3' : 156,
'E3' : 165,
'F3' : 175, 'FS3' : 185,
'G3' : 196, 'GS3' : 208,
'A3' : 220, 'AS3' : 233,
'BB3' : 233,
'B3' : 247,
'C4' : 262, 'CS4' : 277,
'D4' : 294, 'DS4' : 311,
'EB4' : 311,
'E4' : 330,
'F4' : 349, 'FS4' : 370,
'G4' : 392, 'GS4' : 415,
'A4' : 440, 'AS4' : 466,
'BB4' : 466,
'B4' : 494,
'C5' : 523, 'CS5' : 554,
'D5' : 587, 'DS5' : 622,
'EB5' : 622,
'E5' : 659,
'F5' : 698, 'FS5' : 740,
'G5' : 784, 'GS5' : 831,
'A5' : 880, 'AS5' : 932,
'BB5' : 932,
'B5' : 988,
'C6' : 1047, 'CS6' : 1109,
'D6' : 1175, 'DS6' : 1245,
'EB6' : 1245,
'E6' : 1319,
'F6' : 1397, 'FS6' : 1480,
'G6' : 1568, 'GS6' : 1661,
'A6' : 1760, 'AS6' : 1865,
'BB6' : 1865,
'B6' : 1976,
'C7' : 2093, 'CS7' : 2217,
'D7' : 2349, 'DS7' : 2489,
'EB7' : 2489,
'E7' : 2637,
'F7' : 2794, 'FS7' : 2960,
'G7' : 3136, 'GS7' : 3322,
'A7' : 3520, 'AS7' : 3729,
'BB7' : 3729,
'B7' : 3951,
'C8' : 4186, 'CS8' : 4435,
'D8' : 4699, 'DS8' : 4978
}

melody = [
notes['E7'], notes['E7'], 0, notes['E7'],
0, notes['C7'], notes['E7'], 0,
notes['G7'], 0, 0, 0,
notes['G6'], 0, 0, 0,
notes['C7'], 0, 0, notes['G6'],
0, 0, notes['E6'], 0,
0, notes['A6'], 0, notes['B6'],
0, notes['AS6'], notes['A6'], 0,
notes['G6'], notes['E7'], notes['G7'],
notes['A7'], 0, notes['F7'], notes['G7'],
0, notes['E7'], 0, notes['C7'],
notes['D7'], notes['B6'], 0, 0,
notes['C7'], 0, 0, notes['G6'],
0, 0, notes['E6'], 0,
0, notes['A6'], 0, notes['B6'],
0, notes['AS6'], notes['A6'], 0,
notes['G6'], notes['E7'], notes['G7'],
notes['A7'], 0, notes['F7'], notes['G7'],
0, notes['E7'], 0, notes['C7'],
notes['D7'], notes['B6'], 0, 0
]
tempo = [
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
]

underworld_melody = [
notes['C4'], notes['C5'], notes['A3'], notes['A4'],
notes['AS3'], notes['AS4'], 0,
0,
notes['C4'], notes['C5'], notes['A3'], notes['A4'],
notes['AS3'], notes['AS4'], 0,
0,
notes['F3'], notes['F4'], notes['D3'], notes['D4'],
notes['DS3'], notes['DS4'], 0,
0,
notes['F3'], notes['F4'], notes['D3'], notes['D4'],
notes['DS3'], notes['DS4'], 0,
0, notes['DS4'], notes['CS4'], notes['D4'],
notes['CS4'], notes['DS4'],
notes['DS4'], notes['GS3'],
notes['G3'], notes['CS4'],
notes['C4'], notes['FS4'], notes['F4'], notes['E3'], notes['AS4'], notes['A4'],
notes['GS4'], notes['DS4'], notes['B3'],
notes['AS3'], notes['A3'], notes['GS3'],
0, 0, 0
]

underworld_tempo = [
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
6, 18, 18, 18,
6, 6,
6, 6,
6, 6,
18, 18, 18, 18, 18, 18,
10, 10, 10,
10, 10, 10,
3, 3, 3
]

adventure_time_melody = [
notes['D5'],
notes['G5'], notes['G5'], notes['G5'], notes['G5'], notes['FS5'],
notes['FS5'], notes['E5'], notes['D5'], notes['E5'], notes['D5'], notes['D5'],
notes['C5'], notes['B5'], notes['A5'], notes['G4'],
0, notes['C5'], notes['B5'], notes['A5'], notes['G4'], 0,
notes['G5'], 0, notes['G5'], notes['G5'], 0, notes['G5'],
notes['FS5'], 0, notes['E5'], notes['E5'], notes['D5'], notes['D5'],
notes['C5'], notes['C5'], notes['C5'], notes['D5'],
notes['D5'], notes['A5'], notes['B5'], notes['A5'], notes['G4'],
notes['G5']
]

adventure_time_tempo = [
24,
24, 12, 12, 12, 24,
12, 24, 24, 24, 12, 24,
12, 12, 12, 12,
24, 12, 24, 24, 12, 24,
24, 24, 24, 12, 24, 12,
24, 24, 24, 12, 12, 24,
8, 24, 24, 8,
8, 24, 12, 24, 24,
12
]

star_wars_melody = [
notes['G4'], notes['G4'], notes['G4'],
notes['EB4'], 0, notes['BB4'], notes['G4'],
notes['EB4'], 0, notes['BB4'], notes['G4'], 0,

notes['D4'], notes['D4'], notes['D4'],
notes['EB4'], 0, notes['BB3'], notes['FS3'],
notes['EB3'], 0, notes['BB3'], notes['G3'], 0,

notes['G4'], 0, notes['G3'], notes['G3'], 0,
notes['G4'], 0, notes['FS4'], notes['F4'],
notes['E4'], notes['EB4'], notes['E4'], 0,
notes['GS3'], notes['CS3'], 0,

notes['C3'], notes['B3'], notes['BB3'], notes['A3'], notes['BB3'], 0,
notes['EB3'], notes['FS3'], notes['EB3'], notes['FS3'],
notes['BB3'], 0, notes['G3'], notes['BB3'], notes['D4'], 0,


notes['G4'], 0, notes['G3'], notes['G3'], 0,
notes['G4'], 0, notes['FS4'], notes['F4'],
notes['E4'], notes['EB4'], notes['E4'], 0,
notes['GS3'], notes['CS3'], 0,

notes['C3'], notes['B3'], notes['BB3'], notes['A3'], notes['BB3'], 0,

notes['EB3'], notes['FS3'], notes['EB3'],
notes['BB3'], notes['G3'], notes['EB3'], 0, notes['BB3'], notes['G3'],
]

star_wars_tempo = [
2, 2, 2,
4, 8, 6, 2,
4, 8, 6, 2, 8,

2, 2, 2,
4, 8, 6, 2,
4, 8, 6, 2, 8,

2, 16, 4, 4, 8,
2, 8, 4, 6,
6, 4, 4, 8,
4, 2, 8,
4, 4, 6, 4, 2, 8,
4, 2, 4, 4,
2, 8, 4, 6, 2, 8,

2, 16, 4, 4, 8,
2, 8, 4, 6,
6, 4, 4, 8,
4, 2, 8,
4, 4, 6, 4, 2, 8,
4, 2, 2,
4, 2, 4, 8, 4, 2,
]

popcorn_melody = [
notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'],
notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'],

notes['A4'], notes['B4'], notes['C5'], notes['B4'], notes['C5'], notes['A4'], notes['B4'], notes['A4'], notes['B4'], notes['G4'],
notes['A4'], notes['G4'],notes['A4'], notes['F4'], notes['A4'],

notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'],
notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'],

notes['A4'], notes['B4'], notes['C5'], notes['B4'], notes['C5'], notes['A4'], notes['B4'], notes['A4'], notes['B4'], notes['G4'],
notes['A4'], notes['G4'],notes['A4'], notes['B4'], notes['C5'],

notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'],
notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'],

notes['E5'], notes['FS5'], notes['G5'], notes['FS5'], notes['G5'], notes['E5'], notes['FS5'], notes['E5'], notes['FS5'], notes['D5'],
notes['E5'], notes['D5'],notes['E5'], notes['C5'], notes['E5'],

notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'],
notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'],

notes['E5'], notes['FS5'], notes['G5'], notes['FS5'], notes['G5'], notes['E5'], notes['FS5'], notes['E5'], notes['FS5'], notes['D5'],
notes['E5'], notes['D5'],notes['B4'], notes['D5'], notes['E5'],
]

popcorn_tempo = [
8,8,8,8,8,8,4,
8,8,8,8,8,8,4,

8,8,8,8,8,8,8,8,8,8,
8,8,8,8,4,

8,8,8,8,8,8,4,
8,8,8,8,8,8,4,

8,8,8,8,8,8,8,8,8,8,
8,8,8,8,4,

8,8,8,8,8,8,4,
8,8,8,8,8,8,4,

8,8,8,8,8,8,8,8,8,8,
8,8,8,8,4,

8,8,8,8,8,8,4,
8,8,8,8,8,8,4,

8,8,8,8,8,8,8,8,8,8,
8,8,8,8,4,
]

twinkle_twinkle_melody = [
notes['C4'], notes['C4'], notes['G4'], notes['G4'], notes['A4'], notes['A4'], notes['G4'],
notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['D4'], notes['C4'],

notes['G4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'],
notes['G4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'],

notes['C4'], notes['C4'], notes['G4'], notes['G4'], notes['A4'], notes['A4'], notes['G4'],
notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['D4'], notes['C4'],
]

twinkle_twinkle_tempo = [
4,4,4,4,4,4,2,
4,4,4,4,4,4,2,

4,4,4,4,4,4,2,
4,4,4,4,4,4,2,

4,4,4,4,4,4,2,
4,4,4,4,4,4,2,
]

crazy_frog_melody = [
notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'],
notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'],
notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'],
notes['A4'],0,

notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'],
notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'],
notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'],
notes['A4'],0,

notes['A3'], notes['G3'], notes['E3'], notes['D3'],

notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'],
notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'],
notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'],
notes['A4'],
]

crazy_frog_tempo = [
2,4,4,8,4,4,4,
2,4,4,8,4,4,4,
4,4,4,8,4,8,4,4,
1,4,

2,4,4,8,4,4,4,
2,4,4,8,4,4,4,
4,4,4,8,4,8,4,4,
1,4,

8,4,4,4,

2,4,4,8,4,4,4,
2,4,4,8,4,4,4,
4,4,4,8,4,8,4,4,
1,
]

deck_the_halls_melody = [
notes['G5'], notes['F5'], notes['E5'], notes['D5'],
notes['C5'], notes['D5'], notes['E5'], notes['C5'],
notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'],
notes['C5'], notes['B4'], notes['C5'], 0,

notes['G5'], notes['F5'], notes['E5'], notes['D5'],
notes['C5'], notes['D5'], notes['E5'], notes['C5'],
notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'],
notes['C5'], notes['B4'], notes['C5'], 0,

notes['D5'], notes['E5'], notes['F5'], notes['D5'],
notes['E5'], notes['F5'], notes['G5'], notes['D5'],
notes['E5'], notes['F5'], notes['G5'], notes['A5'], notes['B5'], notes['C6'],
notes['B5'], notes['A5'], notes['G5'], 0,

notes['G5'], notes['F5'], notes['E5'], notes['D5'],
notes['C5'], notes['D5'], notes['E5'], notes['C5'],
notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'],
notes['C5'], notes['B4'], notes['C5'], 0,
]

deck_the_halls_tempo = [
2, 4, 2, 2,
2, 2, 2, 2,
4, 4, 4, 4, 2, 4,
2, 2, 2, 2,

2, 4, 2, 2,
2, 2, 2, 2,
4, 4, 4, 4, 2, 4,
2, 2, 2, 2,

2,4,2,2,
2,4,2,2,
4,4,2,4,4,2,
2,2,2,2,

2, 4, 2, 2,
2, 2, 2, 2,
4, 4, 4, 4, 2, 4,
2, 2, 2, 2,
]

manaderna_melody = [
notes['E4'],notes['E4'],notes['F4'],notes['G4'],
notes['G4'],notes['F4'],notes['E4'],notes['D4'],
notes['C4'],notes['C4'],notes['D4'],notes['E4'],
notes['E4'],0,notes['D4'],notes['D4'],0,

notes['E4'],notes['E4'],notes['F4'],notes['G4'],
notes['G4'],notes['F4'],notes['E4'],notes['D4'],
notes['C4'],notes['C4'],notes['D4'],notes['E4'],
notes['D4'],0,notes['C4'],notes['C4'],0,

notes['D4'],notes['D4'],notes['E4'],notes['C4'],
notes['D4'],notes['E4'],notes['F4'],notes['E4'],notes['C4'],
notes['D4'],notes['E4'],notes['F4'],notes['E4'],notes['D4'],
notes['C4'],notes['D4'],notes['G3'],0,

notes['E4'],notes['E4'],notes['F4'],notes['G4'],
notes['G4'],notes['F4'],notes['E4'],notes['D4'],
notes['C4'],notes['C4'],notes['D4'],notes['E4'],
notes['D4'],0,notes['C4'],notes['C4'],
]

manaderna_tempo = [
2,2,2,2,
2,2,2,2,
2,2,2,2,
2,4,4,2,4,

2,2,2,2,
2,2,2,2,
2,2,2,2,
2,4,4,2,4,

2,2,2,2,
2,4,4,2,2,
2,4,4,2,2,
2,2,1,4,

2,2,2,2,
2,2,2,2,
2,2,2,2,
2,4,4,2,
]

bonnagard_melody = [
notes['C5'],notes['C5'],notes['C5'],notes['G4'],
notes['A4'],notes['A4'],notes['G4'],
notes['E5'],notes['E5'],notes['D5'],notes['D5'],
notes['C5'],0,notes['G4'],

notes['C5'],notes['C5'],notes['C5'],notes['G4'],
notes['A4'],notes['A4'],notes['G4'],
notes['E5'],notes['E5'],notes['D5'],notes['D5'],
notes['C5'],0,notes['G4'],notes['G4'],

notes['C5'],notes['C5'],notes['C5'],notes['G4'],notes['G4'],
notes['C5'],notes['C5'],notes['G4'],
notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],
notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],0,

notes['C5'],notes['C5'],notes['C5'],notes['G4'],
notes['A4'],notes['A4'],notes['G4'],
notes['E5'],notes['E5'],notes['D5'],notes['D5'],
notes['C5'],0,
]

bonnagard_tempo = [
2,2,2,2,
2,2,1,
2,2,2,2,
1,2,2,

2,2,2,2,
2,2,1,
2,2,2,2,
1,2,4,4,

2,2,2,4,4,
2,2,1,
4,4,2,4,4,2,
4,4,4,4,2,2,4,

2,2,2,2,
2,2,1,
2,2,2,2,
1,1,
]

final_countdown_melody = [
notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'],
notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'],
notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'],
notes['C5'],notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'],
notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'],
notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'],
notes['C5'],notes['B4'],notes['C5'],notes['D5'],notes['C5'],notes['D5'],
notes['E5'],notes['D5'],notes['C5'],notes['B4'],notes['A4'],notes['F5'],
notes['E5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
notes['E5'],
]

final_countdown_tempo = [
1,16,16,4,4,
1,16,16,8,8,4,
1,16,16,4,4,
2,4,16,16,8,8,8,8,
4,4,16,16,4,4,
1,16,16,8,8,4,
1,16,16,4,4,
2,4,16,16,8,8,8,8,
4,16,16,4,16,16,
8,8,8,8,4,4,
2,8,4,16,16,
1,
]

def buzz(frequency, length): #create the function "buzz" and feed it the pitch and duration)

if(frequency==0):
time.sleep(length)
return
period = 1.0 / frequency #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec)
delayValue = period / 2 #calcuate the time for half of the wave
numCycles = int(length * frequency) #the number of waves to produce is the duration times the frequency

for i in range(numCycles): #start a loop from 0 to the variable "cycles" calculated above
GPIO.output(buzzer_pin, True) #set pin 27 to high
time.sleep(delayValue) #wait with pin 27 high
GPIO.output(buzzer_pin, False) #set pin 27 to low
time.sleep(delayValue) #wait with pin 27 low

def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer_pin, GPIO.IN)
GPIO.setup(buzzer_pin, GPIO.OUT)

def destroy():
GPIO.cleanup() # Release resource

def play(melody,tempo,pause,pace=0.800):

for i in range(0, len(melody)): # Play song
noteDuration = pace/tempo[i]
buzz(melody[i],noteDuration) # Change the frequency along the song note

pauseBetweenNotes = noteDuration * pause
time.sleep(pauseBetweenNotes)

if __name__ == '__main__': # Program start from here
try:
setup()
#print("Super Mario Theme")
play(melody, tempo, 1.3, 0.800)
time.sleep(2)
#print("Super Mario Underworld Theme")
play(underworld_melody, underworld_tempo, 1.3, 0.800)
time.sleep(2)
#print("The Final Countdown")
play(final_countdown_melody, final_countdown_tempo, 0.30, 1.2000)
time.sleep(2)
#print("Per Olssons Bonnagard (Old MacDonald Had A Farm) Melody")
play(bonnagard_melody, bonnagard_tempo, 0.30, 0.800)
time.sleep(2)
#print("Manaderna (Symphony No. 9) Melody")
play(manaderna_melody, manaderna_tempo, 0.30, 0.800)
time.sleep(2)
#print("Deck The Halls Melody")
play(deck_the_halls_melody, deck_the_halls_tempo, 0.30, 0.800)
time.sleep(2)
#print("Crazy Frog (Axel F) Theme")
play(crazy_frog_melody, crazy_frog_tempo, 0.30, 0.900)
time.sleep(2)
#print("Twinkle, Twinkle, Little Star Melody")
play(twinkle_twinkle_melody, twinkle_twinkle_tempo, 0.50, 1.000)
time.sleep(2)
#print("Popcorn Melody")
play(popcorn_melody, popcorn_tempo, 0.50, 1.000)
time.sleep(2)
#print("Star Wars Theme")
play(star_wars_melody, star_wars_tempo, 0.50, 1.000)
time.sleep(2)
#print("Adventure Time Theme")
play(adventure_time_melody, adventure_time_tempo, 1.3, 1.500)
destroy()

except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

작성하신 소스를 라즈베리파이에 넣고 실행을 하기위해

소스파일을 라즈베리파이의 임의의 폴더에 올려주시기 바랍니다.

그리고 라즈베리파이를 VNC를 이용해 접속후 터미널을 실행해 주세요.

image

라즈베리파이의 리스트 목록을 보시면 gpio_buzzer.py 파일명으로 파일이 올라가 있는걸 확인할수 있습니다.

이제 명령어로 피에조 부저에서 슈퍼마리오 테마송이 나오는지 확인해 보도록 하겠습니다.

아래의 명령어를 입력해 주세요.

1
2
# 파이썬 2.x버전대를 이용하실경우
$ sudo python ./gpio_buzzer.py
1
2
# 파이썬 3.x버전대를 이용하실경우
$ sudo python3 ./gpio_buzzer.py

저의 경우는 3.x버전대를 이용하기에 3.x버전대의 명령어로 실행을 하겠습니다.

image

피에조 부저에서 소리가 나오게 되면 정상적으로 된것입니다.

만일 소리가 안나올경우 점퍼케이블 연결이나 피에조 부저의 불량을 생각해 보셔야만 합니다.

AkibaTV