This example shows how to use the Embedded Coder® Support Package for STMicroelectronics® Discovery Boards to configure and read data from these sensors on the STM32L475VG Discovery (B-L475E-IOT01A) board:
LPS22HB MEMS nano pressure sensor
LSM6DSL 3D accelerometer and 3D gyroscope
LIS3MDL digital output magnetic sensor
HTS221 capacitive digital sensor for relative humidity and temperature
The sensors are interfaced with the STM32L475VG Discovery (B-L475E-IOT01A) board using an I2C bus (module 2).
This example shows how to program the STM32L475VG Discovery (B-L475E-IOT01A) board to read data from these sensors and display the data using external mode.
The example includes these target models:
LPS22HB MEMS nano pressure sensor: stm32l475vg_pressure_sensor
LSM6DSL 3D accelerometer and 3D gyroscope: stm32l475vg_accel_gyro_sensor
LIS3MDL digital output magnetic sensor: stm32l475vg_magnetometer_sensor
HTS221 capacitive digital sensor for relative humidity and temperature: stm32l475vg_humidity_temperature_sensor
These models are preconfigured for the STMicroelectronics STM32L475VG Discovery (B-L475E-IOT01A) board.
Complete the Getting Started with Embedded Coder Support Package for STMicroelectronics Discovery Boards example.
To run this example you need the following hardware:
USB cable
In this task, you will configure the target models to read data from the sensors on the STM32L475VG Discovery (B-L475E-IOT01A) board.
Configure LPS22HB MEMS Nano Pressure Sensor
1. Open the stm32l475vg_pressure_sensor target model.

2. Configure these parameters of the I2C Master Read block in the stm32l475vg_pressure_sensor/Execution Loop subsystem:
Sample time - You can change the block sample time. The block uses a sample time of -1 by default, which initiates an I2C read request every 0.2 seconds.
Slave address - The address is set to 0x5D based on the sensor connections in the STM32L475VG Discovery (B-L475E-IOT01A) board.
For more details, see the LPS22HB and B-L475E-IOT01A datasheets.
3. The model uses the following workflow to read data from the pressure sensor.
a. The model executes the One Time Initialization subsystem only once during the first time step to initialize the pressure sensor.
The model writes the value 0x32 to the CTRL_REG1 (10h) register by using the I2C Master Write block to set the output data rate (ODR) to 25 Hz and enable the block data update (BDU) bit.
b. The model subsequently executes the Execution Loop subsystem.
(i) It uses the I2C Master Read block to read the PRESS_OUT_XL (28h), PRESS_OUT_L (29h), and PRESS_OUT_H (2Ah) registers and convert them to uint32 by using the Data Type Conversion block.
(ii) The model left-shifts the value of the PRESS_OUT_L (29h) register by 8 bits and the PRESS_OUT_H (2Ah) value by 16 bits. It performs OR operation on the PRESS_OUT_XL (28h) and the left-shifted PRESS_OUT_L (29h) and PRESS_OUT_H (2Ah) values to obtain the uint32 pressure value.
Note: The model sets the MSB of the sensor register address (from where it reads the sensor data) to 1 by using the I2C Master Read block. This increments the register address when performing multiple byte I2C read.
(iii) The model follows these steps to convert the pressure value from uint32 to int32:
If the MSB of PRESS_OUT_H (2Ah) register is 1, the model updates the 8 MSBs of the uint32 pressure value with FF.
The model uses the Data Type Conversion block to convert the resulting uint32_t data to int32.
(iii) The model divides the resulting int32 value by the pressure sensitivity value (4096) to obtain the output pressure data (in hPa).
Configure LSM6DSL 3D accelerometer and 3D gyroscope
1. Open the stm32l475vg_accel_gyro_sensor target model.

