LM35 Sensor not consistent

I set it up using This code:

int val;
int tempPin = 1;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000; 
  float cel = mv/10;
  float farh = (cel*9)/5 + 32;

  Serial.print("TEMPRATURE = ");
  Serial.print(cel);
  Serial.print("*C");
  Serial.println();
  delay(1000);

and this configuration:

However i get this inconsistent output:

When i put it in cold and hot places the output does go higher and lower, however i carnt understand why it does not maintain the same temperature???

Try adding a few capacitors. 0.1uf across the the arduion 5V and ground, and across the LM35 power leads (5V and ground).
On the output pin of the LM35, put a 68 ohm resistor in series to a 1uf capacitor, to ground.
That usually works wonders.

With those large variations, I suspect a supply problem.

9volt PP3 battery?

Try this. It uses the 1.1volt reference voltage.
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(A1); // one unused reading to clear old sh#t
  for (int x = 0; x < 100; x++) { // 100 readings for averaging
    total = total + analogRead(A1); // add each value
  }
  // convert value to temp
  tempC = total * Aref / 1024; // value to temp conversion
  tempF = tempC * 1.8 + 32; // Celcius to Fahrenheit conversion
  total = 0; // reset value
  // print to serial monitor
  Serial.print("The temperature is  ");
  Serial.print(tempC, 1); // one decimal place
  Serial.print(" Celcius  ");
  Serial.print(tempF, 0); // no decimal places
  Serial.println(" Fahrenheit");
  delay(1000); // slows readings
}

That then gives this result ???

"That then gives this result "

Please be a little bit more specific. I don't know what that means. Was that from adding caps, or what?

LeonWilberforce:
That then gives this result ???

Did you read te sketch?

Did you set the serial monitor to the correct speed?

You can also change the code to 9600.
Leo..

I experienced fluctuations +-10 degrees using an LM335Z sensor but could not use an internal reference voltage. Even with a cap to smooth things out didn't help.

The strange characters would indicate that you have a different baud rate set in your code than what's selected in the Serial monitor.

I ended up using a DHT22 sensor which is giving me very consistent readings, even with many devices plugged into my circuit. I also did not need to calibrate it.

Another good sensor is the Dallas DS18B20 or the TMP36.

OP has the LM35, not the LM335Z zener version.

The LM35, with the right code, should be stable.
Leo..

LM35 is a good product that works very well.
This is an analog sensor and like any analog products it requires a good ground.

It delivers 10 mV / ° C, it is very little. You must be careful not to noisy the wires (data and gnd). A good idea is to twist together data and gnd wires.
You must also use the internal reference of the ADC.
With the default setting, analog_reference is Vcc(5V), step is 5 mV or 0.5 ° C. Do not forget that the analog-digital converter can not give a far more accurate than +/- 3 or 5 LSB , ( +/- 1.5 ° C ) --> read Atmel datasheet.
If you use the internal reference, measurement step will not be divided by 5 which will give a total accuracy of +/- 0.3 ° C.

Please note : value of the internal reference may range between 1.0V and 1.2V depending on the batch (see Atmel datasheet). It does not matter because you can measure it:
When the internal reference is selectioned it is possible to measure his voltage on the terminal Aref.
A good idea is to write this value in EEPROM for future use.

Another point Arduino boards are poorly designed for analog measurements.
If you have a UNO card you can greatly improve the board by welding a 100 nF capacitor to the back of the card between the pin and gnd Aref ON THE MICRO SOCKET.

I join a picture, I use SMD 0805 capacitor but "classic" capacitor can works : Warning wires length should be short.

Aref_Uno.png

68tjs:
Another point Arduino boards are poorly designed for analog measurements.
If you have a UNO card you can greatly improve the board by welding a 100 nF capacitor to the back of the card between the pin and gnd Aref ON THE MICRO SOCKET.

Can you explain "greatly improved".
Aref is already decoupled with a 100n cap. C4 on the schematic.
It's near the Aref pin.
Leo..

The addition of a capacitor, say 0.1 uF, from the analog pin to ground is a good one. I had some current measuring circuitry hooked to two analog pins on a Nano, and got wildly varying results. No software trick would clean up the problem, but the capacitors did. I still don't know why, but it worked.

Aref is already decoupled with a 100n cap. C4 on the schematic.

We don't speak about same Aref.
You speak about Aref of the board
I speak about Aref pin of the microcontroler package.

Indeed there is a capacitor at the input of the board. This capacitor protects against external disturbances .
The problem is that aref track crosses diagonally on the board, lengh is about 10 cm.
During these 10 cm, track is noisy by the other tracks of the board, it is an internal disturbance,
A capacitor at each end of the track is needed.
In the analog domain, track is not a simple wire. It is a distributed impedance: resistor, inductor, capacitor.
Tracks are coupled together as like a transformative effect (in fact this is absolutly not a transformative effect but it's like ..)
This explain for what reasons C4 protects against external disturbances but not against internal disturbances near the microcontroler : there is inductor between the two Aref.
This is absolutely not a DC problem.

Can you explain "greatly improved".

Before I have fluctuation due to the noise : about +/- 5 LSB depending of external environment.
After I have only one LSB.