피에조 스피커에 대한 기초 전자회로에 대해 알아보고, Push Button 3개를 이용하여 피에조 스피커를 제어해본다.
1. 회로도 구성
- 피에조 스피커의 주파수를 제어하기 위한 Push buttion이 3개 있으며 순서대로 디지털 입력 4,3,2에 연결됨
- 피에조 스피커는 극성이 있으며 +를 디지털 출력 7번핀에 연결됨
2. 회로설명
1) 피에조 스피커
- 전기적 신호를 주면 수축 또는 확장하게 되는 피에조 효과 이용
- 판을 진동시켜 소리를 냄
- 극성있음, 다리가 긴 쪽이 +
- PWM 주기를 변경하여 다양한 음을 만들 수 있음
- 아두이노에서는 tone()을 이용하여 PWM의 주기를 변경
3. 소스코드
AUNO_Basic_03_Piezo_Button.ino
0.00MB
/*****************************************************/ /* Project : Arduino Basic Training Course /* Title : 03_Piezo_Button /* Descript : Piezo Speaker control example with 3 Push Button /* Platform : Arduion UNO /* IDE: Arduino Sketch 1.6/1.8 or vMicro /* Author : shlee853 /* History : Rev 1.0 2020.01.31 - Initial release /* /*****************************************************/ #define PS 7 #define RB 4 #define GB 3 #define BB 2 #define NOTE_C3 131 #define NOTE_D3 147 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_G3 196 #define NOTE_A3 220 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_D4 294 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_G4 392 #define NOTE_A4 440 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_D5 587 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_G5 784 #define NOTE_A5 880 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_D6 1175 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_G6 1568 #define NOTE_A6 1760 #define NOTE_B6 1976 unsigned char cnt=0; int melody[] = { NOTE_C3,NOTE_D3,NOTE_E3,NOTE_F3,NOTE_G3,NOTE_A3,NOTE_B3,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5,NOTE_B5,NOTE_C6,NOTE_D6,NOTE_E6,NOTE_F6,NOTE_G6,NOTE_A6,NOTE_B6 }; void setup() { // put your setup code here, to run once: pinMode(RB, INPUT); pinMode(GB, INPUT); pinMode(BB, INPUT); } void loop() { // put your main code here, to run repeatedly: if(digitalRead(RB) == HIGH) { cnt++; if(cnt>27) cnt = 27; } if(digitalRead(GB) == HIGH) { cnt=0; noTone(PS); } if(digitalRead(BB) == HIGH) { cnt--; if(cnt<0) cnt = 0; } tone(PS, melody[cnt]); // analogWrite(PS, melody[cnt]); // PWM 값을 출력 delay(500); } |
728x90
'엔지니어링 > 임베디드' 카테고리의 다른 글
[아두이노 기초교육] I2C 통신을 이용한 LCD 제어 (0) | 2020.02.02 |
---|---|
[아두이노 기초교육] 7-세그먼트 제어 (0) | 2020.02.01 |
[아두이노 기초교육] RGB LED 제어 (0) | 2020.02.01 |
[아두이노 기초교육] LED 제어 (0) | 2020.01.16 |
[아두이노 기초교육] 아두이노 소개 (0) | 2020.01.15 |
댓글