Problem with temperature sensor reference 52AB LM35DZ

Hello I have a problem with a temperature sensor.The reference is 52AB LM35DZ.I want to see the ambient temperature value in celsius in the serial monitor.The problem is that the values variation is weird,indeed it print 20.40 °C,0.00 °C,26.00 °C,0.00 °C,26.40 °C,0.00 °C,26.80 °C,0.00 °C,27.60°C,21.20 °C,0.00°C.I attach the sketch.Thank you in advance.

temperaturesensor.ino (360 Bytes)

The definition for analogRead() says that it returns an int and you're assigning it to a float, without casting it.

You probably need to do this:

temp = (float)analogRead(A1);

Please use code tags, so we all can see it without having to download/save it first.
Read the "How to post" sticky.

temp= temp*(5.0*1000.0/1024.0);

Reading an LM35 with default Aref and a 10-bit A/D gives you whole degrees at best (no meaningful decimal places).
And a temp display that depends on the supply voltage of the Arduino.

Switch to the internal 1.1volt Aref, and your display will be more stable, with a 'real' resolution of ~0.1C.
Leo..

float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref
}

void loop() {
  tempC = analogRead(A1) * 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"

  Serial.print("Temperature: ");
  Serial.print(tempC, 1); // one decimal place is all you get
  Serial.println(" °C");

  delay(1000); // use a non-blocking delay when combined with other code
}

Something you may wish to try:

float temp;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  temp = analogRead(A1);
  temp= temp*(5.0*1000.0/1024.0);
  temp= temp/10;
  Serial.print("Temperature : ");
  Serial.print(temp);
  Serial.write(176);
  //Serial.print(char(176)); // May also be used. 
  Serial.print("C ");
  Serial.println();
  delay(1500);
  
}

Ron

MK1888:
The definition for analogRead() says that it returns an int and you're assigning it to a float, without casting it.

You probably need to do this:

temp = (float)analogRead(A1);

I change it but the value range still strange:
emperature :0.00 °C
Temperature :0.00 °C
Temperature :34.18 °C
Temperature :32.71 °C
Temperature :0.00 °C
Temperature :0.00 °C
Temperature :34.67 °C
Temperature :32.23 °C
Temperature :0.00 °C
Temperature :0.00 °C

Wawa:
Please use code tags, so we all can see it without having to download/save it first.
Read the "How to post" sticky.

temp= temp*(5.0*1000.0/1024.0);

Reading an LM35 with default Aref and a 10-bit A/D gives you whole degrees at best (no meaningful decimal places).
And a temp display that depends on the supply voltage of the Arduino.

Switch to the internal 1.1volt Aref, and your display will be more stable, with a 'real' resolution of ~0.1C.
Leo..

float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref
}

void loop() {
  tempC = analogRead(A1) * 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"

Serial.print("Temperature: ");
  Serial.print(tempC, 1); // one decimal place is all you get
  Serial.println(" °C");

delay(1000); // use a non-blocking delay when combined with other code
}

I change it but the value variation is still strange:
Temp.5 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 34.1 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.4 °C
Temperature: 35.3 °C
Temperature: 15.8 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 34.1 °C
Temperature: 35.5 °C
Temperature: 20.9 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 33.6 °C
Temperature: 35.7 °C
Temperature: 18.9 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.3 °C
Temperature: 34.7 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 36.5 °C
Temperature: 28.6 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 29.6 °C
Temperature: 35.8 °C
Temperature: 18.3 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.2 °C
Temperature: 35.3 °C
Temperature: 15.6 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 33.5 °C
Temperature: 35.1 °C
Temperature: 24.1 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 3.0 °C
Temperature: 36.3 °C
Temperature: 31.2 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.6 °C
Temperature: 31.8 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 13.1 °C
Temperature: 35.8 °C
Temperature: 20.3 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.6 °C
Temperature: 30.1 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 19.6 °C
Temperature: 35.6 °C
Temperature: 24.8 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 19.3 °C
Temperature: 36.4 °C
Temperature: 27.1 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 9.5 °C
Temperature: 35.6 °C
Temperature: 29.2 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 35.5 °C
Temperature: 34.7 °C
Temperature: 3.8 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 28.4 °C
Temperature: 35.5 °C
Temperature: 30.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C
Temperature: 0.0 °C

Ron_Blain:
Something you may wish to try:

float temp;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
 
  temp = analogRead(A1);
  temp= temp*(5.0*1000.0/1024.0);
  temp= temp/10;
  Serial.print("Temperature : ");
  Serial.print(temp);
  Serial.write(176);
  //Serial.print(char(176)); // May also be used.
  Serial.print("C ");
  Serial.println();
  delay(1500);
 
}




Ron

Hello I change it but the variation is still unstable:
Temperature : 0.00⸮C
Temperature : 21.00⸮C
Temperature : 0.00⸮C
Temperature : 36.62⸮C
Temperature : 0.00⸮C
Temperature : 0.00⸮C
Temperature : 35.64⸮C
Temperature : 0.00⸮C
Temperature : 0.00⸮C
Temperature : 35.16⸮C
Temperature : 0.00⸮C
Temperature : 0.00⸮C
Temperature : 35.16⸮C

Post a 'real' picture of the setup.
Leo..