TMP102 Temperature Sensor

Table of Contents

  1. Hello World!
  2. Library

The TMP102 is an I2C digital temperature sensor in a small SOT563 package, with a 0.0625C resolution and 0.5C accuracy.

/media/uploads/chris/tmp102_crop.jpg

Hello World!

TMP102mbed
1 - Vcc (square pad)Vout
2 - SDAp9
3 - SCLp10
4 - GndGnd

Import program

00001 #include "mbed.h"
00002 #include "TMP102.h"
00003 
00004 TMP102 temperature(p9, p10, 0x90); //A0 pin is connected to ground
00005 
00006 int main()
00007 {
00008   while(1) {
00009     printf("Temperature: %f\n", temperature.read());
00010     wait(1.0);
00011   }
00012 }

Library

Import libraryTMP102

TMP102 published as a library that can be imported independent





19 comments:

29 Oct 2010
14 Feb 2011

Hi, I want to use two sets of TM102 sensors and I am using the second 12C bus on pins 28,27 however I added an additional class see below with pin numbers changed but the code hangs when attempting to read from the second I2C bus. Main - see code files

/media/uploads/pmr1/tmp102_2.zip

15 Feb 2011

Hi Paul,

There should be no need to create new code to connect to two of these (i.e. no need for TMP102 and TMP102_2 classes), just instance it twice. So instead of:

TMP102 temperature0(p9, p10, 0x90); //A0 pin is connected to ground, Clock is P10
TMP102_2 temperature1(p28, p27, 0x90);

just do:

TMP102 temperature0(p9, p10, 0x90); 
TMP102 temperature1(p28, p27, 0x90);

In fact, the TMP102 can be configured to have different I2C addresses, so you could even put them on the same bus, and just change the address (i.e both use p9, p10).

Simon

07 Mar 2011

Hi I'm seeing my .read() statement hang the MBED. i.e it does not return from the call now and again over long periods of time.

I'm sampling about once a second i.e

temperature.read(); wait(1);

This hanging happens every few hours and does not seem to have a pattern.

I have a watchdog routine set to 30 seconds and it then resets the system and all is well again for a period of time. Anyone else see this? and any possible solutions?

06 Apr 2011

Please note that the example doesn't work with the Cool Components LPC1768 Workshop Development Board (version 2):

http://www.coolcomponents.co.uk/catalog/product_info.php?products_id=608

This is because p9 is tied to 3.3V by the SD socket. You can work around this by using pins 28 and 27:

TMP102 temperature(p28, p27, 0x90);   //A0 pin is connected to ground

As the comment implies, you also need to tie the ADD0 pin on the breakout board to GND.

15 Aug 2012

0.000000 this is the only value i recieve in my terminal at a 9600 baud rate, with other baud rates i get only trash characters ... any ideas ? i swapped p9 with p28 and p10 with p27 and the result is the same :-?

15 Aug 2012

Check the connections on the tmp102 board. Are vcc, gnd, sda and scl OK. Make sure that add0 is connected to gnd also.

20 Aug 2012

Wim Huiskamp wrote:

Check the connections on the tmp102 board. Are vcc, gnd, sda and scl OK. Make sure that add0 is connected to gnd also.

oops! i forgot about de 2.2k pull-ups

edit: i have placed the pull-ups between 3.3V and p28(sda) p27(scl), and the result is the same ... i have a wire from vout to the + bus on my breadboard, add0 and gnd are on the - bus on my breadboard, i have the pullup with one end on the + and the other on the same line with the wire that goes on p28, for 27 it's the same alignment, am i missing something ? /media/uploads/razvancob/_scaled_jpg.jpg

20 Aug 2012

A link to an unscaled version would help, since that picture is too small to see much.

20 Aug 2012

Erik Olieman wrote:

A link to an unscaled version would help, since that picture is too small to see much.

/media/uploads/razvancob/jpg.jpg

here it is, here is the code also:

  1. include "mbed.h"
  2. include "TMP102.h" TMP102 temperature(p28, p27, 0x90); A0 pin is connected to ground

Serial pc(USBTX, USBRX); tx, rx to PC

