SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By donzi
#96726
Hi,
Would anyone have some Arduino code they could share for hooking up a
Digital Temperature Sensor Breakout - TMP102 to an Arduino board?
Looking for how the temperature sensor would be wired into the board and
code to read the temperature readings and display them in the Arduino programing
serial monitor window?

Thanks for any help you can provide.
Don Anderson
By f1fan3584
#115405
Here is the program I've been using, it is origianlly from http://wiring.org.co/learning/libraries ... rkfun.html, but I've added/tweaked it. Check out the url for how to wire and the original program.
Code: Select all
#include <Wire.h>

/* From the datasheet the BMP module address LSB distinguishes
// between read (1) and write (0) operations, corresponding to 
// address 0x91 (read) and 0x90 (write).
// shift the address 1 bit right (0x91 or 0x90), the Wire library only needs the 7
// most significant bits for the address 0x91 >> 1 = 0x48
// 0x90 >> 1 = 0x48 (72)
*/

int sensorAddress = 0b1001000; // From TI TMP102 datasheet page ten sensor address if ADD0 is connected to GND
//int sensorAddress = 0b1001001; // From TI TMP102 datasheet page ten sensor address if ADD0 is connected to V+
//int sensorAddress = 0b1001010; // From TI TMP102 datasheet page ten sensor address if ADD0 is connected to SDA
//int sensorAddress = 0b1001011; // From TI TMP102 datasheet page ten sensor address if ADD0 is connected to SCL

byte msb;
byte lsb;
int temperature;

void setup()
{
  Serial.begin(9600);  // start serial communication at 9600bps
  Wire.begin();        // join i2c bus (address optional for master) 
  Serial.println("TMP102"); //Print name of sketch
}

void loop()
{
  // step 1: request reading from sensor 
  Wire.requestFrom(sensorAddress,2); 
  if (2 <= Wire.available())  // if two bytes were received 
  {
    msb = Wire.receive();  // receive high byte (full degrees)
    Serial.print(" msb Hex:"); Serial.print(msb,HEX); Serial.print(" BIN: "); Serial.println(msb,BIN); // Prints the msb in hex and binary
    lsb = Wire.receive();  // receive low byte (fraction degrees) 
    Serial.print(" lsb Hex:"); Serial.print(lsb,HEX); Serial.print(" BIN: "); Serial.println(lsb,BIN); // Prints the lsb in hex and binary

    temperature = (msb << 8);  // MSB
    temperature |= lsb;   // LSB 
    temperature = temperature >> 4;
    Serial.print("Temperature in binary: ");  
    Serial.println(temperature, BIN);
    Serial.print("Temperature is ");
    //Serial.print(temperature*0.0625); Serial.println(" C");         // prints temperature value in degC
    Serial.print(temperature*0.0625*9/5+32); Serial.println(" F");  // prints temperature value converted to degF
    Serial.println("");
  }
  delay(100);  // wait .1 sec
}
Hope this helps,