Difference between revisions of "TMP102"

From MyLabWiki
Jump to: navigation, search
(Example Code)
(Example Code)
Line 16: Line 16:
 
== Example Code ==
 
== Example Code ==
  
<pre>
+
The following [[Category:Arduino|Arduino]] code snippet reads the temperature from the TMP102, converts the raw value to degrees Celsius and Fahrenheit, prints it on an LCD display and sends it to the serial port.
#include&nbsp;<<span style="color: #CC6600;">LiquidCrystal</span>.h>
+
#include&nbsp;<<span style="color: #CC6600;">Wire</span>.h>
+
  
 
+
  #include&nbsp;<<span style="color: #CC6600;">LiquidCrystal</span>.h>
<span style="color: #7E7E7E;">//&nbsp;initialize&nbsp;the&nbsp;library&nbsp;with&nbsp;the&nbsp;numbers&nbsp;of&nbsp;the&nbsp;interface&nbsp;pins</span>
+
  #include&nbsp;<<span style="color: #CC6600;">Wire</span>.h>
<span style="color: #CC6600;">LiquidCrystal</span> lcd(12, 11, 5, 4, 3, 2);
+
 
 
+
  <span style="color: #7E7E7E;">//&nbsp;initialize&nbsp;the&nbsp;library&nbsp;with&nbsp;the&nbsp;numbers&nbsp;of&nbsp;the&nbsp;interface&nbsp;pins</span>
<span style="color: #CC6600;">byte</span> res;
+
  <span style="color: #CC6600;">LiquidCrystal</span> lcd(12, 11, 5, 4, 3, 2);
<span style="color: #CC6600;">byte</span> msb;
+
 
<span style="color: #CC6600;">byte</span> lsb;
+
  <span style="color: #CC6600;">byte</span> res;
<span style="color: #CC6600;">int</span> val;
+
  <span style="color: #CC6600;">byte</span> msb;
<span style="color: #CC6600;">float</span> tC;  <span style="color: #7E7E7E;">// temperature in Celsius</span>
+
  <span style="color: #CC6600;">byte</span> lsb;
<span style="color: #CC6600;">float</span> tF;  <span style="color: #7E7E7E;">// temperature in Fahrenheit</span>
+
  <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: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>()
&nbsp;&nbsp;<span style="color: #7E7E7E;">// set up the LCD's number (col,row): </span>
+
  {
&nbsp;&nbsp;lcd.<span style="color: #CC6600;">begin</span>(20, 2);
+
  &nbsp;&nbsp;<span style="color: #7E7E7E;">// set up the LCD's number (col,row): </span>
&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"Temp"</span>);
+
  &nbsp;&nbsp;lcd.<span style="color: #CC6600;">begin</span>(20, 2);
 
+
  &nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"Temp"</span>);
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
+
 
&nbsp;&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">begin</span>();
+
  &nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
}
+
  &nbsp;&nbsp;<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>()
+
 
