0
// Tells the Arduino to read A2 and stores the value in “Vin”    
Vin = analogRead (tempSensor); 

// Converts the voltage value to temperature and stores it into the “TC” variable (in °C)
TC = 500.0 * (Vin / 1024.0); 

The temperature shown is about 50.25°C, sometimes it is showing 36-38°C degrees, but the actual temperature is about 25-26°C.

Why this is happening?

Code:

Vin = analogRead (tempSensor);
TC=(500.0*Vin)/1023.0;
TF = ((9.0*TC)/5.0)+32.0;
lcd.setCursor(0, 1);
lcd.print(TF);
lcd.print((char)223);
lcd.print("F ");
lcd.print (TC);
lcd.print((char)223);
lcd.print("C");
delay(1000); 
5
  • 1
    What sensor are you using (part no.)?
    – Gerben
    Feb 12, 2015 at 10:22
  • LM35... @ peter
    – Rappy Saha
    Feb 12, 2015 at 10:28
  • They suggest a 0.01uF bypass capacitor in the datasheet.
    – Gerben
    Feb 12, 2015 at 16:29
  • i use it also but still there is no change in reading
    – Rappy Saha
    Feb 18, 2015 at 10:24
  • Next time please add code to your post instead of in a comment. I've edited your post for now. Thanks! Feb 19, 2015 at 22:53

4 Answers 4

2

Any chance you're printing Vin, and not TC?

500*(50.25/1024) = ~24.5

If not, please post as much of your program as possible

3
  • Vin = analogRead (tempSensor); TC=(500.0*Vin)/1023.0; TF = ((9.0*TC)/5.0)+32.0; lcd.setCursor(0, 1); lcd.print(TF); lcd.print((char)223); lcd.print("F "); lcd.print (TC); lcd.print((char)223); lcd.print("C"); delay(1000); here is the code
    – Rappy Saha
    Feb 18, 2015 at 10:56
  • @RappySaha Thanks, I don't see anything wrong with your code. One thought I had was that the lcd.print function accepts only char,byte,int,long, or string. I'm not sure how it will handle TC and TF, if they're of type 'double'. You might want to try casting them to ints lcd.print((int)TF); As others have said, you could have a bad sensor or a bad connection. Your math looks right, and your code looks alright.
    – krol
    Feb 18, 2015 at 19:38
  • C++ is supposed to do implicit casting but it can sometimes break, especially with warnings disabled on Arduino. I've had issues with Serial.print() specifically, so that might be the issue. Feb 19, 2015 at 22:54
1

I gave up on using a LM335, which is quite similar to your LM35. The problem is these devices self-heat due to the quiescent power they draw. The LM35 data sheet suggests you correct for this in software (footnote 5), and the LM335 datasheet dodges the bullet by quoting accuracy figures that can be achieved in an oil bath rather than free air.

But the figures you are quoting suggest you have a bigger problem, as I am talking of a few degrees inaccuracy, not tens of degrees.

I changed to a DS18B20 and never looked back - simple interface and self-heating is compensated for by the device.

3
  • if i use ds18b20 is the formula will still be same??
    – Rappy Saha
    Feb 18, 2015 at 11:26
  • There is no formula. The library returns the temperature
    – kiwiron
    Feb 19, 2015 at 18:08
  • @RappySaha more specifically the DS18B20 is a digital sensor. Depending on the library used (or not, I haven't tried them all) you can call a function to return the temperature as a float, representing the temperature in degrees Celsius or Farenheit, or as a int16_t with each single bit representing 1/128th of a degree Celsius. No extra maths is needed. Feb 3, 2016 at 19:02
0

It sounds like your voltage to temperature calculation is wrong. Assuming the temperature voltage relationship is linear you can work out the formula. Take two measurements at two known temperatures (quite far apart, perhaps outdoors and in) and note down the value Arduino measures for Vin. Lets call these VinA & VinB for TempA and TempB.

You can calculate measures per degree with...

MeasuresPerDeg = (TempB - TempA) /(VinB - VinA)

And the a constant as...

Constant = TempA - VinA*MeasuresPerDeg

Your equation to calculate temperature should now be

TC=MeasuresPerDeg*(Vin/1024.0) + Constant;

3
  • 1
    According to the datasheet, the LM35 is supposed to output 10mV per degree C. Assuming a standard 5V AREF, the formula in the question looks correct (in theory anyway). Feb 12, 2015 at 14:29
  • You're quite right. Could you post the rest of your code? Feb 12, 2015 at 17:39
  • I'm not the questioner. :) Feb 12, 2015 at 17:55
0

What are the variable types of TC and TF - int/float/double? Something else?

If they're integers, you may be losing some numerical precision in the calculation.

When you get 50.25°C - have you checked the Vin is correct?

Also when you write the values out, the serial.print() can take the number of decimal points as a second argument.

// Print to three decimal places
Serial.print("TF=");
Serial.println((float)TF, 3);
Serial.print("TC=");
Serial.println((float)TC, 3);
Serial.print("Vin=");
Serial.println(Vin, DEC);
2
  • "serial.print() needs the number of decimal points" : nope, a default of '2' is included within the function prototype, therefore you can skip the second parameter. The code wouldn't compile if it was needed but omitted. Feb 3, 2016 at 19:04
  • @CharlieHanson - Yep! Thanks I have corrected my answer.
    – Kingsley
    Feb 3, 2016 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.