int main()

{

while(1) {

pc.printf("Temperature: %f\n", temperature.read());

wait(1.0);

}

}

20 Aug 2012

Two things: first of all, you put the breakout board on your breadboard, and then plugged the wires in your breadboard via the breakout board? Does that give a reliable connection? (Not saying it is not possible, just wondering).

Then tracing wires from the picture is not the easiest thing to do, but from the picture on the sparkfun site I see the Vdd connection is on the side opposite to the sparkfun logo, so the right side in this picture. Besides the Vdd pin is the SDA pin, which goes via the light blue (/teal) wire to the bottom pull up resistor and the red wire, which goes to p27 of the mbed. However p27 is the SCL pin on the mbed, not SDA. Maybe I am tracing the wires wrong, but are you sure you got them correctly and not mixed up?

As they said in the other topic you could check the power supply lines on your breadboard, but tbh i would be really surprised if not the entire lane would be connected.

20 Aug 2012

@Erik Agree that breadboard wires plugged into breakout board holes are probably unreliable. Also looks like SDA and SCL might be reversed.

Regarding the breadboard supply lines, there are two types around. Some with power lanes and others without, see http://www.umakebot.nl/en/prototyping-parts/29-prototyping-value-pack-1.html The picture above shows continuous red and blue lines next to the 5 hole connectors. This makes it likely to be full length connected, but better make sure.

21 Aug 2012

Erik Olieman wrote:

Two things: first of all, you put the breakout board on your breadboard, and then plugged the wires in your breadboard via the breakout board? Does that give a reliable connection? (Not saying it is not possible, just wondering).

i remade the whole wiring and did them as they should be, i'm not sure the resistors went in all the way because they were bending from that metal part that goes in, the entire lane is connected, i tried it with another experiment with a led and potentiometer and placed 3.3v and the entire lane is connected. now i've realised that the connection between the wire that goes into the breadboard and the breakboard is imperfect. thank you for noticing this.

11 Nov 2012

The TMP102.cpp read() function does not return correct values below zero.
The high byte (reg[0]) of the TMP102 temperature register contains the sign bit which must be sign extended (when promoting it to 16 bits) before shifting left. The low byte (reg[1]) is unsigned and must be shifted right with zero fill into bits 7-4.
Documentation is linked to at the top of the page.
The proposed change to the TMP102.cpp file is below (just one line to change - do not include the two commented lines):

  // unsigned short res = (reg[0] << 4) | (reg[1] >> 4); // Original.  Incorrect below zero.
  // short res = ((signed char)reg[0] << 4) | ((unsigned char)reg[1] >> 4); // Correct.
  int16_t res  = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4); // Best (uses stdint.h).

Sparkfun TMP102 does not need pull-up resistors
The Sparkfun TMP102 already has the required pull-up resistors (1k) so additional resistors are not required. The resistor values should probably be higher, especially as you can connect four of these on the same I2C bus. Really, it is the bus that should have the pull-up resistors (not all the connected devices), otherwise there is going to be extra current draw when the lines are pulled low (with possible damage to some devices as well as reduced battery life). It would be nice if the Sparkfun breakout was redesigned with jumper leads from V+ (or from the SCL/SDA pins) to each of the SCL and SDA resistors (so the jumper leads can be cut when there are more devices on the same bus).

07 Mar 2013

Hi, I cannot read anything from the TMP102, i tried with the example code and this gave nothing. I made my own code:

#include "mbed.h"

Serial pc (USBTX, USBRX);

I2C i2c (p9, p10);  

const int addr = 0x90; 


int main() {

char send[1];

send[0]=0;   //sending the pointer adres to temperature register

char ontvangentemp[2];  //want 2 bytes of information in

while(1){

i2c.write(addr, send, 1, 0);

i2c.read(addr, ontvangentemp, 2, 0);  

pc.printf("test voor TMP102_nieuw1\n\r");

pc.printf("\n\r the temperatuur is %d , %d", ontvangentemp[0], ontvangentemp[1]);

} 

}

