This code uses many samples and the internal reference voltage for higher accuracy.
LM35 connected to A1, +5volt, and ground.
Leo..
// LM35 temp sensor connected to A1
// ~2 to ~102 degrees C
//
float Aref = 1.063; // calibration, change this to the actual Aref voltage of ---YOUR--- Arduino
unsigned long total; // A/D output
float tempC; // Celcius
float tempF; // Fahrenheit
//
void setup() {
analogReference(INTERNAL); // use the internal ~1.1volt reference, change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(115200); // ---set serial monitor to this value---
}
//
void loop() {
analogRead(1); // one unused reading to clear old sh#t
for (int x = 0; x < 100; x++) { // 100 readings for averaging
total = total + analogRead(1); // add each value
}
// convert value to temp
tempC = total * Aref / 1024; // value to temp conversion
tempF = tempC * 1.8 + 32; // Celcius to Fahrenheit conversion
// print to serial monitor
Serial.print("The temperature is ");
Serial.print(tempC); // default two decimal places
Serial.print(" Celcius ");
Serial.print(tempF, 1); // one decimal place
Serial.println(" Fahrenheit");
delay(1000); // slows readings
total = 0; // reset value
}