Tmp102 + Arduino = Crazy readings?

I’ve been tearing my hair out with this sensor for days now.

I’m using an Arduino compatible controller from Rugged Circuits called a Yellowjacket. It has a ATmega328P chip.

I have checked and double checked my wiring. I have checked my supply voltage on VCC and GND (3.27v). I have grounded the ADO pin so that the I2C address is 72.

I can get readings from the sensor just fine - the I2C seems to be working but the readings themselves are crazy. Well above boiling water temps. They do fluctuate - seemingly inversely to temperature?!

I understand there is a bit of code necessary to make these operate and I have used working code from other people which I will paste below. Even just reading the first byte returned and converting it to an int (which should give me the int portion of the response) gives me crazy high numbers.

In other words: I don’t think this is a programming issue. I feel like maybe my sensor is defective? Or maybe there is some I2C setting that I don’t understand that is causing me to get crazy results?

Any help would be greatly appreciated. I’m new to all of this. My simple analog sensor is working just fine but this one seems to be fubar.

Here’s my code (snippet):

#define TMP102_I2C_ADDRESS      72  //used when ADO pin is grounded - i2c device address

void setup(){
  // Enable Serial output 
  Serial.begin(9600);
  Wire.begin();
}

void loop(){
   getTemp102();
  delay(26);
}

float getTemp102()
{
  byte firstbyte, secondbyte; //these are the bytes we read from the TMP102 temperature registers
  int val; /* an int is capable of storing two bytes, this is where we "chuck" the two bytes together. */
  float convertedtemp; /* We then need to multiply our two bytes by a scaling factor, mentioned in the datasheet. */
 
  /* Reset the register pointer (by default it is ready to read temperatures)
  You can alter it to a writeable register and alter some of the configuration -
  the sensor is capable of alerting you if the temperature is above or below a specified threshold. */ 
  Wire.beginTransmission(TMP102_I2C_ADDRESS); //Say hi to the sensor.
  Wire.send(0x00);  //temperature read function
  Wire.endTransmission();
  Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
  Wire.endTransmission();
  int bytesavailable = Wire.available();

  Serial.print("bytes available:\t");
  Serial.println(bytesavailable); 
 
  firstbyte      = (Wire.receive());
  /*read the TMP102 datasheet - here we read one byte from
  each of the temperature registers on the TMP102*/
  secondbyte     = (Wire.receive());
  /*The first byte contains the most significant bits, and
  the second the less significant */
  //val = ((firstbyte) << 4);  
  val = ((firstbyte) << 8);  
  /* MSB */
  val = val | secondbyte;
  val = val >> 4;
  
  //val |= (secondbyte >> 4);    
  /* LSB is ORed into the second 4 bits of our byte.
  Bitwise maths is a bit funky, but there's a good tutorial on the playground*/
  //convertedtemp = val*0.0625;
  convertedtemp = val*0.1125 + 32;
  /* See the above note on overreading */
 
  Serial.print("firstbyte is ");
  Serial.print("\t");
  Serial.println(firstbyte, BIN);
  Serial.print("secondbyte is ");
  Serial.print("\t");
  Serial.println(secondbyte, BIN);
  Serial.print("Concatenated byte is ");
  Serial.print("\t");
  Serial.println(val, BIN);
  Serial.print("Converted temp is ");
  Serial.print("\t");
  Serial.println(convertedtemp);
  return convertedtemp;
}

Perhaps I should add that I’ve mucked around with the code in a few places to try different things (thus the various commented bit shifting and etc) but I don’t think any of that is the problem. The first byte being returned always looks like “11110111” or similarly insanely high values.

I’ve checked the configuration registers and it’s setup properly. I don’t know what else to try? I’m new to electronics - are bad sensors possible? Par for the course? Should I just contact support about a replacement sensor?

What I don’t get is that the sensor does seem to be reacting to changes in temperature - the super high value will drop slightly when I blow a hairdryer across it - down from say ~420 F to maybe 350 F. I don’t get it.

Here are a few links for anyone who might be interested in taking a look?

The TMP102: http://www.sparkfun.com/products/9418

The Yellowjacket “Arduino”: http://ruggedcircuits.com/html/yellowjacket.html - This is a clone of the async labs version.

quechua:
The first byte being returned always looks like “11110111” or similarly insanely high values.

That looks more like an insanely low (temperature) value. See Table 5 in the datasheet. I see you send the MSB and LSB as well as the combined temp out. Can you collect a few 10’s samples and post them. Perhaps 10 or so at room temp and the same with an ice cube in a baggie sitting on it. If you’re feeling frisky, boil some water with a spoon in it. Then when the spoon is nice and hot, take it out and slap it on the sensor and take another 10 or so readings.

