Go Down

Topic: Help with TMP37 temperature sensor (Read 1 time) previous topic - next topic

HaLo2FrEeEk

Jan 31, 2013, 03:47 pm Last Edit: Jan 31, 2013, 07:34 pm by HaLo2FrEeEk Reason: 1
I recently received a few free samples of this device from analog.com because I wanted to try to modify my old analog thermostat and make it digitally controlled. The device has 3 pins: Vs, out and GND, and it operates between 2.7 and 5.5V. Here's the datasheet:

http://www.analog.com/static/imported-files/data_sheets/TMP35_36_37.pdf

The 37 in particular reads in 20mV/°C, is designed for the 5°-100°C range and has a reading of 500mV at 25°C. Initially I simply plugged in 5V to the Vs, GND to the ground pin and the out pin to A0 on the Arduino, then I read the analog input, adjusted it to convert the 0-1023 analog reading to a mV value like this:

Code: [Select]
int reading = analogRead(A0);
float mv = (reading * 5.0) / 1024.0;


Then subtracted 500 and divided by 20 to get degrees C. This is where I'm not sure I'm doing it right. Basically, I don't believe what the sensor is saying the temperature is in my room. Right now it's saying it's 77.07° F (25.04° C). I know that it's hotter than that (it gets really hot in my room). Here's my current sketch. I'm using 3.3V aref and printing to an LCD.

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
#define aref_voltage 3.3

int tempPin = 1;
int tempReading;

void setup(void) {
 analogReference(EXTERNAL);
 lcd.begin(16,2);
 lcd.clear();
 lcd.setCursor(0,0);
}

void loop(void) {
 tempReading = analogRead(tempPin);
 float voltage = tempReading * aref_voltage;
 voltage /= 1024.0;

 float temperatureC = (voltage - 0.5) * 200 ;
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
 lcd.clear();
 lcd.print(temperatureC);
 lcd.print(" C");
 lcd.setCursor(0, 1);
 lcd.print(temperatureF);
 lcd.print(" F");
 delay(1000);
}


The article I was reading to help me figure out how to set this thing up was using a TMP36, which is slightly different than the 37. From the datasheet:

Quote
The TMP36 is specified from ?40°C to +125°C, provides a 750 mV output at 25°C, and operates to 125°C from a single 2.7 V supply. [...] Both the TMP35 and TMP36 have an output scale factor of 10 mV/°C.


Quote
The TMP37 is intended for applications over the range of 5°C to 100°C and provides an output scale factor of 20 mV/°C. The TMP37 provides a 500 mV output at 25°C.


The difference I see is that the 36 reads 500mV at 0°C whereas the 37 reads 500mV at 25°C. My tiny tired brain can't work out what I'm supposed to do to account for this.

I'm hoping someone here who is less tired and has a bigger brain can help me figured the math out for this.

robtillaart


Quote
int reading = analogRead(A0);
float mv = (reading * 5.0) / 1024.0;

this code results in volts...

try this
Code: [Select]

int raw = analogRead(tempPin);
float milliVolts = raw * 4.887586;        // faster than * 5.0/1023      ==> 0 .. 5000
float tempC = (milliVolts - 500) * 0.05;  // faster than / 20            ==> -500 .. 4500 ==> / 20 ==> -25 .. +225
float tempH = tempC * 1.8 + 32;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

HaLo2FrEeEk

That still doesn't explain why my readings seem to be off. Applying your math to an example analog value from the sensor of 200:

200 * 4.887586 = 977.52
(977.52 - 500) * 0.05 = 23.88 (°C)
23.88 * 1.8 + 32 = 74.98 (°F)

And my math:

200 * (5000/1024) = 976.56
(976.56 - 500) / 20 = 23.83 (°C)
(23.83 * 9 / 5) + 32 = 74.89 (°F)

The difference is .05°C (and I did my math above assuming I was using 5V as the Vs, I'm actually using 3.3V with aref so it's more precise and less noisy). I don't think the way I'm doing the math itself is wrong, I'm just wondering why the number coming out seems wrong. Right now it says it's 72.43°F in my room, but it feels a lot hotter to me. I have no way of verifying the actual temperature in the room.

robtillaart


Your math is sound, the difference of 0.05 is because of rounding errors you have with floats. The 0.05 is probably within the limits of the accuracy of the sensor.
Note that the Arduino float only has 7 digits of precision, and every float operation adds rounding errors.

long has 9 digits of precision, and is faster

But part of the temperature you "experience/feel" has to do with humidity.
A high temperature with low humidity feels very different from same temp with high humidity.

Time for the next sensor (one that reads rH)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

HaLo2FrEeEk

I just can't believe that humidity is making up that much of a difference. Right now the device is reading 55.03°F. There is no possible way it's 55° in my room, the rest of the house is much cooler and the AC (in the living room) is set to turn on at 75°. I unplugged and replugged the Arduino from USB and it's now reading 53.87°F. I also removed the sensor from the breadboard and put in another one (I have two of the same kind), then unplugged and replugged the Arduino and it started at 65° and dropped down (quickly at first, then slower) to around 56°. It is not that cold in here, it never gets that cold in here, or anywhere else in the house for that matter, I hate cold.

Go Up