MATLAB Examples

Read data from an I2C based sensors using STMicroelectronics Nucleo board

This example shows how to use Simulink® Coder Support Package for STMicroelectronics Nucleo Boards to configure and read data from an I2C based sensors.

Available versions of this example:

STM Nucleo board: stmnucleo_I2C_temp

STM Nucleo board: stmnucleo_I2C_temp_extendedMode

STM Nucleo board: stmnucleo_AccelGyro_extmode

The provided model is pre-configured for STMicroelectronics Nucleo F401RE board. You can configure this model for other supported Nucleo boards by selecting them as the "Hardware board" on the Hardware Implementation pane of the Model Configuration Parameters as described in Task 2 of this example.

Contents

Introduction

Simulink Coder Support Package for STMicroelectronics Nucleo Boards 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 STMicroelectronics Nucleo 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.

In this example you will also learn how to communicate to the Sparkfun Digital 6DOF accelerometer and gyrometer sensor. This sensor is interfaced with the STMicroelectronics Nucleo board using the I2C bus. It will send X,Y and Z axis data for Accelerometer and Gyrometer sensor present in the sensor board. These sensors need to be configured separately and acts as two different I2C slave registers. For more details about the device, refer to the ADXL345 datasheet and ITG-3200 datasheet.

This example shows how to program the STMicroelectronics Nucleo board to:

1. Read the temperature from the TMP102 sensor using the I2C bus. It also illustrates how to program the STMicroelectronics Nucleo board to initialize the sensor with some advanced settings.

2. Read the Accelerometer and Gyrometer data from ITG3200/ADXL345 sensors using I2C Master read block and display it.

Prerequisites

We recommend completing the Getting Started with STMicroelectronics Nucleo board example, completing the Communicating with STMicroelectronics Nucleo board example

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 STMicroelectronics Nucleo board

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

1. Connect the TMP102 sensor to the STMicroelectronics Nucleo board using the following table:

    TMP102 pin |   STMicroelectronics Nucleo F401RE pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      VCC      |  3.3 V
      GND      |  GND
      SDA      |  14
      SCL      |  15
      ALT      |  Not Connected
      ADD0     |  GND
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
   Most of the Nucleo board uses <matlab:web('https://developer.mbed.org/platforms/','-browser')
   MBED> abstraction and thus implements
   Arduino uno header which means same pins are used for designated
   functionality. However in case of Nucleo F031K6, Arduino nano headers
   are used by <matlab:web('https://developer.mbed.org/platforms/','-browser')
   MBED> abstraction.
    TMP102 pin |   STMicroelectronics Nucleo F031K6 pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      VCC      |  3.3 V
      GND      |  GND
      SDA      |  4
      SCL      |  5
      ALT      |  Not Connected
      ADD0     |  GND
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Task 2 - Configure the Model for Supported STMicroelectronics Nucleo board

In this task, you will configure the model for the supported STMicroelectronics Nucleo board

1. Open the Read temperature from an I2C based sensor using STMicroelectronics Nucleo board 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 STMicroelectronics Nucleo board from the Hardware Implementation->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 Master Read block.

1. Open the Read temperature from an I2C based sensor using STMicroelectronics Nucleo board 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 STMicroelectronics Nucleo board 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 and in Configuration Parameters > Hardware Implementation > Target Hardware Resources > External Mode > COM Port enter the COM port of the serial interface on Windows. For reference look at the figure below where External mode on Nucleo F401RE board is configured:

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

5. Observe the STMicroelectronics Nucleo F401RE onboard LED glow 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.

Model

The following figure shows the example model for next task:

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 STMicroelectronics Nucleo board 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 Master Write block to write to the Configuration Register of the TMP102 sensor:

  • Open the I2C Master Write block. The Slave device address parameter of the block is set to '0x48' as earlier.
  • Select the Enable register access parameter. Then the Slave 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 Slave 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 Master 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.
  • Similar to the Task 4, run the model in External Mode.

Task 6 - Connect the ITG3200/ADXL345 sensor to the STMicroelectronics Nucleo board

In this task, you will connect the ITG3200/ADXL345 sensor to the STMicroelectronics Nucleo board.

1. Connect the ITG3200/ADXL345 sensor to the STMicroelectronics Nucleo board using the following table:

  ITG3200/ADXL345 pin |   STMicroelectronics Nucleo F401RE pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      VCC             |  3.3 V
      GND             |  GND
      SDA             |  14
      SCL             |  15
     INT0             |  Not Connected
     INT1             |  Not Connected
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

