Documentation

Read temperature from an I2C based sensor using Arduino® Hardware

This example shows how to use Simulink® Support Package for Arduino Hardware to configure and read temperature from an I2C based sensor.

Supported Hardware:

  • Arduino Uno

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Due

  • Arduino Leonardo

  • Other Arduino derived boards having I2C module

Available versions of this example:

Arduino Mega 2560 board: arduino_I2C_temp

The provided model is pre-configured for Arduino Mega 2560. You can run this model on any of the board listed in the "Supported Hardware" section by changing the "Hardware board" parameter as described in Task 2 of this example.

Introduction

Simulink Support Package for Arduino Hardware enables you to use the I2C interface to communicate with I2C based devices.

In this example, you will learn how to communicate to the Sparkfun digital temperature sensor TMP102. This sensor is interfaced with the Arduino board using the I2C bus. By default, it will send a 12-bit temperature value with a resolution of 0.0625 degree Celsius. You can configure this sensor to an Extended mode that provides 13-bit temperature measurements. For more details about the device, refer to the TMP102 datasheet.

This example shows how to program the Arduino board to read the temperature from the sensor using the I2C bus. It also illustrates how to program the Arduino board to initialize the sensor with some advanced settings.

Prerequisites

We recommend completing the Getting Started with Arduino Hardware example, completing the Communicating with Arduino Hardware example to learn about External mode, and reading Support I2C Communication to know more about the I2C protocol.

Required Hardware

To run this example, you will need the following hardware:

Model

The following figure shows the example model:

Task 1 - Connect the TMP102 sensor to the Arduino Hardware

In this task, you will connect the TMP102 sensor to the Arduino board. The Arduino onboard LED at pin 13 will indicate when the TMP102 reading exceeds a specified temperature threshold.

1. Connect the TMP102 sensor to the Arduino board using the following circuit diagram:

    TMP102 pin |   Arduino Mega 2560 pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      VCC      |  3.3 V
      GND      |  GND
      SDA      |  20
      SCL      |  21
      ALT      |  Not Connected
      ADD0     |  GND
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Task 2 - Configure the Model for Supported Arduino Hardware

In this task, you will configure the model for the supported Arduino hardware.

1. Open the Read temperature from an I2C based sensor using Arduino Hardware model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and select your required Arduino hardware from the Hardware board parameter list. Do not change any other settings.

4. Click OK.

Task 3 - Configure the Model to Read Temperature using the I2C Read Block

In this task, you will configure the model to read data from the TMP102 sensor using the I2C Read block.

1. Open the Read temperature from an I2C based sensor using Arduino Hardware model.

2. Configure the I2C Read block:

  • Open the I2C Read block. Notice that the Slave device address parameter of the block is set to '0x48'. The ADD0 pin of the TMP102 sensor is grounded which corresponds to a 7-bit address of 1001000 (0x48 in hexadecimal) according to the TMP102 datasheet.

  • The TMP102 sensor contains multiple registers. On power-up reading data from the device returns the value stored in register 0 - Temperature Register (Read Only). Leave the Read from I2C slave register parameter unchecked as you will read the default Temperature Register in this step.

  • The Temperature Register has a size of 16-bit. It stores the 12-bit temperature value in a left justified 2's complement format. The TMP102 sensor sends data using Big Endian. Select the Data type to 'uint16', Byte ordering to 'Big Endian', Data size (N) to 1, and Sample Time to 0.1. This initiates an I2C read request every 0.1 second.

3. Notice the following points in the model:

  • The Data Type Conversion block is used to convert the read data to int16 to take care of positive and negative temperature.

  • The Gain block is used to right shift the int16 data by 4 bits (division by 16) to right justify the upper 12 bits corresponding to the temperature measurement. The Gain block also multiplies the 12-bit data with the sensor resolution of 0.0625 to get the corresponding temperature in degree Celsius.

  • The two Constant blocks with values of 1 and 0 are used with a Switch block to compare the temperature reading against a threshold of 27 degree Celsius. This glows the Arduino onboard LED at pin 13 when the TMP102 sensor reading exceeds the specified temperature threshold. Apply a temperature change to the sensor to observe this behavior.

Task 4 - Run the Model in External Mode

In this task, you will run the model in External mode to monitor the temperature.

1. Change the Simulation mode on the toolbar of the model to External.

2. Click the Run button on the toolbar of the model to run it in External mode.

