使用LM35模组进行温度传感时,忘记设置参考电压,默认使用当前Arduino工作电压作为参考电压。但是,当用不稳定的外部电源供电时,输出值就不准确了。这时,最好使用内部参考电压。看下面的程序:
int LM35 = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
analogReference(INTERNAL); //调用板载1.1V基准源
//analogReference(DEFAULT); //调用当前工作电压基准源
}
void loop() {
// put your main code here, to run repeatedly:
int n = analogRead(LM35); //读取A0口的电压值
float temp = (1.1 * n * 100.0) / 1024;//初始化时,必须指明电压基准源:analogReference(INTERNAL); //调用板载1.1V基准源
//float temp = (5.0 * n * 100.0) / 1024;
Serial.print("temperature ");
Serial.print(temp);
Serial.println("C");
delay(2000);
}
这样就能比较准确地得到传感器的数值。