본문 바로가기

Embedded/Arduino

CH06 - 1)피에조(Piezo) 스피커

피에조 스피커

- 피에조 스피커는 전기적 신호를 소리로 변환해 주는 소자

- 전기신호가 압전소자의 기계적 떨림을 만들고 기계적 떨림이 소리가 되는 원리

- 전원과 그라운드로 연결됨(극성이 있음) : LED와 같이 플러스의 다리길이가 길다

- 왼쪽 그림의 스티커를 때면 오른쪽 그림과 같이 극성이 표시되어 있다.

 

스피커에 전원공급함

- 지지지직 거리기만 한다.

int myNum;
int buzzPin=8;

String msg ="Input your Number : ";
int delayTime=2000;

void setup(){
  Serial.begin(9600);
  pinMode(buzzPin,OUTPUT);
}

void loop(){
  Serial.print(msg);
  while(Serial.available()==0){
  }
  myNum = Serial.parseInt();
  Serial.println();
  if(myNum>10){
    digitalWrite(buzzPin,HIGH);
    delay(delayTime);
    digitalWrite(buzzPin,LOW);
  }
}

int potVal;
int buzzPin=8;
int potPin=A2;
String msg ="Input your Number : ";
int delayTime=100;

void setup(){
  Serial.begin(9600);
  pinMode(buzzPin,OUTPUT);
  pinMode(potPin,INPUT);
}

void loop(){
  
  potVal = analogRead(potPin);
  Serial.print(potVal);
  Serial.println();
  if(potVal>900){
    digitalWrite(buzzPin,HIGH);
    delay(delayTime);
    digitalWrite(buzzPin,LOW);
  }
}

int potVal;
int buzzPin=8;
int potPin=A2;

void setup(){
  Serial.begin(9600);
  pinMode(buzzPin,OUTPUT);
  pinMode(potPin,INPUT);
}

void loop(){
  
  potVal = analogRead(potPin);
  Serial.println(potVal);
  
  if(potVal>900){
    digitalWrite(buzzPin,HIGH);
  
    potVal = analogRead(potPin);
    Serial.println(potVal);
  }
  
  digitalWrite(buzzPin,LOW);
}

 

소리가 나기 시작

int buzzPin=8;
int delayTime1=1;
int delayTime2=2;

void setup(){
  pinMode(buzzPin,OUTPUT);

}

void loop(){
  for (int i=1; i<=100; i++){
    digitalWrite(buzzPin, HIGH);
    delay(delayTime1);
    digitalWrite(buzzPin, LOW);
    delay(delayTime1);
  }
  for (int i=1; i<=100; i++){
    digitalWrite(buzzPin, HIGH);
    delay(delayTime2);
    digitalWrite(buzzPin, LOW);
    delay(delayTime2);
  }
}

단순히 징~ 거림

int buzzPin=8;
int delayTime=1;

void setup(){
  pinMode(buzzPin,OUTPUT);

}

void loop(){
  digitalWrite(buzzPin, HIGH);
  delay(delayTime);
  digitalWrite(buzzPin, LOW);
  delay(delayTime);
}

이제 소리가 나도록 변경해보자

- delayTime을 변경해보면 소리가 달라진다

- 1000 -> 800 -> 500 ->200 -> 100으로 변경해보자

- 80 -> 50 -> 20  으로 변경할 수 있다.

- 소리가 다르게 나온다

 

int buzzPin=8;
int delayTime=500;

void setup(){
  pinMode(buzzPin,OUTPUT);

}

void loop(){
  digitalWrite(buzzPin, HIGH);
  delayMicroseconds(delayTime);
  digitalWrite(buzzPin, LOW);
  delayMicroseconds(delayTime);
}

가변저항을 사용하여 소리를 다르게 나타내 보자

int potVal;
int buzzPin=8;
int potPin=A2;
int toneVal;

void setup(){
  Serial.begin(9600);
  pinMode(buzzPin,OUTPUT);
  pinMode(potPin,INPUT);
}

void loop(){
  
  potVal = analogRead(potPin);
  toneVal=(9940./1023.)+potVal+60;
  digitalWrite(buzzPin, HIGH);
  delayMicroseconds(toneVal);
  digitalWrite(buzzPin, LOW);
  delayMicroseconds(toneVal);
}

도레미파솔라시도

int PIEZO_PIN = 8;  // 피에조 스피커에 연결된 디지털 핀 번호

// 음계 별 주파수를 저장한 변수 선언
int melody[] = {  262,  // 도, 주파수: 262Hz
                  294,  // 레, 294Hz
                  330,  // 미, 330Hz
                  349,  // 파, 349Hz
                  392,  // 솔, 392Hz
                  440,  // 라, 440Hz
                  494,  // 시, 494Hz
                  523   // 도, 523Hz
};

void setup()
{
  // "도레미파솔라시도"를 순차적으로 출력
  // 음의 길이가 500ms이며, 간격이 500ms가 되도록 지연시간을 줌
  tone(PIEZO_PIN, melody[0], 500); delay(500);
  tone(PIEZO_PIN, melody[1], 500); delay(500);
  tone(PIEZO_PIN, melody[2], 500); delay(500);
  tone(PIEZO_PIN, melody[3], 500); delay(500);
  tone(PIEZO_PIN, melody[4], 500); delay(500);
  tone(PIEZO_PIN, melody[5], 500); delay(500);
  tone(PIEZO_PIN, melody[6], 500); delay(500);
  tone(PIEZO_PIN, melody[7], 500); delay(500);
}

void loop() {
}

-for 문을 사용하여 단축가능

int PIEZO_PIN = 8;  // 피에조 스피커의 변수 선언

int melody[] = {  262,  // 도, 주파수: 262Hz
                  294,  // 레, 294Hz
                  330,  // 미, 330Hz
                  349,  // 파, 349Hz
                  392,  // 솔, 392Hz
                  440,  // 라, 440Hz
                  494,  // 시, 494Hz
                  523   // 도, 523Hz
};

void setup() {
  for (int i=0; i<8; i++) {           // for 반복문을 통해 melody 배열에 있는 주파수를 한번씩 호출
    tone(PIEZO_PIN, melody[i], 500);  // 8번 핀에 0.5초 동안 특정 주파수의 소리가 발생하도록 전압을 인가함
    delay(500);                      // 0.5초간 지연
  }
}

void loop() {
  
}

 

 

'Embedded > Arduino' 카테고리의 다른 글

01. Arduino 설치하기  (0) 2021.06.09
초음파센서+브라킷+서보모터  (0) 2021.05.26
Servo 제어  (0) 2020.11.21
CH08 -1) 푸시버튼 사용하기 - II  (0) 2020.10.23
CH07 -1) 푸시버튼으로 LED제어  (0) 2020.10.12