아두이노2015. 9. 3. 16:54

아두이노에서는 피에조부저를 이용해서 간단하게 소리를 낼 수가 있다.

이를 이용하여 간단한 멜로디를 만들어보자.


피에조 부저의 모습


아두이노와 피에조 부저의 연결은 다음과 같다.

피에조 부저는 극성이 있다. 핀이 긴쪽이 + 이고 짧은쪽이 - 이다.

짧은쪽은 GND에 긴쪽은 아두이노 디지털 단자에 연결해 준다.



##소스 1##

BEEP 음을 1초 마다 내는 소스, tone() 의 파라미터를 조절하면 다양한 음을 만들 수 있다.

1
2
3
4
5
6
7
8
9
int speakerpin = 12//스피커가 연결된 디지털핀 설정
 
void setup() {
}
 
void loop() {
  tone(speakerpin,500,1000);  //500: 음의 높낮이(주파수), 1000: 음의 지속시간(1초)
  delay(2000); 
}

cs


##결과 1##



##소스 2##

도레미파솔라시도를 연주한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int speakerpin = 12//스피커가 연결된 디지털핀 설정
int note[] = {2093,2349,2637,2793,3136,3520,3951,4186}; //도레미파솔라시도
 
void setup() {
  int elementCount = sizeof(note) / sizeof(int);
  for (int i=0; i < elementCount; i++)    //note를 play
  {
    tone(speakerpin,note[i],500);
    delay(750);
  }
}
 
void loop() {
}
cs



##결과 2##




위와 같이 도레미파솔라시도를 연주할 경우 각각의 음계의 tone값을 적어주어야 한다. 만약 tone값을 모를 경우 멜로디를 만들기가 아주 힘들다. 이런 경우 사용할 수 있도록 tone값에 대해서 미리 정의해 놓은 pitches.h 라는 헤더파일을 만들어 이용하면 손쉽게 멜로디를 만들 수 있다.


※ 참조: https://www.arduino.cc/en/Tutorial/Tone


아래의 소스를 pitches.h 라는 이름으로 프로그램과 같은 폴더에 저장해 놓고 프로그램에서 include하여 사용하면 된다.


## pitches.h ##

아래의 소스를 pitches.h 라는 이름으로 프로그램과 같은 폴더에 저장


/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31

#define NOTE_C1  33

#define NOTE_CS1 35

#define NOTE_D1  37

#define NOTE_DS1 39

#define NOTE_E1  41

#define NOTE_F1  44

#define NOTE_FS1 46

#define NOTE_G1  49

#define NOTE_GS1 52

#define NOTE_A1  55

#define NOTE_AS1 58

#define NOTE_B1  62

#define NOTE_C2  65

#define NOTE_CS2 69

#define NOTE_D2  73

#define NOTE_DS2 78

#define NOTE_E2  82

#define NOTE_F2  87

#define NOTE_FS2 93

#define NOTE_G2  98

#define NOTE_GS2 104

#define NOTE_A2  110

#define NOTE_AS2 117

#define NOTE_B2  123

#define NOTE_C3  131

#define NOTE_CS3 139

#define NOTE_D3  147

#define NOTE_DS3 156

#define NOTE_E3  165

#define NOTE_F3  175

#define NOTE_FS3 185

#define NOTE_G3  196

#define NOTE_GS3 208

#define NOTE_A3  220

#define NOTE_AS3 233

#define NOTE_B3  247

#define NOTE_C4  262

#define NOTE_CS4 277

#define NOTE_D4  294

#define NOTE_DS4 311

#define NOTE_E4  330

#define NOTE_F4  349

#define NOTE_FS4 370

#define NOTE_G4  392

#define NOTE_GS4 415

#define NOTE_A4  440

#define NOTE_AS4 466

#define NOTE_B4  494

#define NOTE_C5  523

#define NOTE_CS5 554

#define NOTE_D5  587

#define NOTE_DS5 622

#define NOTE_E5  659