http://www.sparkfun.com/datasheets/Sens … tmp102.pdf

Perhaps I’m confused.

My understanding is that the sensor should be giving me 2 bytes back on I2C in response to a temp request. It does appear to be doing this. I check the bytes available after the request, it reads as 2. The sensor returns MSB first, followed by LSB.

Then, as you say, I print out the MSB and LSB and then the final temp after calculations. The 1st byte returned (MSB) is always reading high. The example above converts to 123 in decimal. My understanding is that the MSB represents the whole number portion of the result. 4 bits of the LSB represent the decimal portion of the result. So the sensor is telling me that it’s at least 123 degrees celsius in my room (ambient temp ~72 F).

So the fact that the first byte is always coming back above 100 or so makes me think something is very wrong.

I’m not sure how the value above could appear very low to you? Please let me know if I’m confused.

I will try to get some readings tonight and see how it behaves.

Thanks for the reply.

quechua:
Perhaps I’m confused.

The 1st byte returned (MSB) is always reading high. The example above converts to 123 in decimal.

You are confused. Did you read table 5 in the manual ? The 1’st byte above, “11110111” is not 123. It’s a 2’s compliment form of negative 9, that’s -9 bits. With the lsb of the MSB worth 1 deg C, that’s also -9 deg C. Still far off from room temp though so there’s something else wrong. Maybe with some more data at (or close to) known temps we can figure out what’s going on.

Unless you’re in extended mode and then the above is really 011110111. Which is 247. I think that’s out of range for the sensor so my belief is that you’re not in extended mode.

Sigh - thank you for persisting with the reading of Table 5. I had “read” it before but I neglected the portion concerning negative numbers. I did the top few lines, “understood” the pattern and then thought I knew what was going on. The Twos-Complement language was meaningless to me so I assumed (absurdly) that it didn’t matter once I saw the pattern. I’m very new to this and obviously should have looked things up and figured out the whole table. I see now that when you meant really low you meant negative and not close to 0 as I was thinking (lack of imagination). I believe I now understand. Thank you.

I ran 3 tests - 1 at room temp, 1 with the icecube, and 1 with a hair dryer blowing onto the chip (best I can do at the moment). I haven’t yet figured out how to take into account the sign bit progmatically and output the negative number so the “(int)” and “DEC” values are all wrong for those but the bytes are all there for analysis.

The sensor holds stable at room temperature around 1110101 ~117 C I believe?

When I put the bagged ice cube directly on the sensor it stabilizes at 1100110 ~102 C. A 15 C drop in temperature.

When I blow the hair dryer on the sensor something weird happens? The values start dropping dramatically. Eventually into negative territory which I was previously understanding to be super high temperatures.

So it looks to me like the sensor is reading highest at room temperature, lowest at high temperature, and somewhere in between at freezing?

Sorry about the (int) and DEC spam - I was trying to cast the byte into the 2s complement because I read that the int structure already accounted for this but it doesn’t seem to be working as I expected. Let me know if you need me to strip those lines out for readability.

Thanks for the help, and thanks again for being persistent when I’m obviously clueless.

Room Temperature:

bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	11100000
Concatenated byte is 	11101011110
Converted temp is 	244.17

Ice Cube:

bytes available:	2
firstbyte is 	1100111
firstbyte as (int) is 	103
firstbyte as DEC is 	103
secondbyte is 	1100000
Concatenated byte is 	11001110110
Converted temp is 	218.08
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	1100000
Concatenated byte is 	11001100110
Converted temp is 	216.27
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17
bytes available:	2
firstbyte is 	1100110
firstbyte as (int) is 	102
firstbyte as DEC is 	102
secondbyte is 	11100000
Concatenated byte is 	11001101110
Converted temp is 	217.17

Hair Dryer

