Main Content

Measure Temperature from I2C Peripheral Device on NI USB-8452 Controller

This example shows how to communicate with I2C peripheral devices on the NI™ USB-8452 controller. In this example, a TMP102 digital temperature sensor is connected to the NI USB-8452 controller.

The TMP102 is a two-wire serial output digital sensor that can read temperature with a resolution of 0.0625 °C. It can also read temperature values above 128 °C in extended mode.

Set Up Hardware

Connect the SDA, SCL, GND, and VCC pins of the sensor to the corresponding pins on the NI USB-8452 hardware. For this example, connect the SDA and SCL pins of the sensor to Pin 3 and Pin 5 of the NI USB-8452, respectively. Connect the GND and VCC pins to Pin 2 and Pin 7 (DIO(0)), respectively.

Connect to I2C Peripheral Device

Search for NI USB-8452 hardware connected to your machine using ni845xlist and connect to it in MATLAB® using ni845x.

list = ni845xlist
list=1×2 table
             Model        SerialNumber
         _____________    ____________

    1    "NI USB-8452"     "01F26E0A" 

controller = ni845x(list.SerialNumber)
controller = 
  NI845x with properties:

                   Model: "NI USB-8452"
            SerialNumber: "01F26E0A"
    AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]

  Show all properties, functions

Configure the DIO(0) pin as output and output a logic high level voltage of 3.3 V to power the temperature sensor.

configureDigitalPin(controller,"P0.0","output");
writeDigitalPin(controller,"P0.0",1);

Scan the NI USB-8452 hardware for available I2C addresses. The temperature sensor is represented by the I2C address 0x48.

address = scanI2CBus(controller)
address = 1×2 string
    "0x48"    "0x53"

Connect to the I2C peripheral device using the device function with the I2C address returned by scanI2CBus.

tempSensor = device(controller,I2CAddress=address(1))
tempSensor = 
  I2CDevice with properties:

      Protocol: "I2C"
    I2CAddress: 72
       BitRate: 100000
     ByteOrder: "little-endian"

  Show all functions

Read Temperature Value

In normal mode, the sensor returns a temperature value digitized into 12 bits, with 8 bits in MSB and 4 bits in LSB. Each LSB is equal to 0.0625 °C. Read two bytes of data from register address 0 of the sensor as uint8 data type.

Calculate the temperature in °C by using the tmp102Temperature helper function. You can find this helper function at the end of this example and attached to this example as a supporting file.

data = readRegister(tempSensor,0,2,"uint8");
temperature = tmp102Temperature(data,12)
temperature = 24.7500

Read Temperature with Higher Measurement Limit

You can measure temperatures above 128 °C by using 13 bits in the TMP102 sensor's extended mode. To do so, write the value 'B060' as a hex value to the configuration register at address 1, as specified in the TMP102 device datasheet.

writeRegister(tempSensor,1,0xB060,"uint16");

Read the temperature from register address 0 to get a more precise result. Since the TMP102 sensor's conversion rate defaults to 4 Hz, pause MATLAB for about 0.25 seconds before each reading. Convert the data to °C by using the tmp102Temperature helper function.

write(tempSensor,0x0,"uint8");
pause(0.25);
data = read(tempSensor,2,"uint8");
temperature = tmp102Temperature(data,13)
temperature = 24.7500

Change back to the default configuration, as specified in the TMP102 device datasheet.

writeRegister(tempSensor,1,0xA060,"uint16");

Clean Up

When you are finished working with the NI USB-8452, clear the associated device and ni845x objects.

clear tempSensor controller

Helper Function

function T = tmp102Temperature(data,numBits)
% tmp102Temperature Convert TMP102 raw temperature register data to temperature in °C
% 
%  T = tmp102Temperature(data,numBits)
%    data is 1x2 row vector of uint8 values in big-endian order
%    numBits corresponds to the TMP102 temperature mode (12 bits for normal
%    mode, or 13 bits for extended mode)

% TMP102 resolution (°C / count)
resolution = 0.0625;

% Digital temperature output (counts)
numShiftBits = 16-numBits;
digitalT = bitshift(typecast(uint8(fliplr(data)),'int16'),-numShiftBits);

% Temperature in °C
T = double(digitalT) * resolution;
end

See Also

| | | | | | | | |

Related Topics