Notes About Working with Various Arduino & Netduino Microcontroller Boards

Saturday, December 8, 2012

Using a TMP102 Temperature Sensor with a Netduino Plus 2


The following example takes a reading and prints out the temperature in degrees Celsius and Fahrenheit. 

Connections:
TMP102  -->  Netduino Plus 2
GND           GND
SCL           SCL
SDA           SDA
V+            3V3
ADD0          GND  

C# Code:

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace TMP102
{
    public class Program
    {
        public static void Main()
        {
            // 
            byte TMP102Addr = 0x48;
            I2CDevice tmp102 = new I2CDevice(new I2CDevice.Configuration(TMP102Addr, 100));
            byte[] tempData = { 0, 0 };
            I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateReadTransaction(tempData)
            };
            int bytesRead = tmp102.Execute(reading, 100);
            int readVal = ((tempData[0] << 8) | tempData[1]) >> 4;
            double c = readVal * 0.0625;
            double f = c * 1.8 + 32;
            Debug.Print(c.ToString() + " C / " + f.ToString() +" F");            
        }
    }
}

No comments:

Post a Comment