videanuadrian
Posts: 3
Joined: Sun Jan 27, 2013 6:43 am

tmp102 problems

Sun Jan 27, 2013 6:53 am

Hi all,
i have two tmp102 sensors wired in parallel on I2C bus on 0x48 and 0x49 addresses. When i read the values and convert them to Celsius sometimes it seems to show the correct values sometimes not. I guess that i`m doing something wrong here. This is my code:

Code: Select all

public class TempSensor {

public static void main(String[] args) throws Exception {

    I2CBus i2cBus = I2CFactory.getInstance(I2CBus.BUS_1);

    for (int i=0;i<1000;i++){


    I2CDevice insideSensor = i2cBus.getDevice(0x48);
    I2CDevice outsideSensor = i2cBus.getDevice(0x49);

        byte[] bufferInside = new byte[2];
        byte[] bufferOutside = new byte[2];

        int amountInside = insideSensor.read(bufferInside,0,2);
        int amountOutside = outsideSensor.read(bufferOutside,0,2);

        Integer msbIn = bufferInside[0]<<4;
        Integer lsbIn = bufferInside[1]>>4;
        Integer tIn = msbIn | lsbIn;

        System.out.println("IN : "+tIn*0.0625);

        Integer msbOut = bufferOutside[0]<<4;
        Integer lsbOut = bufferOutside[1]>>4;
        Integer tOut = msbOut | lsbOut;

        System.out.println("Out : "+tOut*0.0625);

        Thread.sleep(10000);
        tIn = null;
        tOut = null;
        insideSensor = null;
        outsideSensor = null;

    }
}
}
Here we have some output data:

java -classpath .:classes:/opt/pi4j/lib/'*' TempSensor
Jan 26, 2013 10:40:28 PM com.pi4j.util.NativeLibraryLoader load
INFO: Library [pi4j] loaded successfully using embedded resource file: [jar:file:/opt/pi4j/lib/pi4j-core.jar!/lib/hard-float/libpi4j.so]
IN : 24.4375
Out : 21.4375
IN : 24.375
Out : 21.4375
IN : 24.375
Out : 21.375
IN : -0.125
Out : 21.3125
IN : 26.3125
Out : 21.375
IN : 27.1875
Out : 21.375
IN : -0.1875
Out : 21.4375
IN : 28.3125
Out : 21.4375

So as you can see the value differs, without moving the sensors.
Any idea ?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: tmp102 problems

Tue Feb 05, 2013 5:25 pm

I assume the device.read() returns the number of bytes read? You should probably check the amountInside to see if the read was successful? And only show the value if so.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

pskopek
Posts: 2
Joined: Tue Feb 12, 2013 10:08 pm

Re: tmp102 problems

Tue Feb 12, 2013 10:13 pm

I am experiencing the same problem, but not very frequently.
Number of bytes read from read method is always 2.

pskopek
Posts: 2
Joined: Tue Feb 12, 2013 10:08 pm

Re: tmp102 problems

Thu Feb 14, 2013 11:51 pm

Problem solved!
Don't forget that all Java primitive integer types are signed (yes, including byte). When leftmost bit is 1 it means that value is negative for Java, but i2c driver reads data as they appear on TMP102 temperature register, so one must be careful when converting those bytes to integers.

Here is my solution:

Code: Select all

    public static double temperature(byte[] tempBuffer) {
        
        int msb = tempBuffer[0] < 0 ? 256 + tempBuffer[0] : tempBuffer[0];  
        int lsb = tempBuffer[1] < 0 ? 256 + tempBuffer[1] : tempBuffer[1];
        
        msb = msb << 4;
        lsb = lsb >> 4;
        
        int count = msb | lsb;
        
        return count * 0.0625;
    }
With this change it works like a charm.

videanuadrian
Posts: 3
Joined: Sun Jan 27, 2013 6:43 am

Re: tmp102 problems

Sat Feb 16, 2013 6:40 am

Hi, i solved the issue by performing an AND with 0xFF and then shifting the result with 4 for the second byte:

Code: Select all

I2CBus i2cBus = I2CFactory.getInstance(I2CBus.BUS_1);
I2CDevice insideSensor = i2cBus.getDevice(0x48);
byte[] bufferInside = new byte[2];
insideSensor.read(bufferInside, 0, 2);
Byte b1In = bufferInside[0];
Byte b2In = bufferInside[1];

Integer msbIn = b1In << 4;
Integer lsbIn = (b2In & 0xFF)>>4;
Integer tIn = msbIn | lsbIn;
System.out.println("IN Temp: " + tIn * 0.0625 + " HEX:"	+ bufferInside[0] + "," + bufferInside[1]);

Return to “Java”

Who is online

Users browsing this forum: No registered users and 2 guests