2. Similar to Task 2, now Configure the Model for Supported STMicroelectronics Nucleo board.

Model

The following figure shows the example model for next task:

Task 7 - Configure the Model to Read sensor data using the I2C Read Block

This task shows how to initialize the ITG3200/ADXL345 pin sensor to the Measure mode.

1. Open the Read the sensor data using I2C Master Read blocks on STMicroelectronics Nucleo board model.

2. Notice the two subsystems in the model:

The Get_Accel_data subsystem configures the ADXL345 sensor to read accelerometer data.

  • In the Get_Accel_data subsystem the sensor is configured in Measure mode by setting the Measure bit in the POWER_CTL Register to 08 as per the ADXL345 datasheet. This implies a value of '0x08' to be written to the POWER_CTL Register located at address 0x2D of the ADXL345 sensor. This value is written using a constant block outside the subsystem.

The Get_Gyro_data subsystem configures the ITG3200 sensor to read Gyrometer data.

  • In the Get_Gyro_data subsystem the sensor is configured in PLL with X Gyro reference by setting the first bit in the POWER Management Register to 01 as per the ITG-3200 datasheet. This implies a value of '0x01' to be written to the POWER Management Register located at address 0x3E of the ITG-3200 sensor. This value is written using a constant block outside the subsystem.

3. Open the Get_Accel_data subsystem.

4. Configure the I2C Master Write block to write to the POWER_CTL Register of the ADXL345 sensor:

  • Open the I2C Master Write block. The Slave device address parameter of the block is set to '0x53' as this is the slave address.
  • Select the Enable register access parameter. Then the Slave register address parameter appears in the block.
  • Set the I2C slave device register address parameter to 0x2D. The address of the POWER_CTL Register is 0x2D as per ADXL345 datasheet.
  • Set the Slave byte order parameter to little Endian as the POWER_CTL Register has a size of 8-bit whose value needs to be sent over the I2C bus using little Endian.

5. Notice the following in the Get_Accel_data subsystem:

  • The input port is supplied with a value of '0x08' to be written to the POWER_CTL Register.
  • The I2C Master Read block connected to the Display block ensures that the correct data is written to the POWER_CTL Register. The I2C Read block uses the same settings as the I2C Write block.

6. Open the Get_Gyro_data subsystem.

7. Configure the I2C Master Write block to write to the POWER Management Register of the ITG3200 sensor:

  • Open the I2C Master Write block. The Slave device address parameter of the block is set to '0x68' as this is the slave address.
  • Select the Enable register access parameter. Then the Slave register address parameter appears in the block.
  • Set the I2C slave device register address parameter to 0x3E. The address of the POWER Management Register is 0x3E as per ITG3200 datasheet.
  • Set the Slave byte order parameter to little Endian as the POWER Management Register has a size of 8-bit whose value needs to be sent over the I2C bus using little Endian.

8. Notice the following in the Get_Gyro_data subsystem:

  • The input port is supplied with a value of '0x01' to be written to the POWER Management Register.
  • The I2C Master Read block connected to the Display block ensures that the correct data is written to the POWER Management Register. The I2C Read block uses the same settings as the I2C Write block.
  • For both the subsystems 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 POWER_CTL/POWER Management 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.

9. Similar to the Task 4, run the model in External Mode.

Note:- In the case of external mode default 'serial transmit'(USBTX) and serial receive(USBRX) is used. The corresponding GPIO pins for USBTX and USBRX is D1 and D0 respectively. This will require editing other serial transmit and serial receive pin in case of running SCI blocks in external mode. To edit these settings go to the "Simulation > Model Configuration Parameters > Hardware Implementation > Target Hardware Resources > SCI" and edit appropriate Pins for Receive Pin and Transmit Pin. If you would like to build and deploy the code on hardware without running external mode, keep the default settings as it is in the Simulation > Model Configuration Parameters > Hardware Implementation > Target Hardware Resources > SCI.

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.
  • Refer to the ADXL345 datasheet and try to configure ADXL345 in different g Range settings using DATA FORMAT register.
  • Refer to the ITG3200 datasheet and try to configure ITG3200 in different CLK_SEL modes.
  • Follow the steps in this example to communicate to other I2C based sensors.

Summary

This example showed how to program your STMicroelectronics Nucleo board to configure and read sensor data from an I2C based sensors. In this example you learned how to:

  • Communicate with an I2C based sensor.
  • Program the STMicroelectronics Nucleo 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.