{
+
  <span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>()
&nbsp;&nbsp;lcd.<span style="color: #CC6600;">clear</span>();
+
  {
&nbsp;&nbsp;
+
  &nbsp;&nbsp;lcd.<span style="color: #CC6600;">clear</span>();
&nbsp;&nbsp;<span style="color: #7E7E7E;">/* get new value from TMP102 */</span>
+
  &nbsp;&nbsp;
&nbsp;&nbsp;res&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">requestFrom</span>(72,2);
+
  &nbsp;&nbsp;<span style="color: #7E7E7E;">/* get new value from TMP102 */</span>
&nbsp;&nbsp;
+
  &nbsp;&nbsp;res&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">requestFrom</span>(72,2);
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (res == 2) {
+
  &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;msb&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Whole degrees */</span>  
+
  &nbsp;&nbsp;<span style="color: #CC6600;">if</span> (res == 2) {
&nbsp;&nbsp;&nbsp;&nbsp;lsb&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Fractional degrees */</span>  
+
  &nbsp;&nbsp;&nbsp;&nbsp;msb&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Whole degrees */</span>  
&nbsp;&nbsp;&nbsp;&nbsp;val&nbsp;=&nbsp;((msb)&nbsp;<<&nbsp;4);&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* MSB */</span>
+
  &nbsp;&nbsp;&nbsp;&nbsp;lsb&nbsp;=&nbsp;<span style="color: #CC6600;">Wire</span>.<span style="color: #CC6600;">receive</span>(); <span style="color: #7E7E7E;">/* Fractional degrees */</span>  
&nbsp;&nbsp;&nbsp;&nbsp;val&nbsp;|=&nbsp;(lsb&nbsp;>>&nbsp;4);&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* LSB */</span>
+
  &nbsp;&nbsp;&nbsp;&nbsp;val&nbsp;=&nbsp;((msb)&nbsp;<<&nbsp;4);&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* MSB */</span>
&nbsp;&nbsp;&nbsp;&nbsp;
+
  &nbsp;&nbsp;&nbsp;&nbsp;val&nbsp;|=&nbsp;(lsb&nbsp;>>&nbsp;4);&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* LSB */</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* calculate temperature */</span>
+
  &nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;tC&nbsp;=&nbsp;val*0.0625;
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* calculate temperature */</span>
&nbsp;&nbsp;&nbsp;&nbsp;tF&nbsp;=&nbsp;(tC&nbsp;*&nbsp;9/5)&nbsp;+&nbsp;32;
+
  &nbsp;&nbsp;&nbsp;&nbsp;tC&nbsp;=&nbsp;val*0.0625;
&nbsp;&nbsp;&nbsp;&nbsp;
+
  &nbsp;&nbsp;&nbsp;&nbsp;tF&nbsp;=&nbsp;(tC&nbsp;*&nbsp;9/5)&nbsp;+&nbsp;32;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* show temperatures on display */</span>
+
  &nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//lcd.setCursor(0, 0);</span>
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">/* show temperatures on display */</span>
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(tC);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(tC);
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"C"</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"C"</span>);
 
+
 
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">setCursor</span>(0, 1);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">setCursor</span>(0, 1);
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(tF);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(tF);
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"F"</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"\xdf"</span><span style="color: #006699;">"F"</span>);
 
+
 
 
+
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tC);
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tC);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"C "</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"C   "</span>);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tF);
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(tF);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"F"</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"F"</span>);
 
+
 
&nbsp;&nbsp;}
+
  &nbsp;&nbsp;}
&nbsp;&nbsp;<span style="color: #CC6600;">else</span> {
+
  &nbsp;&nbsp;<span style="color: #CC6600;">else</span> {
&nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"ERR"</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;lcd.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"ERR"</span>);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"ERROR"</span>);
+
  &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"ERROR"</span>);
&nbsp;&nbsp;}
+
  &nbsp;&nbsp;}
&nbsp;&nbsp;
+
  &nbsp;&nbsp;
&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(1000);
+
  &nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(1000);
}
+
  }
 
+
</pre>
+
  
 
== References ==
 
== References ==

Revision as of 18:58, 2 June 2010

Contents

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

The following code snippet reads the temperature from the TMP102, converts the raw value to degrees Celsius and Fahrenheit, prints it on an LCD display and sends it to the serial port.

 #include <LiquidCrystal.h>
 #include <Wire.h>
 
 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
 byte res;
 byte msb;
 byte lsb;
 int val;
 float tC;  // temperature in Celsius
 float tF;  // temperature in Fahrenheit
 
 void setup()
 {
   // set up the LCD's number (col,row): 
   lcd.begin(20, 2);
   lcd.print("Temp");
 
   Serial.begin(9600);
   Wire.begin();
 }
 
 void loop()
 {
   lcd.clear();
   
   /* get new value from TMP102 */
   res = Wire.requestFrom(72,2);
   
   if (res == 2) {
     msb = Wire.receive(); /* Whole degrees */ 
     lsb = Wire.receive(); /* Fractional degrees */ 
     val = ((msb) << 4);   /* MSB */
     val |= (lsb >> 4);    /* LSB */
     
     /* calculate temperature */
     tC = val*0.0625;
     tF = (tC * 9/5) + 32;
     
     /* show temperatures on display */
     lcd.print(tC);
     lcd.print("\xdf""C");
 
     lcd.setCursor(0, 1);
     lcd.print(tF);
     lcd.print("\xdf""F");
 
 
     Serial.print(tC);
     Serial.print("C   ");
     Serial.print(tF);
     Serial.println("F");
 
   }
   else {
     lcd.print("ERR");
     Serial.println("ERROR");
   }
   
   delay(1000);
 }

References