//30327簡銘賢
//NTC熱敏電阻
//2023.9.26
#include <Arduino.h>
// 定義NTC熱敏電阻的接腳
const int ntcPin = 15; // 連接到ESP32的模擬輸入D15
// NTC的參數
const float ntcResistance = 10000.0; // NTC的標稱電阻值 (10kΩ)
const float nominalTemperature = 25.0; // NTC的標稱溫度 (25°C)
const float betaValue = 3950.0; // NTC的Beta值 (通常為3950)
void setup() {
Serial.begin(115200); // 初始化串口通信
}
void loop() {
// 讀取NTC電阻值
int rawValue = analogRead(ntcPin);
// 計算NTC的電阻值
float resistance = ntcResistance / (4095.0 / rawValue - 1.0);
// 計算溫度(攝氏度)
float steinhart = resistance / ntcResistance; // 計算Steinhart-Hart公式的中間值
steinhart = log(steinhart); // 自然對數
steinhart /= betaValue; // 除以Beta值
steinhart += 1.0 / (nominalTemperature + 273.15); // 加上(1 / T0)
steinhart = 1.0 / steinhart; // 取倒數,得到溫度(Kelvin)
steinhart -= 273.15; // 轉換為攝氏度
// 顯示溫度
Serial.print("NTC電阻值: ");
Serial.print(resistance);
Serial.print(" ohms, 溫度: ");
Serial.print(steinhart);
Serial.println(" °C");
delay(100); // 延遲1秒再次讀取
}