arduino-esp32-c3对MF53-103F-3950-800L温度传感器的温度读取代码参考
时间: 2023-09-30 20:10:56 浏览: 181
以下是一份arduino-esp32-c3对MF53-103F-3950-800L温度传感器的温度读取代码参考:
```
#include <analogWrite.h>
#define THERMISTORPIN 34
#define SAMPLING_INTERVAL 500 //milliseconds
#define NO_OF_SAMPLES 10
int samples[NO_OF_SAMPLES];
int sampleIndex = 0;
//thermistor output curve for temperature calculation
float Thermistor(int RawADC) {
float logR, R, T;
float R_25 = 10000.0; // resistance at 25 degrees Celsius
float B = 3950; //beta value
logR = log(10000.0 * ((1024.0 / RawADC - 1)));
R = 1.0 / (0.0002242 + (0.000000101 * logR) + (0.00000000000429 * logR * logR * logR)); //resistance of thermistor
T = (1.0 / ((log(R / R_25) / B) + (1.0 / (25.0 + 273.15)))) - 273.15; //temperature in Celsius
return T;
}
void setup() {
pinMode(THERMISTORPIN,INPUT);
Serial.begin(115200);
}
void loop() {
static unsigned long lastSamplingTime = 0;
//take samples at periodic intervals
if (millis() - lastSamplingTime > SAMPLING_INTERVAL) {
lastSamplingTime = millis();
samples[sampleIndex] = analogRead(THERMISTORPIN);
sampleIndex++;
if (sampleIndex >= NO_OF_SAMPLES)
sampleIndex = 0;
}
//calculate average sample value
long average = 0;
for (int i = 0; i < NO_OF_SAMPLES; i++)
average += samples[i];
average /= NO_OF_SAMPLES;
//calculate temperature from average sample value
float temperature = Thermistor(average);
//print temperature
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degrees Celsius");
}
```
这份代码的原理是通过使用Arduino的analogRead函数读取温度传感器的电阻值,然后将其转化为温度值。MF53-103F-3950-800L是一种热敏电阻器,其特定的电阻值和环境温度成正比,因此通过读取电阻值可以计算出当前温度。具体计算过程可以参考代码中的Thermistor函数。这份代码采用了对一段时间内的多个采样值进行平均的方式,以提高读取温度的准确性。
阅读全文