本条只提供代码,元器件连接方式可参考其他博客
#include<math.h>
//只用调用math库
void setup() {
Serial.begin(9600);
pinMode(A0,INPUT);
}
void loop() {
//模拟输入
int sensorValue=analogRead(A0);
//得到电压
float volt=sensorValue/4095.0*3.3;
//得到ntc阻值
float res=10*(3.3-volt)/volt;
Serial.print(volt);
Serial.print("V ");
Serial.print(res);
Serial.print("k ");
Serial.print(GetTemperature(res));
Serial.print("C");
Serial.print("\n");
delay(1000);
}
/ /定义数组,里面是0~100度时,热敏电阻的阻值
const float ntc_B3950_10k[]{
32.6142,
30.9966,29.4688,28.0521,26.6606,25.3704,
24.1501,22.9955,21.9028,20.8682,19.8884,
18.6509,17.7941,16.9815,16.2106,15.4790,
14,9945,14.3191,13.6779,13.0690,12.4905,
11.9409,11.4184,10.9217,10.4494,10.0000,
9.5724,9.1654,8.7779,8.4089,8.0574,
7.7225,7.4033,7.0990,6.8088,6.5321,
6.2680,6.0160,5.7755,5.5459,5.3266,
5.1171,4.9169,4.7257,4.5428,4.3680,
4.2008,4.0409,3.8878,3.7414,3.6012,
3.4670,3.3385,3.2154,3.0975,2.9845,
2.8762,2.7723,2.6727,2.5772,2.4856,
2.3977,2.3134,2.2324,2.1547,2.0801,
2.0084,1.9396,1.8734,1.8098,1.7487,
1.6900,1.6335,1.5792,1.5269,1.4767,
1.4283,1.3818,1.3369,1.2938,1.2523,
1.2123,1.1737,1.1366,1.1008,1.0663,
1.0331,1.0011,0.9702,0.9404,0.9116,
0.8839,0.8572,0.8314,0.8064,0.7824,
0.7592,0.7367,0.7151,0.6942,0.6739
};
//得到温度
int GetTemperature(float res){
int i;
if((res>ntc_B3950_10k[0])||(res<ntc_B3950_10k[100])){
Serial.print("温度超出测量范围");
return -1;
}
for(i=0;i<=100;i++){
if((res<ntc_B3950_10k[i])&&(res>ntc_B3950_10k[i+1])){
return i;
}
}
}