We are using an LM35 temperature sensor with the Arduino board pin A0 to A7, any one pin of them really. The problem is that I can not get the steady and accurate value in the serial window in the Arduino software. Below is the code I am using:
int pin = 0; // analog pin
int tempc = 0, tempf = 0; // Temperature variables
int samples[8]; // Variables to make a better precision
int maxi = -100, mini = 100; // To start max/min temperature
int i;
void setup()
{
Serial.begin(9600); // Start serial communication
}
void loop()
{
for(i = 0; i <= 7; i++) { // Gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);
}
tempc = tempc/8.0; // Better precision
tempf = (tempc * 9)/ 5 + 32; // Converts to fahrenheit
if (tempc > maxi) {
maxi = tempc;
} // Set max temperature
if (tempc < mini) {
mini = tempc;
} // Set min temperature
Serial.print(tempc,DEC);
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");
tempc = 0;
delay(1000); // Delay before loop
}