MATLAB Answers


Simulink, Arduino, and I2C accelerometer

Asked by Daniel
on 28 Mar 2014
Latest activity Commented on by Sayat Dakarimov on 7 Mar 2019
I am trying to develop a control system using Simulink's model-based design approach, and I need to use the data from an accelerometer. Ultimately this control system will be downloaded onto/implemented on an Arduino Mega microcontroller.
My question: How can I, in Simulink, recieve the accelerometer data? I have found out that the accelerometer uses I2C communication. I notice that there's a "Serial recieve" block in the Simulink Arduino support package, but I cannot figure out how to make it work.
This video shows exactly what I want to be able to do:
Any help would be GREATLY appreciated!

  1 Comment

Sign in to comment.

3 Answers

Answer by Omer Ikram on 7 Apr 2014
Edited by Omer Ikram on 7 Apr 2014

Hi there, I am also stuck, same place as you are. Serial Receive will not work for you. You need a block which can read I2C. However no I2C read/write block comes with Arduino Support package. I am trying to write an sFunction for it but stuck with the compatibility of the library files. "Wire.h" which is used for I2C communication in Arduino SDK uses "inttypes.h" and this header file is now obsolete and new compilers such as Windows SDK 7.1 can not recognize them. I dont know how to solve this problem and a bit of help will be appreciated.
However, if you want to use only accelerometer data out. you can use the Matlab code to read the receiving data.
In the Arduino code export the data to the serial port 0
by write example lines in your code
you should be able to receive it by listening the COM and embed the matlab code in the Simulink.


on 8 Apr 2014
That would be ideal if so. I don't really see the point of the support package if it isn't possible to interface with other devices. Certainly not in our case anyway. Is there any way that you know to obtain the arduino code generated by Simulink to embed in your own sketch?
The I2C lib I use is here.
:D I think our interests perfectly align. I am also looking for a ways to use the generated code by Simulink to embed in our own code for arduino. But that is a later part, if I am not able to read I2C device with-in Simulink.
Currently the problem is that I can not find any I2C library which do not use inttypes.h header file.
inttypes.h file is not compatible with current version of c compilers which are used by Matlab 2013
An-other approach is to use any digital pin as SDA and SCL and use it for reading and writing the data but i feel it as a waste of resources.
on 11 Apr 2014
I've actually resorted to using a separate arduino and sending the data received from the gyro over serial to the one running from simulink in the end. But if you find a better way of doing this, please let me know!

Sign in to comment.

Answer by Mohamed
on 19 May 2014

What is wrong with that one , can it be used ? I2C block for ArduinoIO simulink package


Sign in to comment.

Answer by John
on 29 May 2014

I have the same problem - I want to use a mpu 9150 in my project


Hi Guys, I am trying to reed the MPU 6050. I wondered if they had found a solution to your communication. Any help will be appreciated.

Sign in to comment.