Main Content

Read Temperature from 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

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino MKR ZERO

  • Arduino Nano 33 IoT

  • Arduino Nano 33 BLE Sense

  • 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 Step 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

Before you start with this example, we recommend you complete:

Required Hardware

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

Step 1 - Connect TMP102 sensor to Arduino Hardware

In this section, 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
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Step 2 - Configure Simulink Model for Supported Arduino Hardware

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

1. Open the arduino_I2C_temp Simulink 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.

Step 3 - Configure Simulink Model to Read Temperature Using I2C Controller Read Block

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

1. Open the arduino_I2C_temp Simulink model.

2. Configure the I2C Controller Read block:

  • Open the I2C Controller Read block. Notice that the Peripheral 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 Enable register access 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.

Step 4 - Run Simulink Model in Normal Mode with Connected I/O

You can simulate the model in Normal mode with Connected I/O to validate the model design before generating the code and deploying the model on Arduino. In this example, when you simulate the model in Connected I/O, the model reads temperature from the TMP102 sensor that is interfaced with Arduino. This communication between the model and the Arduino hardware does not require any code generation or model deployment, thus accelerating the simulation process. For more information on Connected I/O, see Communicate with Hardware Using Connected IO.

To simulate the model in Normal mode with Connected I/O:

1. On the Hardware tab of the Simulink model, in the Mode section, select Connected IO and then click Run with IO.

During simulation, the Display block displays the temperature reading from the TMP102 sensor in degree celsius. When the temperature reading exceeds a threshold value of 27 degree celsius, the LED on Arduino starts glowing.

2. On the Simulink model toolbar, click Stop to stop the simulation.

Step 5 - Run Simulink Model in External Mode (Monitor and Tune)

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

1. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

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

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

4. 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.

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

Step 6 - Configure Simulink Model to Initialize Temperature Sensor to 13-bit Extended Mode

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

1. Open the arduino_I2C_temp_init Simulink 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 reads the 13-bit temperature.

  • The One_time_initialization subsystem executes only once at the first step of the execution of the model. 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 Controller Write to write to the Configuration Register of the TMP102 sensor:

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

  • Select the Enable register access parameter. Then the Peripheral register address parameter appears in the block.

  • Set the Peripheral 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 Controller Read block connected to the Display block ensures that the correct data is written to the Configuration Register. The I2C Controller Read block uses the same settings as the I2C Controller Write block.

  • The priority of the I2C Controller Write block is set to 1. The priority of the I2C Controller 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 Specify Block Properties.

6. Observe the following:

  • This subsystem resembles the arduino_I2C_temp model.

  • 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.

Step 7 - Run Simulink Model in External Mode (Monitor and Tune)

Tip: Before you simulate the model in External mode, you can simulate the model in Normal mode with Connected I/O as described in Step 4 - Run the Model in Normal Mode with Connected I/O. Simulating the model in Connected I/O enables you to validate the model design before generating the code and deploying the model on Arduino.

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

1. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

2. Open the One_time_initialization subsystem.

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

4. 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.

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.

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.