3. Notice that the Display block in the model shows the TMP102 sensor temperature reading in degree Celsius.

4. Observe the Arduino onboard LED glow when the Display block in the model shows a temperature above 27 degree Celsius.

5. Change the threshold value in the Switch block according to your ambient temperature and click Apply. See the onboard LED glow when the temperature exceeds the new threshold value.

6. Click the Stop button in the Simulink model to end the External mode execution.

Task 5 - Configure the Model to Initialize the Temperature Sensor to 13-bit Extended Mode

This task shows how to initialize the TMP102 sensor to the 13-bit Extended mode.

1. Open the Initialize and Read temperature from I2C sensor using Arduino Hardware model.

2. Notice the two subsystems in the model:

The One_time_initialization subsystem configures the TMP102 sensor to the 13-bit Extended mode.

The Execution_loop subsystem reads the 13-bit temperature.

  • The One_time_initialization subsystem executes only once at the first step of the execution of the model. For the rest of the execution time, the Execution_loop subsystem runs. This selective execution of subsystems is handled using the Unit Delay and the Enabled Subsystem blocks from the Simulink library.

  • The sensor is configured in Extended mode by setting the EM bit in the Configuration Register to 1 as per the TMP102 datasheet. This implies a value of '0x60B0' to be written to the Configuration Register located at address 1 of the TMP102 sensor.

3. Open the One_time_initialization subsystem.

4. Configure the I2C Write block to write to the Configuration Register of the TMP102 sensor:

  • Open the I2C Write block. The Slave device address parameter of the block is set to '0x48' as earlier.

  • Select the Write to I2C slave register parameter. Then the I2C slave device register address parameter appears in the block.

  • Set the I2C slave device register address parameter to 1. The address of the Configuration Register is 1 as per TMP102 datasheet.

  • Set the Data type parameter to uint16 and Byte order parameter to Big Endian as the Configuration Register has a size of 16-bit whose value needs to be sent over the I2C bus using Big Endian.

5. Notice the following in the One_time_initialization subsystem:

  • The Constant block holds a value of '0x60B0' to be written to the Configuration Register.

  • The I2C Read block connected to the Display block ensures that the correct data is written to the Configuration Register. The I2C Read block uses the same settings as the I2C Write block.

  • The priority of the I2C Write block is set to 1. The priority of the I2C Read block is set to any value higher than 1 to ensure you read the value of the Configuration Register after it is set. To set the Priority of a block, right click on the block > Properties > General > Priority. To know more about block priorities and their impact on block execution order, refer to Set Block Properties.

6. Open the Execution_loop subsystem and observe the following:

  • The Gain block performs a division by 8 that corresponds to a right shift of 3 bits. The division right justifies the 13-bit temperature value.

Task 6 - Run the Model in External Mode

In this task, you will run the model in External mode to monitor the temperature.

1. Change the Simulation mode on the toolbar of the model to External.

2. Click the Run button on the toolbar of the model to run it in External mode.

3. Open the One_time_initialization subsystem.

4. Verify that the Display block shows a value of '0x60B0' in hexadecimal corresponding to the desired Configuration Register value.

5. In the Execution_loop subsystem, monitor the Display block to observe the TMP102 temperature reading in degree Celsius. The Arduino onboard LED glows when the Display block in the model shows a temperature above 27 degree Celsius.

6. Change the threshold value in the Switch block according to your ambient temperature and click Apply. See the onboard LED glow when the temperature exceeds the new threshold value.

7. Click the Stop button in the Simulink model to end the External mode execution.

Other Things to Try

  • Configure the TMP102 sensor to restore the output to 12-bit.

  • Change the conversion rate of the ADC in the sensor using the Configuration Register. Refer to the Continuous-Conversion Mode and the Configuration Register sections in TMP102 datasheet for more details.

  • Refer to the TMP102 datasheet and try to configure TMP102 to run in Shutdown Mode and/or Comparator Mode. Try to change the values of High-and-Low-Limit-Registers.

  • Follow the steps in this example to communicate to other I2C based sensors.

Summary

This example showed how to program your Arduino board to configure and read temperature from an I2C based sensor. In this example you learned how to:

  • Communicate with an I2C based sensor.

  • Program the Arduino board to write data to and read data from specific registers on the I2C based sensor.

  • Initialize the sensor with some advanced settings.

You can use this example as a reference to access other I2C based sensors.

Was this topic helpful?