페이지

2026년 6월 19일 금요일

6.2.3. 폭염 알림 경보기 만들기





CODE 6.4 | 온도 센서로 폭염 알림 경보기 만들기
01int tempPin = A0; // 온도 센서를 연결한 아날로그 핀
02int buzzerPin = 8; // 피에조 부저를 연결한 디지털 핀
03 
04void setup() {
05  Serial.begin(9600); // 시리얼 모니터 통신 시작
06  pinMode(buzzerPin, OUTPUT); // 부저 핀을 출력 모드로 설정
07}
08 
09void loop() {
10  int sensorValue = analogRead(tempPin); // 온도 센서 값 읽기
11 
12  // 센서 값(0~1023)을 전압 값으로 변환
13  float voltage = sensorValue * (5.0 / 1023.0);
14 
15  // TMP36 온도 변환 공식으로 섭씨온도 계산
16  float temperature = (voltage - 0.5) * 100;
17 
18  Serial.print("Current Temp: ");
19  Serial.print(temperature);
20  Serial.println(" C");
21 
22  // 온도가 30℃ 이상이면 경고음 발생
23  if (temperature >= 30.0) {
24    tone(buzzerPin, 1000); // 1000Hz 소리 발생
25  }
26  // 온도가 30℃ 미만이면 소리 끄기
27  else {
28    noTone(buzzerPin); // 소리 끄기
29  }
30 
31  delay(500); // 0.5초 대기 후 다시 측정
32}




 

댓글 없음:

댓글 쓰기