TMP36 sensor shows a temperature higher than real temp.

Hi,
I´m using a TMP36 sensor to obtain the temperature but I get a higher temperature than real, 7 or 8 degrees more.

This is the code I´m using:

void loop(){
int sensor;
float volts, temp;

sensor = analogRead(sensorPin);
volts = (sensor / 1024.0) * 5.0;
temp = (volts - .5) * 100;
delay(1000);
}

Thanks...

Do you power the Arduino with the usb connector ?
In that case the 5V could be 4.8V.
The lower the 5.0V is, the higher temperature the Arduino is reading, since the 5V is used as a reference.

Yes, I power with an usb connector.
I have put 4.8 instead of 5 and the temperature is lower than before.
This was the mistake.

Thanks, regards.

Use either a power supply to the DC jack of the Arduino board, or use a digital temperature sensor.

The DS18B20 is very accurate and cheap.
It needs a library to make it run, and it is slow, but you can be sure that the temperature is accurate.

Ok, thanks...

Okay this is interesting.

There may be a problem if the Arduino is used on a test bench with a USB cable and then powered in some other way as a mobile device. Is there a way to capture the input voltage and use this as a variable to ensure that the temperature readings are accurate?

Yes, but there is more to it.

The Arduino can use the internal voltage reference, independent of the power supply. That solves the problem.

It is possible to measure its own 5V, by testing it against the internal voltage reference. The internal mux can be switch in a way to make this possible. Search for : Arduino ReadVcc
Like this:
http://code.google.com/p/tinkerit/wiki/SecretVoltmeter

However, sometimes the variation of the 5V is needed for more accuracy.
Suppose a resistor and LDR are used to measure the amount of light. Since they use the 5V, also the Arduino should use the 5V as reference. If the 5V changes to 4.8V, they both change according that and the result is a steady measurement.

When sensor outputs a certain specific voltage (for example 2.0V for 20 degrees), the Arduino should use a fixed voltage as reference. That is why the internal voltage reference is better in such cases.