bytes available:	2
firstbyte is 	1110100
firstbyte as (int) is 	116
firstbyte as DEC is 	116
secondbyte is 	11100000
Concatenated byte is 	11101001110
Converted temp is 	242.38
bytes available:	2
firstbyte is 	1110101
firstbyte as (int) is 	117
firstbyte as DEC is 	117
secondbyte is 	1100000
Concatenated byte is 	11101010110
Converted temp is 	243.27
bytes available:	2
firstbyte is 	1110110
firstbyte as (int) is 	118
firstbyte as DEC is 	118
secondbyte is 	1100000
Concatenated byte is 	11101100110
Converted temp is 	245.08
bytes available:	2
firstbyte is 	1101110
firstbyte as (int) is 	110
firstbyte as DEC is 	110
secondbyte is 	11100000
Concatenated byte is 	11011101110
Converted temp is 	231.58
bytes available:	2
firstbyte is 	1110111
firstbyte as (int) is 	119
firstbyte as DEC is 	119
secondbyte is 	11100000
Concatenated byte is 	11101111110
Converted temp is 	247.77
bytes available:	2
firstbyte is 	1011010
firstbyte as (int) is 	90
firstbyte as DEC is 	90
secondbyte is 	100000
Concatenated byte is 	10110100010
Converted temp is 	194.22
bytes available:	2
firstbyte is 	1010000
firstbyte as (int) is 	80
firstbyte as DEC is 	80
secondbyte is 	11010000
Concatenated byte is 	10100001101
Converted temp is 	177.46
bytes available:	2
firstbyte is 	100101
firstbyte as (int) is 	37
firstbyte as DEC is 	37
secondbyte is 	1110000
Concatenated byte is 	1001010111
Converted temp is 	99.39
bytes available:	2
firstbyte is 	11101
firstbyte as (int) is 	29
firstbyte as DEC is 	29
secondbyte is 	10100000
Concatenated byte is 	111011010
Converted temp is 	85.32
bytes available:	2
firstbyte is 	11000
firstbyte as (int) is 	24
firstbyte as DEC is 	24
secondbyte is 	10010000
Concatenated byte is 	110001001
Converted temp is 	76.21
bytes available:	2
firstbyte is 	10010
firstbyte as (int) is 	18
firstbyte as DEC is 	18
secondbyte is 	1110000
Concatenated byte is 	100100111
Converted temp is 	65.19
bytes available:	2
firstbyte is 	1100
firstbyte as (int) is 	12
firstbyte as DEC is 	12
secondbyte is 	11100000
Concatenated byte is 	11001110
Converted temp is 	55.18
bytes available:	2
firstbyte is 	1000
firstbyte as (int) is 	8
firstbyte as DEC is 	8
secondbyte is 	1100000
Concatenated byte is 	10000110
Converted temp is 	47.08
bytes available:	2
firstbyte is 	11
firstbyte as (int) is 	3
firstbyte as DEC is 	3
secondbyte is 	11000000
Concatenated byte is 	111100
Converted temp is 	38.75
bytes available:	2
firstbyte is 	11111111
firstbyte as (int) is 	255
firstbyte as DEC is 	255
secondbyte is 	11100000
Concatenated byte is 	111111111110
Converted temp is 	492.57
bytes available:	2
firstbyte is 	11111011
firstbyte as (int) is 	251
firstbyte as DEC is 	251
secondbyte is 	10110000
Concatenated byte is 	111110111011
Converted temp is 	485.04
bytes available:	2
firstbyte is 	11111000
firstbyte as (int) is 	248
firstbyte as DEC is 	248
secondbyte is 	11000000
Concatenated byte is 	111110001100
Converted temp is 	479.75
bytes available:	2
firstbyte is 	10
firstbyte as (int) is 	2
firstbyte as DEC is 	2
secondbyte is 	1110000
Concatenated byte is 	100111
Converted temp is 	36.39
bytes available:	2
firstbyte is 	11111110
firstbyte as (int) is 	254
firstbyte as DEC is 	254
secondbyte is 	11100000
Concatenated byte is 	111111101110
Converted temp is 	490.77
bytes available:	2
firstbyte is 	11111010
firstbyte as (int) is 	250
firstbyte as DEC is 	250
secondbyte is 	11000000
Concatenated byte is 	111110101100
Converted temp is 	483.35
bytes available:	2
firstbyte is 	11101000
firstbyte as (int) is 	232
firstbyte as DEC is 	232
secondbyte is 	11100000
Concatenated byte is 	111010001110
Converted temp is 	451.17
bytes available:	2
firstbyte is 	11100110
firstbyte as (int) is 	230
firstbyte as DEC is 	230
secondbyte is 	1100000
Concatenated byte is 	111001100110
Converted temp is 	446.67
bytes available:	2
firstbyte is 	11100011
firstbyte as (int) is 	227
firstbyte as DEC is 	227
secondbyte is 	1110000
Concatenated byte is 	111000110111
Converted temp is 	441.39

Here’s something to try:

For the lines:

Wire.requestFrom(TMP102_I2C_ADDRESS, 2);

Wire.endTransmission();

int bytesavailable = Wire.available();

I think you want to delete that endTransmission() following the requestFrom().

Also, the pointer register powers up to “00”, so you should be able to eliminate the send(0x00) altogether unless there is some other configuration you want to perform. If you just power up and do readings it is redundant.

Jim

I ordered 2 other tmp102 sensors and they solved the problem. Think I had a bad sensor. Thanks for the help.