ESP32 NTC測溫代碼,它包括了溫度補償和校準功能,可以提高測量精度
以下是一個更加完整的ESP32 NTC測溫代碼,它包括了溫度補償和校準功能,可以提高測量精度。
以下是一個更加完整的ESP32 NTC測溫代碼,它包括了溫度補償和校準功能,可以提高測量精度。
// 定義NTC電阻和電壓分壓電阻
#define R_NTC 10000.0
#define R_DIV 10000.0
// 定義NTC電阻的參考溫度和參考電阻值
#define NTC_REF_TEMP 25.0
#define NTC_REF_RES 10000.0
// 定義溫度補償參數(shù)
#define TEMP_COMP_BETA 3892.0
#define TEMP_COMP_REF_RES 10000.0
// 定義校準參數(shù)
#define CALIBRATION_OFFSET 0.0
#define CALIBRATION_SCALE 1.0
void setup() {
? Serial.begin(9600);
? while (!Serial) {
? ? delay(10);
? }
}
void loop() {
? // 讀取NTC電阻對應的電壓
? float voltage = analogRead(A0) * 3.3 / 4095.0;
? // 計算NTC電阻的電阻值
? float r_ntc = R_DIV * voltage / (3.3 - voltage);
? // 計算NTC電阻的溫度
? float beta = 3950.0; // NTC的Beta值
? float steinhart = r_ntc / R_NTC; // 計算Steinhart-Hart方程中的變量
? steinhart = log(steinhart);
? steinhart /= beta;
? steinhart += 1.0 / (NTC_REF_TEMP + 273.15);
? steinhart = 1.0 / steinhart;
? float ntc_temp = steinhart - 273.15; // 轉化為攝氏度
? // 溫度補償
? float temp_comp_res = TEMP_COMP_REF_RES * exp(-TEMP_COMP_BETA / (NTC_REF_TEMP + 273.15));
? float temp_comp_ratio = temp_comp_res / (temp_comp_res + r_ntc);
? ntc_temp = (ntc_temp - NTC_REF_TEMP) / (1.0 + temp_comp_ratio);
? // 校準
? ntc_temp = ntc_temp * CALIBRATION_SCALE + CALIBRATION_OFFSET;
? // 輸出溫度
? Serial.print("NTC temperature: ");
? Serial.print(ntc_temp);
? Serial.println(" °C");
? delay(1000);
}