#define NOTE_F5  698

#define NOTE_FS5 740

#define NOTE_G5  784

#define NOTE_GS5 831

#define NOTE_A5  880

#define NOTE_AS5 932

#define NOTE_B5  988

#define NOTE_C6  1047

#define NOTE_CS6 1109

#define NOTE_D6  1175

#define NOTE_DS6 1245

#define NOTE_E6  1319

#define NOTE_F6  1397

#define NOTE_FS6 1480

#define NOTE_G6  1568

#define NOTE_GS6 1661

#define NOTE_A6  1760

#define NOTE_AS6 1865

#define NOTE_B6  1976

#define NOTE_C7  2093

#define NOTE_CS7 2217

#define NOTE_D7  2349

#define NOTE_DS7 2489

#define NOTE_E7  2637

#define NOTE_F7  2794

define NOTE_FS7 2960

#define NOTE_G7  3136

#define NOTE_GS7 3322

#define NOTE_A7  3520

#define NOTE_AS7 3729

#define NOTE_B7  3951

#define NOTE_C8  4186

#define NOTE_CS8 4435

#define NOTE_D8  4699

#define NOTE_DS8 4978






##소스 3##

pitches.h를 이용하여 '학교종이 떙땡땡' 노래를 연주한다.


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
 
#include "pitches.h"
 
int speakerpin = 12//스피커가 연결된 디지털핀 설정
 
//음계 데이터 (24개)
int melody[] = {NOTE_G7,NOTE_G7,NOTE_A7,NOTE_A7,NOTE_G7,
                NOTE_G7,NOTE_E7,NOTE_G7,NOTE_G7,NOTE_E7,
                NOTE_E7,NOTE_D7,NOTE_G7,NOTE_G7,NOTE_A7,
                NOTE_A7,NOTE_G7,NOTE_G7,NOTE_E7,NOTE_G7,
                NOTE_E7,NOTE_D7,NOTE_E7,NOTE_C7};
//음의길이, 4:4분음표, 2:2분음표
int noteDurations[] = {4,4,4,4,4,4,2,4,4,4,4,1,4,4,4,4,4,4,2,4,4,4,4,1};
 
void setup() {
  for (int thisNote = 0; thisNote < 24; thisNote++)
  {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(speakerpin, melody[thisNote], noteDuration); //소리를 낸다.
    int pauseBetweenNotes = noteDuration * 1.30;      //delay 계산식
    delay(pauseBetweenNotes);                         //delay
    noTone(speakerpin);                               //대상핀 출력 중단
  }
}
 
void loop() {
}
cs


##결과 3##
뭔가 음정이 이상하지만 비슷하게는 된다. ^^


반응형
Posted by 대네브 (deneb)