2. Configure these parameters of the I2C Master Read block in the stm32l475vg_accel_gyro_sensor/Read Accelerometer Data/Execution Loop and stm32l475vg_accel_gyro_sensor/Read Gyroscope Data/Execution Loop subsystems:
Sample time - You can change the block sample time. The block uses a sample time of -1 by default, which initiates an I2C read request every 0.2 second.
Slave address - The address is set to 0x6A based on the sensor connections in the STM32L475VG Discovery (B-L475E-IOT01A) board.
For more details, see the LSM6DSL and B-L475E-IOT01A datasheets.
3. The model uses the following workflow to read data from the accelerometer and gyroscope.
The model follows these steps to read the accelerometer data:
a. The model executes the One Time Initialization subsystem only once during the first time step to initialize the accelerometer.
The model writes the value 0x80 to the CTRL1_XL(10h) register by using the I2C Master Write block to set the output data rate (ODR) to 1.66 kHz.
The model writes the value 0x44 to the CTRL3_C(12h) register by using the I2C Master Write block to set the BDU and IF_INC bits to 1. Setting the IF_INC value to 1 increments the register address when performing multiple byte I2C read.
b. The model uses the I2C Read block to read:
The x-axis (int16) value from the 28h (LSB part) and 29h (MSB part) registers.
The y-axis (int16) value from the 2Ah (LSB part) and 2Bh (MSB part) registers.
The z-axis (int16) value from the 2Ch (LSB part) and 2Dh (MSB part) registers.
c. The model multiplies the x, y, and z values by the linear acceleration sensitivity value (0.061) (
) to obtain the output linear acceleration values in mg. The model further multiplies the output with (9.81/1000) to obtain the linear acceleration values in
.
The model follows these steps to read the gyroscope data:
a. The model executes the One Time Initialization subsystem only once during the first time step to initialize the gyroscope.
The model writes the value 0x80 to the CTRL2_G (11h) register by using the I2C Master Write block to set the output data rate (ODR) to 1.66 kHz and FS to 250 dps.
The model writes the value 0x44 to the CTRL3_C(12h) register by using the I2C Master Write block to set the BDU and IF_INC bits to 1. Setting the IF_INC value to 1 increments the register address when performing multiple byte I2C read.
b. The model uses the I2C Read block to read:
The x-axis (int16) value from the 22h (LSB part) and 23h (MSB part) registers.
The y-axis (int16) value from the 24h (LSB part) and 25h (MSB part) registers.
The z-axis (int16) value from the 26h (LSB part) and 27h (MSB part) registers.
c. The model multiplies the x, y, and z values by the angular rate sensitivity value (8.75) (FS = 250 dps) to obtain the output angular rate values in mdps. The model further multiplies the output with (0.001) to obtain the angular rate data in dps.
Configure LIS3MDL digital output magnetic sensor
1. Open the stm32l475vg_magnetometer_sensor target model.

2. Configure these parameters of the I2C Master Read block in the stm32l475vg_magnetometer_sensor/Execution Loop subsystem:
Sample time - You can change the block sample time. The block uses a sample time of -1 by default, which initiates an I2C read request every 0.2 seconds.
Slave address - The address is set to 0x1E based on the sensor connections in the STM32L475VG Discovery (B-L475E-IOT01A) board.
For more details, see the LIS3MDL and B-L475E-IOT01A datasheets.
3. The model uses the following workflow to read data from the magnetic sensor:
a. The model executes the One Time Initialization subsystem only once during the first time step to initialize the magnetic sensor.
The model writes:
The value 0x58 to the CTRL_REG1 (20h) register to set the x and y axis operating mode to high performance.
The value 0x00 to the CTRL_REG3 (22h) register to set the operating mode to continuous-conversion mode.
The value 0x08 to the CTRL_REG4 (23h) register to set the z axis operating mode to high performance.
The value 0x40 to the CTRL_REG5 (24h) register to enable the block data update (BDU) bit.
b. The model subsequently executes the Execution Loop subsystem.
(i) The model uses the I2C Read block to read:
The x-axis (int16) value from the 28h (LSB part) and 29h (MSB part) registers.
The y-axis (int16) value from the 2Ah (LSB part) and 2Bh (MSB part) registers.
The z-axis (int16) value from the 2Ch (LSB part) and 2Dh (MSB part) registers.
Note: The model sets the MSB of the sensor register address (from where it reads the sensor data) to 1 by using the I2C Master Read block. This increments the register address when performing multiple byte I2C read.
(ii) The model divides the resulting x, y, and z values by the sensitivity value (6842) to obtain the output magnetometer data (in Gauss).
Configure HTS221 capacitive digital sensor for relative humidity and temperature
1. Open the stm32l475vg_humidity_temperature_sensor target model.

