TMP421, JeeNode and below freezing temps

Interfacing hardware, sensors, output drivers, chips.
Post Reply
cinch123
Posts: 10
Joined: Sun Oct 31, 2010 1:33 pm

TMP421, JeeNode and below freezing temps

Post by cinch123 » Fri Dec 03, 2010 10:43 pm

Hello,

My TMP421 sensor running on the JeeNode worked great until the temps dropped below freezing. All of a sudden, the math to determine the temperature doesn't work! Here's the part of the sketch that pulls the bytes representing the temperature out of the TMP421:

DeviceI2C temperature (1, 0x2A); // I2C address of 0X2A

temperature.send(); // go into send mode
temperature.write(0x00); // high byte is on register 0
temperature.receive(); // grab it
temp_hi = temperature.read(1); // store it
temperature.stop(); // done

temperature.send(); // go into send mode
temperature.write(0x10); // low byte is on register 0x10
temperature.receive(); // grab it
temp_lo = temperature.read(1); // store it
temperature.stop(); // done

tempC = (float)temp_lo / 256; // make low byte into remainder
tempC = tempC + temp_hi; // add high byte to make total temp

(above was adapted from http://www.ka1kjz.com/?p=797)

So this is all fine and dandy until the temp drops below 0C. Once it does, I get values like 251 for temp_hi and 192 for temp_lo. Do the math and that's 251.75. With snow on the ground, I don't think that's likely.

Any idea how this should be adapted to handle below-freezing temps?

hjohnson
Posts: 2
Joined: Fri Dec 03, 2010 10:54 pm

Re: TMP421, JeeNode and below freezing temps

Post by hjohnson » Sat Dec 04, 2010 12:01 am

The registers in the TMP421 seem to use 2's compliment math on the high bytes to store and communicate temperatures. See http://focus.ti.com/lit/ds/sbos398b/sbos398b.pdf page 10.

Not sure how familiar you are with 2's compliment, but think of it this way: When the temperature goes below zero, the last bit of a normally positive number is flipped, and this bit signifies -256. So, your 251 in fact means -5. Somehow, this seems to be getting lost, maybe in the way that the ints are sent by the TMP421, or the way they are saved in the jee board.

Hack fix: Run this before adding the temp high and temp low. The temp low isn't 2's compliment or anything like that, so no problems there.
if(temp_high>127) { //int is unsigned when sent, so >127 means that the high bit is flipped, should be negative.
temp_high = (tmp_high-256); //Calculate the real number.
}

edit: after further review, the reason this messes up is that the code in the jee labs ports library uses an unsigned (positive only) 8-bit integer as the data type for I2C reading. Fair enough, this is needed for ASCII char, recognizable byes 0->256 instead of -128->127, etc. When the unsigned is cast as an integer, it remains unsigned.This is just something to watch out for when negative numbers are needed, as the conversion of numbers needs to be done manually.

This should fix it, but without a Jee Board, a temperature sensor, or a thermal chamber in front of me I can't know for sure. Also, there is probably a more elegant and robust solution. All I know is that with your numbers, (251-256)+(192/256)=-4.25, which is really reasonable if you say stuck it in a freezer.

Hope this has helped!

Harry

cinch123
Posts: 10
Joined: Sun Oct 31, 2010 1:33 pm

Re: TMP421, JeeNode and below freezing temps

Post by cinch123 » Sat Dec 04, 2010 7:51 pm

Your hack works flawlessly. Thank you so much!

hjohnson
Posts: 2
Joined: Fri Dec 03, 2010 10:54 pm

Re: TMP421, JeeNode and below freezing temps

Post by hjohnson » Sat Dec 04, 2010 11:32 pm

Glad I could help. Hope your project turns out well!

Post Reply