Simulink, Arduino, and I2C accelerometer

7 views (last 30 days)
Daniel
Daniel on 28 Mar 2014
Commented: Sayat Dakarimov on 7 Mar 2019
Hi!
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:
https://www.youtube.com/watch?v=04QI08JNSnw
Any help would be GREATLY appreciated!

Answers (3)

Omer Ikram
Omer Ikram on 7 Apr 2014
Edited: 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
Serial.println(Xdata);
Serial.println(Ydata);
Serial.Println(Zdata);
you should be able to receive it by listening the COM and embed the matlab code in the Simulink.
  5 Comments
Omer Ikram
Omer Ikram on 10 Apr 2014
: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.
Alex
Alex 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.


Mohamed
Mohamed on 19 May 2014
What is wrong with that one , can it be used ? I2C block for ArduinoIO simulink package

John
John on 29 May 2014
I have the same problem - I want to use a mpu 9150 in my project
  2 Comments
Cristian Acosta
Cristian Acosta on 25 Jan 2016
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.

Community Treasure Hunt

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

Start Hunting!