2. Configure these parameters of the I2C Master Read blocks in the stm32l475vg_humidity_temperature_sensor/Execution Loop/Read Temperature Data/Get Register Data and stm32l475vg_humidity_temperature_sensor/Execution Loop/Read Humidity Data/Get Register Values subsystems:
Sample time - You can change the block sample time. The block uses a sample time of -1 by default, which initiates an I2C read request every 0.2 second.
Slave address - The address is set to 0x5F based on the sensor connections in the STM32L475VG Discovery (B-L475E-IOT01A) board.
For more details, see the HTS221 and B-L475E-IOT01A datasheets.
3. The model uses the following workflow to read data from the relative humidity and temperature sensors.
a. The model executes the One Time Initialization subsystem only once during the first time step to initialize the relative humidity and temperature sensors.
The model writes 0x85 to the CTRL_REG1 (20h) register by using the I2C Master Write block to enable the PD and the block data update (BDU) bits and set the output data rate (ODR) to 1 Hz.
b. The model subsequently executes the Execution Loop subsystem.
The following steps describe how the model reads the temperature sensor data:
(i) The calibration temperature values consist of unsigned 10 bits. The 2 MSB bits are present in the 35h register and the 8 LSB bits are present in the 32h and 33h registers.
The model uses the I2C Master Read block to read the MSBs of T0_degC_x8 and T1_degC_x8 from the 35h register and converts them to uint16 values using the Data Type Conversion block. It left-shifts the resulting uint16 values by 8 bits.
The model uses the I2C Master Read block to read the LSBs of T0_degC_x8 from 32h register and LSBs of T1_degC_x8 from 33h register. The model performs bitwise OR operation on the resulting LSB and MSB values to obtain the T0_degC_x8 and T1_degC_x8 values.
The model divides these values by 8 to obtain the T0_degC and T1_degC calibration temperature values.
(ii) The I2C Master Read block reads:
The T_OUT (int16) value from the 2A (LSB part) and 2B (MSB part) registers.
The T0_OUT (int16) value from the 3C(LSB part) and 3D(MSB part) registers.
The T1_OUT (int16) value from the 3E(LSB part) and 3F(MSB part) registers.
(iii) The model computes the temperature sensor output (in Celsius) using this equation:
Temperature in degC (T_degC) = (((T1_degC - T0_degC) * (T_OUT - T0_OUT)) / (T1_OUT - T_OUT)) + T0_degC
The following steps describe how the model reads the relative humidity sensor data:
(i) The I2C Master Read block:
Computes the H0_rH_x2 and H1_rH_x2 values by reading the data from the 30h and 31h registers and dividing them by 2.
Reads the H1_T0_OUT (int16) value from the 3A (LSB part) and 3B (MSB part) registers.
Reads the H0_T0_OUT (int16) value from the 36h (LSB part) and 37h (MSB part) registers.
Reads the H_OUT (int16) value from the 28h (LSB part) and 29h (MSB part) registers.
(ii) The model computes the relative humidity sensor (percentage) output using this equation:
Relative Humidity in percentage (H_rh) = (((H1_rH_x2 - H0_rH_x2) * (H_OUT - H0_T0_OUT)) / (H1_T0_OUT - H0_T0_OUT)) + H0_rH_x2
In this task, you will configure the hardware and monitor the sensor data obtained by the target model.
1. In the Modeling tab, click Model Settings or press Ctrl+E to open the 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. The Display blocks in the target model display the sensor output.
5. Click the Stop button in the Hardware tab to end the Monitor and Tune execution.
Exchanging WiFi Data and Using ThingSpeak with STM32L475VG Discovery Board