Main Content

Read Data from I2C-Based Sensors Using STMicroelectronics Nucleo Boards

This example shows how to use the Simulink® Coder™ Support Package for configuring and reading data from an I2C-based sensor using an STMicroelectronics® Nucleo board.

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 sends 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 the accelerometer and gyrometer sensor present in the sensor board. These sensors need to be configured separately and acts as two different I2C peripheral 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:

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

  • Read the accelerometer and gyrometer data from ITG3200/ADXL345 sensors using I2C Controller read block and display it.

Available models of this example:

STM Nucleo board: I2CTemperature

STM Nucleo board: I2CTemperatureExtendedMode

STM Nucleo board: AccelerometerGyrometerExtmode

The model provided is pre-configured for STMicroelectronics Nucleo F401RE board. You can configure this model for other supported Nucleo boards by browsing to Configuration Parameters > Hardware Implementation > Hardware board and selecting the required board.

Prerequisites

We recommend completing the following examples:

Required Hardware

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

Model

The following figure shows the example model:

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

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 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 MBED abstraction.

    TMP102 pin |   STMicroelectronics Nucleo F031K6 pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      VCC      |  3.3 V
      GND      |  GND
      SDA      |  4
      SCL      |  5
      ALT      |  Not Connected
      ADD0     |  GND
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Configuring the Model for Supported STMicroelectronics Nucleo Board

In this task, you 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, open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.

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.

Configuring 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 Controller 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 Peripheral 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 peripheral 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.

Run the Model in Normal Mode with Simulink IO

In this task you will simulate the model in Normal mode with Simulink IO to validate the design before generating code and deploying the model on the Nucleo board.For more information see Communicate with Hardware Using Connected IO.

To simulate the model in Normal Mode with Simulink IO:

1. Enable Simulink IO as described in How to Enable Connected IO.

2. Click the Run button on the toolbar of the model to run it in Normal mode. During simulation, the Display block displays the temperature reading from the TMP102 sensor in degree celsius. When the temperature reading exceeds the threshold value of 27 degree celsius, the LED on Nucleo board starts glowing.

3. Click the Stop button in the Simulink model to end the Normal mode execution.

Configure the Hardware and Model for Monitoring and Tuning

In this task, you will configure the hardware and perform Monitor and Tune action to monitor the temperature.

1. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.

2. Navigate to Hardware Implementation > Target Hardware Resources > External Mode > COM Port and enter the COM port of the serial interface on Windows®.

3. Open the Hardware tab and click Monitor & Tune.

4. 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 Hardware tab to end the Monitor and Tune execution.

Model

The following figure shows the example model for next task:

Configuring 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 Controller Write block to write to the Configuration Register of the TMP102 sensor:

  • Open the I2C Controller Write block. The Peripheral device 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 I2C peripheral device register address parameter to 1. The address of the Configuration Register is 1 as per TMP102 datasheet.

  • Set the Peripheral 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 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 Specify 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 Normal Mode with Simulink IO.

  • Similar to the Task 5, perform Monitor and Tune.

Connecting 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, configure the model for supported STMicroelectronics Nucleo board.

Model

The following figure shows the example model for next task:

Configuring 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 Controller 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 Controller Write block to write to the POWER_CTL Register of the ADXL345 sensor:

  • Open the I2C Controller Write block. The Peripheral device address parameter of the block is set to '0x53' as this is the peripheral address.

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

  • Set the I2C peripheral device register address parameter to 0x2D. The address of the POWER_CTL Register is 0x2D as per ADXL345 datasheet.

  • Set the Peripheral 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 Controller 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 Controller Write block to write to the POWER Management Register of the ITG3200 sensor:

  • Open the I2C Controller Write block. The Peripheral device address parameter of the block is set to '0x68' as this is the peripheral address.

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

  • Set the I2C peripheral device register address parameter to 0x3E. The address of the POWER Management Register is 0x3E as per ITG3200 datasheet.

  • Set the Peripheral 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 Controller 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 Specify Block Properties.

9. Similar to the Task 4, run the model in Normal Mode with Simulink IO.

10. Similar to the Task 5, perform Monitor and Tune.

Note: In the case of Monitor and Tune 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 Monitor and Tune. 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 Monitor and Tune, 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.