TMP102 on a Raspberry Pi using Python

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

TMP102 on a Raspberry Pi using Python

Post by static »

I'm trying to expand the Adafruit I2C Python Library.
I'm not having much success. I believe this is because I'm messing up the swapping of the least significant bit and most significant bit.

Code: Select all

#!/usr/bin/python

import time
from Adafruit_I2C import Adafruit_I2C

class TMP102 :
  i2c = None


  # Constructor
  def __init__(self, address=0x48, mode=1, debug=False):
    self.i2c = Adafruit_I2C(address)

    self.address = address
    self.debug = debug
    # Make sure the specified mode is in the appropriate range
    if ((mode < 0) | (mode > 3)):
      if (self.debug):
        print "Invalid Mode: Using STANDARD by default"
      self.mode = self.__BMP085_STANDARD
    else:
      self.mode = mode



  def readRawTemp(self):
    "Reads the raw (uncompensated) temperature from the sensor"
    raw = self.i2c.readU16(0x48)     #The TMP102 returns 12-bits, I think
    raw = self.i2c.reverseByteOrder(raw)  #I need to get the bit order reveresed
    if (self.debug):
      print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw)
    return raw


  def readTemperature(self):
    "Gets the compensated temperature in degrees celcius"

    RawBytes = self.readRawTemp()  #get the temp from readRawTemp (above)


    temp = float(RawBytes) *0.0625  #this is the conversion value from the data sheet.
    if (self.debug):
      print "DBG: Calibrated temperature = %f C" % temp
    
    
    return RawBytes,temp


    
This doesn't look like it is too complicated. That being said, i've been plugging away at this for hours. If anyone has any ideas, I'm more than happy to try them.
I'm getting values that change with the temp, but they're incredibly high.

Thanks,
Static

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: TMP102 on a Raspberry Pi using Python

Post by static »

I modified the code a little bit. First, I did away with the "Reverse Byte Order" conversion. Next, I bit shifted the raw temp over four bits. That got me something that looks closer to the temperature. However, it always seems to end in a .375. The temperature is very close to the temp that I'm getting from the BMP085 that is sitting 15 centimeters away from it (and two and a half meters of cabling away).

I'm not sure that I'm correct here. Can anyone check this code?

Code: Select all

#!/usr/bin/python

import time
from Adafruit_I2C import Adafruit_I2C

class TMP102 :
  i2c = None


  # Constructor
  def __init__(self, address=0x48, mode=1, debug=False):
    self.i2c = Adafruit_I2C(address)

    self.address = address
    self.debug = debug
    # Make sure the specified mode is in the appropriate range
    if ((mode < 0) | (mode > 3)):
      if (self.debug):
        print "Invalid Mode: Using STANDARD by default"
      self.mode = self.__BMP085_STANDARD
    else:
      self.mode = mode



  def readRawTemp(self):
    "Reads the raw (uncompensated) temperature from the sensor"
    raw = self.i2c.readU16(0x48)     #The TMP102 returns 12-bits, I think
    #raw = self.i2c.reverseByteOrder(raw)  #I need to get the bit order reveresed
    if (self.debug):
      print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw)
    return raw


  def readTemperature(self):
    "Gets the compensated temperature in degrees celcius"

    RawBytes = self.readRawTemp()  #get the temp from readRawTemp (above)
    RawBytes = (RawBytes >> 4)


    temp = float(RawBytes) *0.0625  #this is the conversion value from the data sheet.
    if (self.debug):
      print "DBG: Calibrated temperature = %f C" % temp
    
    
    return RawBytes,temp


pwootton
 
Posts: 3
Joined: Wed Mar 13, 2013 9:47 pm

Re: TMP102 on a Raspberry Pi using Python

Post by pwootton »

Try this

Code: Select all

#!/usr/bin/python

import time
from Adafruit_I2C import Adafruit_I2C

# ============================================================================
# Tmp102 Class
# ============================================================================

class Tmp102:
  i2c = None

  # Constructor
  def __init__(self, address=0x48, mode=1, debug=False):
    self.i2c = Adafruit_I2C(address, debug=debug)

    self.address = address
    self.debug = debug
    # Make sure the specified mode is in the appropriate range
    if ((mode < 0) | (mode > 3)):
      if (self.debug):
        print "Invalid Mode: Using STANDARD by default"
      self.mode = self.__BMP085_STANDARD
    else:
      self.mode = mode

def readRawTemp(self):
    "Reads the raw (uncompensated) temperature from the sensor"
    self.i2c.write8(0, 0x00)                 # Set temp reading mode
    raw = self.i2c.readList(0,2)

    val = raw[0] << 4;
    val |= raw[1] >> 4;

    return val


  def readTemperature(self):
    "Gets the compensated temperature in degrees celcius"

    RawBytes = self.readRawTemp()  #get the temp from readRawTemp (above)
    temp = float(float(RawBytes) * 0.0625)  #this is the conversion value from the data sheet.
    if (self.debug):
      print "DBG: Raw Temp: 0x%04X (%d)" % (RawBytes & 0xFFFF, RawBytes)
      print "DBG: Calibrated temperature = %f C" % temp
    
    return RawBytes,temp

if __name__ == "__main__":
  import time

  mytemp = Tmp102(address=0x48)
  print mytemp.readTemperature()[1]

I'm not sure what the mode stuff on the constructor is for, but the crucial change is reading the 2 data bytes in a readList, rather than as U16 (or S16 or 2*U8 or 2*S8)

-- Paul --

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: TMP102 on a Raspberry Pi using Python

Post by static »

I just updated to the new Adafruit I2C code. I need to look at how the changes are propagating through my code. Right now, it looks like the readlist() addition improves the quality of the data coming from the sensor. However, it looks like the new Adafruit code breaks my other sensors. I'm getting bizarre data from them.

Thanks for checking my work. I knew there was something hinky there, I just wasn't seeing how the bit shift worked.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: TMP102 on a Raspberry Pi using Python

Post by static »

I haven't seen that final code snippet before. I like that a lot. That's going to save me soooo much time troubleshooting and deploying code.

What's the [1] for here?

Code: Select all

print mytemp.readTemperature()[1]
The constructor passes self (needed), the default address, mode (this is junk code from when I used the BMP085 code as a basis for this, it doesn't do anything here), debug (this is going to be passed down the code to the I2C code when I clean things up). I need to add the bus number to the constructor, as it's pretty easy to use both buses on the Pi. I've re-written my code, but I'm error checking it today. I need to clean it up and re-list it.

I seem to be getting weird values since I switch to the new Adafruit_I2C code (the "ReadList" function wasn't available before). Currently my office is reading as 131.4 degrees Celsius... I'm still alive and pretty comfortable, so that's not right. It's reading my external temp as too high as well (two different sensors, internal is the BMP085, external is the TMP102). My light sensors are also acting wonky.

More as I get it.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”