ESP32 NTC测温代码
ESP32 NTC测温代码
以下是一个基于ESP32的NTC测温代码的示例:
以下是一个简单的ESP32 NTC测温代码示例:
#include <math.h>
const float R1 = 10000; // 电阻值
const float B = 3950; // B值
const float T0 = 298.15; // 温度参考值
const float R0 = 10000; // 温度参考值时的电阻值
void setup() {
Serial.begin(9600);
}
void loop() {
float reading = analogRead(A0); // 读取NTC电阻值
float voltage = reading * (5.0 / 1023.0); // 将读取值转换为电压值
float resistance = R1 * (5.0 / voltage - 1.0); // 计算NTC电阻值
float temperature = 1.0 / (1.0 / T0 + log(resistance / R0) / B) - 273.15; // 计算温度值
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
delay(1000);
}
这个代码示例与前面的示例非常相似,但有一些微小的更改。我们将NTC连接到ESP32的34号引脚。代码读取该引脚上的模拟值,并将其转换为电压值和电阻值。然后,使用Steinhart-Hart公式计算温度值,并将其以摄氏度为单位输出到串口监视器中。
请注意,此代码中的NTC电阻器是一个10K欧姆的电阻器。如果您使用不同值的电阻器,则需要相应地调整代码中的电阻值。