댓글을 달아 주세요

  1. hihi

    좋은 정보 감사합니다! 프로젝트하는데 상당한 도움을 주었습니다~

    2015.11.01 22:08 [ ADDR : EDIT/ DEL : REPLY ]
  2. tjfgk12

    #ifndef PITCHES_H
    #define PITCHES_H

    #define note_C0 16.3515978313 //C0, 27.5*(2^(1/12))^-9
    #define note_Db0 17.3239144361 //C#0/Db0, 27.5*(2^(1/12))^-8
    #define note_D0 18.3540479948 //D0, 27.5*(2^(1/12))^-7
    #define note_Eb0 19.4454364826 //D#0/Eb0, 27.5*(2^(1/12))^-6
    #define note_E0 20.6017223071 //E0, 27.5*(2^(1/12))^-5
    #define note_F0 21.8267644646 //F0, 27.5*(2^(1/12))^-4
    #define note_Gb0 23.1246514195 //F#0/Gb0, 27.5*(2^(1/12))^-3
    #define note_G0 24.4997147489 //G0, 27.5*(2^(1/12))^-2
    #define note_Ab0 25.9565435987 //G#0/Ab0, 27.5*(2^(1/12))^-1
    #define note_A0 27.5 //A0, 27.5*(2^(1/12))^0
    #define note_Bb0 29.1352350949 //A#0/Bb0, 27.5*(2^(1/12))^1
    #define note_B0 30.8677063285 //B0, 27.5*(2^(1/12))^2
    #define note_C1 32.7031956626 //C1, 27.5*(2^(1/12))^3
    #define note_Db1 34.6478288721 //C#1/Db1, 27.5*(2^(1/12))^4
    #define note_D1 36.7080959897 //D1, 27.5*(2^(1/12))^5
    #define note_Eb1 38.8908729653 //D#1/Eb1, 27.5*(2^(1/12))^6
    #define note_E1 41.2034446141 //E1, 27.5*(2^(1/12))^7
    #define note_F1 43.6535289291 //F1, 27.5*(2^(1/12))^8
    #define note_Gb1 46.249302839 //F#1/Gb1, 27.5*(2^(1/12))^9
    #define note_G1 48.9994294977 //G1, 27.5*(2^(1/12))^10
    #define note_Ab1 51.9130871975 //G#1/Ab1, 27.5*(2^(1/12))^11
    #define note_A1 55 //A1, 27.5*(2^(1/12))^12
    #define note_Bb1 58.2704701898 //A#1/Bb1, 27.5*(2^(1/12))^13
    #define note_B1 61.735412657 //B1, 27.5*(2^(1/12))^14
    #define note_C2 65.4063913251 //C2, 27.5*(2^(1/12))^15
    #define note_Db2 69.2956577442 //C#2/Db2, 27.5*(2^(1/12))^16
    #define note_D2 73.4161919794 //D2, 27.5*(2^(1/12))^17
    #define note_Eb2 77.7817459305 //D#2/Eb2, 27.5*(2^(1/12))^18
    #define note_E2 82.4068892282 //E2, 27.5*(2^(1/12))^19
    #define note_F2 87.3070578583 //F2, 27.5*(2^(1/12))^20
    #define note_Gb2 92.4986056779 //F#2/Gb2, 27.5*(2^(1/12))^21
    #define note_G2 97.9988589954 //G2, 27.5*(2^(1/12))^22
    #define note_Ab2 103.826174395 //G#2/Ab2, 27.5*(2^(1/12))^23
    #define note_A2 110 //A2, 27.5*(2^(1/12))^24
    #define note_Bb2 116.54094038 //A#2/Bb2, 27.5*(2^(1/12))^25
    #define note_B2 123.470825314 //B2, 27.5*(2^(1/12))^26
    #define note_C3 130.812782651 //C3, 27.5*(2^(1/12))^27
    #define note_Db3 138.591315489 //C#3/Db3, 27.5*(2^(1/12))^28
    #define note_D3 146.832383959 //D3, 27.5*(2^(1/12))^29
    #define note_Eb3 155.563491861 //D#3/Eb3, 27.5*(2^(1/12))^30
    #define note_E3 164.813778456 //E3, 27.5*(2^(1/12))^31
    #define note_F3 174.614115716 //F3, 27.5*(2^(1/12))^32
    #define note_Gb3 184.997211355 //F#3/Gb3, 27.5*(2^(1/12))^33
    #define note_G3 195.997717991 //G3, 27.5*(2^(1/12))^34
    #define note_Ab3 207.65234879 //G#3/Ab3, 27.5*(2^(1/12))^35
    #define note_A3 220 //A3, 27.5*(2^(1/12))^36
    #define note_Bb3 233.081880759 //A#3/Bb3, 27.5*(2^(1/12))^37
    #define note_B3 246.941650628 //B3, 27.5*(2^(1/12))^38
    #define note_C4 261.625565301 //C4, 27.5*(2^(1/12))^39
    #define note_Db4 277.182630978 //C#4/Db4, 27.5*(2^(1/12))^40
    #define note_D4 293.664767918 //D4, 27.5*(2^(1/12))^41
    #define note_Eb4 311.126983722 //D#4/Eb4, 27.5*(2^(1/12))^42
    #define note_E4 329.627556912 //E4, 27.5*(2^(1/12))^43
    #define note_F4 349.228231432 //F4, 27.5*(2^(1/12))^44
    #define note_Gb4 369.994422711 //F#4/Gb4, 27.5*(2^(1/12))^45
    #define note_G4 391.995435981 //G4, 27.5*(2^(1/12))^46
    #define note_Ab4 415.30469758 //G#4/Ab4, 27.5*(2^(1/12))^47
    #define note_A4 440 //A4, 27.5*(2^(1/12))^48
    #define note_Bb4 466.163761518 //A#4/Bb4, 27.5*(2^(1/12))^49
    #define note_B4 493.883301256 //B4, 27.5*(2^(1/12))^50
    #define note_C5 523.251130601 //C5, 27.5*(2^(1/12))^51
    #define note_Db5 554.365261954 //C#5/Db5, 27.5*(2^(1/12))^52
    #define note_D5 587.329535835 //D5, 27.5*(2^(1/12))^53
    #define note_Eb5 622.253967444 //D#5/Eb5, 27.5*(2^(1/12))^54
    #define note_E5 659.255113826 //E5, 27.5*(2^(1/12))^55
    #define note_F5 698.456462866 //F5, 27.5*(2^(1/12))^56
    #define note_Gb5 739.988845423 //F#5/Gb5, 27.5*(2^(1/12))^57
    #define note_G5 783.990871964 //G5, 27.5*(2^(1/12))^58
    #define note_Ab5 830.60939516 //G#5/Ab5, 27.5*(2^(1/12))^59
    #define note_A5 880 //A5, 27.5*(2^(1/12))^60
    #define note_Bb5 932.327523036 //A#5/Bb5, 27.5*(2^(1/12))^61
    #define note_B5 987.766602512 //B5, 27.5*(2^(1/12))^62
    #define note_C6 1046.5022612 //C6, 27.5*(2^(1/12))^63
    #define note_Db6 1108.73052391 //C#6/Db6, 27.5*(2^(1/12))^64
    #define note_D6 1174.65907167 //D6, 27.5*(2^(1/12))^65
    #define note_Eb6 1244.50793489 //D#6/Eb6, 27.5*(2^(1/12))^66
    #define note_E6 1318.51022765 //E6, 27.5*(2^(1/12))^67
    #define note_F6 1396.91292573 //F6, 27.5*(2^(1/12))^68
    #define note_Gb6 1479.97769085 //F#6/Gb6, 27.5*(2^(1/12))^69
    #define note_G6 1567.98174393 //G6, 27.5*(2^(1/12))^70
    #define note_Ab6 1661.21879032 //G#6/Ab6, 27.5*(2^(1/12))^71
    #define note_A6 1760 //A6, 27.5*(2^(1/12))^72
    #define note_Bb6 1864.65504607 //A#6/Bb6, 27.5*(2^(1/12))^73
    #define note_B6 1975.53320502 //B6, 27.5*(2^(1/12))^74
    #define note_C7 2093.0045224 //C7, 27.5*(2^(1/12))^75
    #define note_Db7 2217.46104781 //C#7/Db7, 27.5*(2^(1/12))^76
    #define note_D7 2349.31814334 //D7, 27.5*(2^(1/12))^77
    #define note_Eb7 2489.01586978 //D#7/Eb7, 27.5*(2^(1/12))^78
    #define note_E7 2637.0204553 //E7, 27.5*(2^(1/12))^79
    #define note_F7 2793.82585146 //F7, 27.5*(2^(1/12))^80
    #define note_Gb7 2959.95538169 //F#7/Gb7, 27.5*(2^(1/12))^81
    #define note_G7 3135.96348785 //G7, 27.5*(2^(1/12))^82
    #define note_Ab7 3322.43758064 //G#7/Ab7, 27.5*(2^(1/12))^83
    #define note_A7 3520 //A7, 27.5*(2^(1/12))^84
    #define note_Bb7 3729.31009214 //A#7/Bb7, 27.5*(2^(1/12))^85
    #define note_B7 3951.06641005 //B7, 27.5*(2^(1/12))^86
    #define note_C8 4186.00904481 //C8, 27.5*(2^(1/12))^87
    #define note_Db8 4434.92209563 //C#8/Db8, 27.5*(2^(1/12))^88
    #define note_D8 4698.63628668 //D8, 27.5*(2^(1/12))^89
    #define note_Eb8 4978.03173955 //D#8/Eb8, 27.5*(2^(1/12))^90
    #define note_E8 5274.04091061 //E8, 27.5*(2^(1/12))^91
    #define note_F8 5587.65170293 //F8, 27.5*(2^(1/12))^92
    #define note_Gb8 5919.91076339 //F#8/Gb8, 27.5*(2^(1/12))^93
    #define note_G8 6271.92697571 //G8, 27.5*(2^(1/12))^94
    #define note_Ab8 6644.87516128 //G#8/Ab8, 27.5*(2^(1/12))^95
    #define note_A8 7040 //A8, 27.5*(2^(1/12))^96
    #define note_Bb8 7458.62018429 //A#8/Bb8, 27.5*(2^(1/12))^97
    #define note_B8 7902.1328201 //B8, 27.5*(2^(1/12))^98
    #define note_C9 8372.01808962 //C9, 27.5*(2^(1/12))^99
    #define note_Db9 8869.84419126 //C#9/Db9, 27.5*(2^(1/12))^100
    #define note_D9 9397.27257336 //D9, 27.5*(2^(1/12))^101
    #define note_Eb9 9956.06347911 //D#9/Eb9, 27.5*(2^(1/12))^102
    #define note_E9 10548.0818212 //E9, 27.5*(2^(1/12))^103
    #define note_F9 11175.3034059 //F9, 27.5*(2^(1/12))^104
    #define note_Gb9 11839.8215268 //F#9/Gb9, 27.5*(2^(1/12))^105
    #define note_G9 12543.8539514 //G9, 27.5*(2^(1/12))^106
    #define note_Ab9 13289.7503226 //G#9/Ab9, 27.5*(2^(1/12))^107
    #define note_A9 14080 //A9, 27.5*(2^(1/12))^108
    #define note_Bb9 14917.2403686 //A#9/Bb9, 27.5*(2^(1/12))^109
    #define note_B9 15804.2656402 //B9, 27.5*(2^(1/12))^110
    #define note_C10 16744.0361792 //C7, 27.5*(2^(1/12))^111
    #define note_Db10 17739.6883825 //C#7/Db7, 27.5*(2^(1/12))^112
    #define note_D10 18794.5451467 //D7, 27.5*(2^(1/12))^113
    #define note_Eb10 19912.1269582 //D#7/Eb7, 27.5*(2^(1/12))^114
    #define note_E10 21096.1636424 //E7, 27.5*(2^(1/12))^115
    #define note_F10 22350.6068117 //F7, 27.5*(2^(1/12))^116
    #define note_Gb10 23679.6430535 //F#7/Gb7, 27.5*(2^(1/12))^117
    #define note_G10 25087.7079028 //G7, 27.5*(2^(1/12))^118
    #define note_Ab10 26579.5006451 //G#7/Ab7, 27.5*(2^(1/12))^119
    #define note_A10 28160 //A7, 27.5*(2^(1/12))^120
    #define note_Bb10 29834.4807372 //A#10/Bb10, 27.5*(2^(1/12))^121
    #define note_B10 31608.5312804 //B10, 27.5*(2^(1/12))^122

    #endif

    //A0 기준
    //27.5*(2^(1/12))^0
    //1음 올라갈 때 마다 각 음에 2^(1/12)를 곱해야 합니다.

    2020.09.18 11:03 [ ADDR : EDIT/ DEL : REPLY ]