Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

TMP102 Temperature Sensor


  • Please log in to reply
7 replies to this topic

#1 Eppetiano

Eppetiano

    Member

  • Members
  • PipPip
  • 10 posts

Posted 05 May 2012 - 02:21 PM

Hello all, first of all thanks for this amazing forum. I really need your help on this, i'm using the TMP102 temperature sensor, that returns data in a two byte format, but temperature value it's only on the 12 msb. So my question is, how can i convert this two hexadecimal byte format to a Celsius value. I got byte[1] = 224 and byte[0] = 22 which are two strange values... I have my code as attachments. Thanks!!

Attached Files



#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 May 2012 - 02:52 PM

var temperature = ((byte[0] << 4) | (byte[1] >> 4))*0.0625;
The result for sample values is 22.875ºC.

#3 Eppetiano

Eppetiano

    Member

  • Members
  • PipPip
  • 10 posts

Posted 05 May 2012 - 03:13 PM

Hello CW2, first things first, THANKS A LOT for four quick and useful answer. But, can you explain me how you came to that code? Thanks!

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 May 2012 - 06:51 PM

As you already know, the temperature is 12-bit value stored in two bytes

bit#      B7  B6 B5 B4 B3 B2 B1 B0
Byte[0]  T11 T10 T9 T8 T7 T6 T5 T4
Byte[1]   T3  T2 T1 T0  0  0  0  0
To manipulate bits shift and logical operators are often used. Also, one important thing to note is that those operators are not really defined for byte type, so the operands are automatically converted to int, and thus the result is also int (C# specification 7.2.6.2).

So, by shifting byte[1] four bits to the right those four zeros are replaced with T3 - T0, and the result is of type int

bit#         |B31..B12 B11 B10 B9 B8  B7  B6 B5 B4 B3 B2 B1 B0
Byte[0]      |                       T11 T10 T9 T8 T7 T6 T5 T4
Byte[1] >> 4 | 0 .. 0    0   0  0  0   0   0  0  0 T3 T2 T1 T0
In order to be able to combine two bytes into one 12-bit value, byte[0] has to be shifted four bits to the left, so T7-T4 precedes T3 - T0 (they overlap); the result is also int

bit#         |B31..B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
Byte[0] << 4 | 0 .. 0  T11 T10 T9 T8 T7 T6 T5 T4  0  0  0  0
Byte[1] >> 4 | 0 .. 0    0   0  0  0  0  0  0  0 T3 T2 T1 T0
The two integers (results of shift operations) are merged together by logical OR operator ('|')

bit#         |B31..B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
Byte[0] << 4 | 0 .. 0  T11 T10 T9 T8 T7 T6 T5 T4  0  0  0  0
Byte[1] >> 4 | 0 .. 0    0   0  0  0  0  0  0  0 T3 T2 T1 T0
------------------------------------------------------------
OR-ed        | 0 .. 0  T11 T10 T9 T8 T7 T6 T5 T4 T3 T2 T1 T0
The result is then multiplied by 0.0625, which is the resolution of the sensor, as specified in the datasheet.

Please note this expression does not handle negative temperature.

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 May 2012 - 07:20 PM

The following expression handles negative temperatures too:

var temp = ((short)((byte0 << 8) | byte1))/256F;
The analysis is left as an exercise to the reader (or available upon request) Posted Image

#6 Eppetiano

Eppetiano

    Member

  • Members
  • PipPip
  • 10 posts

Posted 05 May 2012 - 10:23 PM

CW2, i just have to say one thing: THANK YOU SO MUCH!!! What an incredible explanation, man! It is now clear for me! At last. I'm really grateful for your help! Keep it that way!

#7 simon_gan01

simon_gan01

    New Member

  • Members
  • Pip
  • 2 posts

Posted 15 May 2012 - 10:34 AM

Hi, could you share how the TMP102 is connected to Netduino? Thanks.

#8 mekros

mekros

    New Member

  • Members
  • Pip
  • 7 posts

Posted 26 May 2012 - 09:33 PM

I connected mine via I2C




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.