Clear Filters
Clear Filters

I2C sensor on USART2 Serial communication not working

8 views (last 30 days)
I am using the embedded coder support package in simulink to interface with my onboard MEMS acceleration sensor (LSM303AGR) on my STM32F411VET discovery board. I could successfully connect and run simple LED program through USART2 on simulink block and it returns button state values as well on display, while running in external mode.
I am moving on to more complicated communication, where i am accessing the I2C sensor via USART2 but I am not able to establish a communication. I have I2C read and I2C write blocks inside the embedded coder package which i am using along with correct read and write addresses for the sensor.
However, My I2C sensor will not respond over USART2, is there a step which I am missing?
How does one transmit and receive I2C sensor data over USART2 serial communication?
Your help would be appreciated.

Answers (2)

Vikky Patil
Vikky Patil on 14 Sep 2023
Edited: Vikky Patil on 14 Sep 2023
Hello Sanath,
We assume you want to read the data from the sensor in Simulink using external mode. The sensor is connected via an I2C interface with the controller. The interface used for external mode is serial.
We don’t have an exact example on the same hardware, but you can refer to an example shipped in 23b demonstrating reading sensor data(which is connected via an I2C interface with the controller) and sending the results over CAN to another controller.
Note : The sensor in the example is to read humidity and temperature.
You can refer to the example for understanding how to read the data using the I2C block and instead of sending it over CAN using CAN write a block you can connect it to an output port and run the model in external mode.
if issue persist, please contact technical support: Contact Us - MATLAB & Simulink (

Sanath on 15 Sep 2023
@Vikky Patil based on two examples, one for I2C communication and another about the specific sensor, LSM303AGR, I made a simulink model, but it does not return any value. what do you think could be the issue?
Vikky Patil
Vikky Patil on 21 Sep 2023
It depends upon the rate at which you are logging and number of data bytes getting logged. If you are seeing data drops in external mode use external FTDI and try to increase the baud rate to its max.
Sanath on 28 Sep 2023
@Vikky Patil Thank you so much for all your help. My model is finally working with all values for x,y and z axis. Your suggestion and explainations really helped me move forward.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!