TMP102

From MyLabWiki
Revision as of 18:51, 2 June 2010 by Alex (Talk | contribs)

Jump to: navigation, search

Description

The TMP102 is a two-wire, serial output temperature sensor available in a tiny SOT563 package. Requiring no external components, the TMP102 is capable of reading temperatures to a resolution of 0.0625°C.

The TMP102 features SMBus and two-wire interface compatibility, and allows up to four devices on one bus. It also features an SMB alert function.

The TMP102 is ideal for extended temperature measurement in a variety of communication, computer, consumer, environmental, industrial, and instrumentation applications. The device is specified for operation over a temperature range of –40°C to +125°C.

TMP102PinConf.png TMP102Diagram.png

Note that the TMP102 is a 3.3V device and will require a Logic Level Converter (Sparkfun) to connect it to a 5V Arduino.

Wiring

Example Code

#include <<span style="color: #CC6600;">LiquidCrystal</span>.h>
#include <<span style="color: #CC6600;">Wire</span>.h>


<span style="color: #7E7E7E;">// initialize the library with the numbers of the interface pins</span>
<span style="color: #CC6600;">LiquidCrystal</span> lcd(12, 11, 5, 4, 3, 2);

<span style="color: #CC6600;">byte</span> res;
<span style="color: #CC6600;">byte</span> msb;
<span style="color: #CC6600;">byte</span> lsb;
<span style="color: #CC6600;">int</span> val;
<span style="color: #CC6600;">float</span> tC;  <span style="color: #7E7E7E;">// temperature in Celsius</span>
<span style="color: #CC6600;">float</span> tF;  <span style="color: #7E7E7E;">// temperature in Fahrenheit</span>


<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>()
{
  <span style="color: #7E7E7E;">// set up the LCD's number (col,row): </span>
  lcd.<span style="color: #CC6600;">begin</span>(20, 2);
  lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"Temp"</span>);

  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
  <span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">begin</span>();
}

<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>()
{
  lcd.<span style="color: #CC6600;">clear</span>();
  
  <span style="color: #7E7E7E;">/* get new value from TMP102 */</span>
  res = <span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">requestFrom</span>(72,2);
  
  <span style="color: #CC6600;">if</span> (res == 2) {
    msb = <span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Whole degrees */</span> 
    lsb = <span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Fractional degrees */</span> 
    val = ((msb) << 4);   <span style="color: #7E7E7E;">/* MSB */</span>
    val |= (lsb >> 4);    <span style="color: #7E7E7E;">/* LSB */</span>
    
    <span style="color: #7E7E7E;">/* calculate temperature */</span>
    tC = val*0.0625;
    tF = (tC * 9/5) + 32;
    
    <span style="color: #7E7E7E;">/* show temperatures on display */</span>
    <span style="color: #7E7E7E;">//lcd.setCursor(0, 0);</span>
    lcd.<span style="color: #CC6600;">print</span>(tC);
    lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"C"</span>);

    lcd.<span style="color: #CC6600;">setCursor</span>(0, 1);
    lcd.<span style="color: #CC6600;">print</span>(tF);
    lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"F"</span>);


    <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tC);
    <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"C "</span>);
    <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tF);
    <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"F"</span>);

  }
  <span style="color: #CC6600;">else</span> {
    lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"ERR"</span>);
    <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"ERROR"</span>);
  }
  
  <span style="color: #CC6600;">delay</span>(1000);
}

References