Main Content

Measure Temperature from I2C Device on Arduino Hardware

This example shows how to use the MATLAB® Support Package for Arduino® Hardware and the I2C interface to communicate with I2C devices.

Overview of TMP102 Temperature Sensor

This example uses TMP102, a two-wire serial output digital sensor, which can read temperature up to a resolution of 0.0625 degree in Celsius. You can also read data from the device in Extended mode with higher measurement limit.

Hardware Setup

1) Connect the SDA, SCL, GND and VCC pins of the sensor to the corresponding pins on Arduino hardware. This examples connects SDA and SCL pins to A4 and A5 on Arduino Uno board. If you are using a different board, check the correct pins before connection.

2) Securely connect the power line of the I2C sensor.

Create an I2C Device Object

1) Create an arduino object and include the I2C library.

a = arduino('COM9', 'Uno', 'Libraries', 'I2C');

2) Scan for available I2C addresses.

addrs = scanI2CBus(a)
addrs = 1×1 cell array

Note the address of the temperature sensor. You will use it to create the I2C device object.

3) Create the I2C device object

tmp102 = device(a,'I2CAddress',0x48)

The bus defaults to 0. If you are using the dedicated I2C interfaces(SDA1, SCL1) on Due board, for example, make sure to set bus to 1.

Read Temperature Value

The sensor's temperature reading is digitized into 12 bits in Normal mode with 8 bits in MSB and 4 bits in LSB. Each LSB equals 0.0625 degrees in Celsius. Write the register address to read from first and then read two bytes of data from it. Use 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.

write(tmp102, 0x0, 'uint8');
data = read(tmp102, 2, 'uint8');
temperature = tmp102Temperature(data,12)
temperature = 26.6250

Read Temperature with Higher Measurement Limit

With the TMP102 sensor's extended mode, you can measure temperature above 128 degrees by using 13 bits. To do so, you need to write value 'B060' in hex to configuration register at address 1.

writeRegister(tmp102, 1, 0xB060, 'uint16');

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

write(tmp102, 0x0, 'uint8');
data = read(tmp102, 2, 'uint8');
temperature = tmp102Temperature(data,13)
temperature = 26.6250

To change back the default configuration, type

writeRegister(tmp102, 1, 0xA060, 'uint16');

Clean Up

Once the connection is no longer needed, clear the associated object.

clear tmp102 a

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;