The first time i got "test .... the temperatuur is 0, 128), the next times i always get 0,0. how come? i know this will not be the right temperature because i still have to do some conversion Next problem is, most of the time when i push on the button nothing happens in hyperterminal, and then suddenly they come with 10 at the time and i dont see a relation when it happens. anybody has an idea, thanks in advance

08 Mar 2013

Adriaan Van Steendam wrote:

Hi, I cannot read anything from the TMP102, i tried with the example code and this gave nothing. I made my own code:

#include "mbed.h"

Serial pc (USBTX, USBRX);

I2C i2c (p9, p10);  

const int addr = 0x90; 


int main() {

char send[1];

send[0]=0;   //sending the pointer adres to temperature register

char ontvangentemp[2];  //want 2 bytes of information in

while(1){

i2c.write(addr, send, 1, 0);

i2c.read(addr, ontvangentemp, 2, 0);  

pc.printf("test voor TMP102_nieuw1\n\r");

pc.printf("\n\r the temperatuur is %d , %d", ontvangentemp[0], ontvangentemp[1]);

} 

}

The first time i got "test .... the temperatuur is 0, 128), the next times i always get 0,0. how come? i know this will not be the right temperature because i still have to do some conversion Next problem is, most of the time when i push on the button nothing happens in hyperterminal, and then suddenly they come with 10 at the time and i dont see a relation when it happens. anybody has an idea, thanks in advance

09 Mar 2013

The address pin must be connected. For 0x90, connect it to Ground.

0x90 = Ground
0x92 = V+
0x94 = SDA
0x96 = SCL

I have changed Adriaan's code so you can play with the address pin as it is running.

#include "mbed.h"
Serial pc (USBTX, USBRX);
I2C i2c (p9, p10);
//I2C i2c (p28, p27);
uint16_t addr = 0x90;
int main() {
    char send[1];
    send[0]=0;   //sending the pointer adres to temperature register
    char reg[2];  //want 2 bytes of information in
    int16_t res;
    while(1){
        reg[0] = reg[1] = 0;
        i2c.write(addr, send, 1, 0);
        i2c.read(addr, reg, 2, 0);
        res  = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4);
        //pc.printf("test voor TMP102_nieuw1\n\r");
        pc.printf("addr = 0x%02x, reg0 = %3d, reg1 = %3d, f = %9.4f\n\r", addr, reg[0], reg[1], (float)res * 0.0625);
        wait(1);
        addr = (addr + 1) & 0x97;
    }
}
17 Jun 2013

John Schooling wrote:

I have changed Adriaan's code so you can play with the address pin as it is running. ...

Tell me did you manage to get it running in the end?

I'm trying right now to get this sensor to work, and I have a few issue...

- it seems to me the lib installed by mbed isn't quite finished (pin(s) are hard coded to p9, p10) so if you are trying to use it with p28, p27 you either modify TMP102.cpp to hardcode the new value or modify the function to be variable.

- Also the address in the TMP102 specification for ADD0 selection are rather 0x48 to 0x4B than 0x90 to 0x93

- My problem is I get temp readings of 0.000000 too! Did you find out why? And a solution?

17 Jun 2013

Pierre Gianni wrote:

Tell me did you manage to get it running in the end? - My problem is I get temp readings of 0.000000 too!

In the end I managed to get it working, here is how...

Pierre Gianni wrote:

- it seems to me the lib installed by mbed isn't quite finished (pin(s) are hard coded to p9, p10) so if you are trying to use it with p28, p27 you either modify TMP102.cpp to hardcode the new value or modify the function to be variable.

There are 2 libs available for the TMP102 sensor, one is correct the other looks like a first draft instantiated the I2C class with hardcoded values but was never used. So no problem here.

Pierre Gianni wrote:

- Also the address in the TMP102 specification for ADD0 selection are rather 0x48 to 0x4B than 0x90 to 0x93

I misread the spec, there is one more bit added to the end to signal whether you're performing a read or a write... So the codes are : 0x90 for ADD0-> Gnd 0x92 for ADD0-> V+ 0x94 for ADD0-> SDA 0x96 for ADD0-> SCL

I Tested my sensor on p28, p27, with ADD0-> V+ 0x92 and got proper readings.