페이지

2026년 6월 19일 금요일

6.2.2. 주변 온도 측정하기





CODE 6.3 | 온도 센서로 주변 온도 측정하기
01int tempPin = A0; // 온도 센서를 연결한 아날로그 핀
02 
03void setup() {
04  Serial.begin(9600); // 시리얼 모니터 통신 시작
05}
06 
07void loop() {
08  int sensorValue = analogRead(tempPin); // 온도 센서 값 읽기
09 
10  // 센서 값(0~1023)을 전압 값으로 변환
11  float voltage = sensorValue * (5.0 / 1023.0);
12 
13  // TMP36 온도 변환 공식으로 섭씨온도 계산
14  float temperature = (voltage - 0.5) * 100;
15 
16  Serial.print("Temp: ");
17  Serial.print(temperature);
18  Serial.println(" C");
19 
20  delay(100); // 0.1초 대기 후 다시 측정
21}



 

댓글 없음:

댓글 쓰기