Code covered by the BSD License  

Highlights from
I2C block for ArduinoIO simulink package

5.0
5.0 | 2 ratings Rate this file 116 Downloads (last 30 days) File Size: 142 KB File ID: #41602 Version: 1.7
image thumbnail

I2C block for ArduinoIO simulink package

by

Zhuo Li (view profile)

 

02 May 2013 (Updated )

This is an augmenting block that enables the ArduinoIO package with the I2C bus read capability.

| Watch this File

File Information
Description

A new method i2cRead() is added to the arduino.m file. The adiosrv.ped file is modified accordingly. So, with this add-on, your Arduino Uno can speak in I2C through Simulink. The I2C read functionality can be accessed either from Matlab command window or Simulink.
1) In command window, a sample syntax is
 a.i2cRead(hex2dec('5A'), hex2dec('07'))
 where '5A' denotes the 7bit device address in hex format 0x5A, and '07' denotes the register address '0x07'.
2) In Simulink, you can find the Arduino I2C read block in the arduino_io_lib. When you put the Arduino I2C Read block into your block diagram, be sure to put the correct device address and register address to the parameter field. Help doc is available for instruction inside this download.

Acknowledgements

Legacy Matlab And Simulink Support For Arduino inspired this file.

Required Products Simulink
MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Other requirements Arduino IDE
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (26)
24 Jun 2015 Michael Chen

Hi,
I use two arduino uno boards to try i2c receive block.I use the example code(from http://www.arduino.cc/en/Tutorial/MasterWriter) to transmit data via i2c and use i2c receive block to receive data.
All of the data I got is 14800, I don't know what's wrong with it.Another issue I am not for sure is the register address. Does it imply to use register address #4 from the following code.
Wire.begin(4); // join i2c bus with address #4

Comment only
16 Jun 2015 Zhuo Li

Zhuo Li (view profile)

Hi michael,
By looking at your error msg (xx.tlc) I guess you are trying to run the ArduinoIO package as real time workshop (RTW) or the target type application. However, to my knowledge, this package is designed for 'normal' simulation instead of 'External' or code gen. It runs on a server-client (PC-Arduino) mechanism through serial communication. For your case, if you really need to run target, you can try the Matlab Arduino support packge, http://www.mathworks.com/hardware-support/arduino-matlab.html

Comment only
16 Jun 2015 Michael Chen

I used the Servo Write block from the Arduino IO Library to construct a model and built it, but failed.From the error message as following ,I can't find the msfun_arduino_servowrite.tlc file from the download files.

The corresponding 'msfun_arduino_servowrite.tlc' file for the MATLAB S-function 'msfun_arduino_servowrite' in block 'arduinouno_servocontrol_sweep_1/Servo Write' must be located in the current working directory, the MATLAB S-function directory 'C:\MATLAB\Targets\R2012b\ArduinoIO\simulink', or the directory 'C:\MATLAB\Targets\R2012b\ArduinoIO\simulink\tlc_c'

Comment only
07 Mar 2015 Zhuo Li

Zhuo Li (view profile)

Hi AG, please refer to the readme file inside this download. hex2dec('5A') and hex2dec('07') refers to the device address and register address of your I2C sensor MLX90614. I met your problem before as well (reading 15xxx 14xxx) but not sure what was exactly happening. When I tried on another machine it worked. Then, I come back to my machine and re-installed my package and it recovered.

Comment only
07 Mar 2015 Zhuo Li

Zhuo Li (view profile)

Hi sunnychahal9, please refer to the readme file inside this download. The installation instruction is there. Howevere, I noticed you are using the R2014 version, and according to JAE, this package may not be compatible with 2014a. So, I don't have a quick solution for you at this moment, but I can try to update this package in the future if possible.

Comment only
07 Mar 2015 Zhuo Li

Zhuo Li (view profile)

Hi J.A.E, Thanks for your feedback. Sorry to know that they no longer support R2014a version. I saw that Matlab R2014a has added much more target options for real-time workshop (RTW), including Arduino Uno and DUE, etc. Maybe you can try the target implementation to better fulfill your needs.

Comment only
03 Mar 2015 sunnychahal9

Hi, I'm trying to use this but when I open up the "I2c_test" all the blocks are red and they say: "Failed to load library 'arduino_io_lib' referenced by 'I2c_test/Arduino I2C Read'" I tried to install it via the support package installer but it wouldn't recognize the file. How do I install this file?

Thanks (using R2014a)

Comment only
11 Feb 2015 chfakht chfakht

Simulink with arduino : data acquisition

Hello all ,
i'm working on data acquisition from a sensor attached to the arduino : MPU6050 using a model in simulink , i have installed the hardware support in simulink but i don't know how to start building the model , the model must read the data from arduino by deploying it into the arduino
my connection arduino <=> MPU6050 is :

Arduino MPU 9150
3.3V VCC
GND GND
A5 SCL
A4 SDA
DIGITAL 2 INT
GND AD0

any help will be appreciated
thanks

Comment only
21 Sep 2014 A G

A G (view profile)

I am using MLX-90614 ir temperature sensor and this package is compatible with the version of matlab that i am using .
But i want to know what does this command
a.i2cRead(hex2dec('5A'), hex2dec('07'))

really do.

When i run this command it return values like 15000,14990 and just like these values. What does these values mean? I want teperature value in celcius. What should i do? can any one help?

Comment only
15 Sep 2014 J.A.E.

J.A.E. (view profile)

Some more information: I use Matlab R2014a. The Arduino IO package is not compatible with this version of Matlab.

Is this I2C block compatible with Matlab R2014a?

Comment only
15 Sep 2014 J.A.E.

J.A.E. (view profile)

Hi,

Looks nice,

but is it easy to make it compatible with the Arduino DUE or the Arduino DUE matlab package (http://www.mathworks.nl/hardware-support/arduino-simulink.html)?

I'm mostly interest in the simulink block. I tried it on my computer, but the ArduinoIO package doesn't work with Arduino DUE (or doesn't work if the Arduino DUE matlab package also is installed on the computer).

I hope you can help me.

Comment only
31 May 2014 addie irawan

Hi,
Good sharing Ingmar

Comment only
06 May 2014 Ingmar

Ingmar (view profile)

Hi naxokite, It seems like you have the same problem as me. My solution was to change the arduino.m file provided by Zhuo Li.

Open the arduino.m file and scroll down to the part that reads out the I2C master. It is right around line 1539. You will find a line that says fwrite... at line 1581. This line sends the address to the arduino. It seems like you have to execute this line two times. Do not ask me why. If you copy that line and past it right behind it on line 1582 it should work.

Good luck with the project!

Comment only
06 Feb 2014 naxokite

Great package! Thank you very much! I am having some trouble reading the temperature with a.i2cRead(hex2dec('5A'), hex2dec('07')) and I get the temperature delayed of one or something like that, each time I put my hand in fron t of the IR sensor, I have to execute that line of code twice in order to get 30 degrees because the first one gives me the ambient temperature and viceversa when I put my hand away, the first one still gives me the hand temperature(no matter how much I wait) and the second one gives me the real temperature, any help? Thanks!!

23 Jan 2014 Zhuo Li

Zhuo Li (view profile)

Hi Ingmar, very sorry for the long waiting. I was traveling overseas last month. Last time when I was about to update it, I found the file was not in my hand. Now, I just submitted the update.

Honestly, I tested repeatedly but still didn't completely eliminate the timeout bug. But by setting a longer serial waiting time and adding more checking statements, the timeout very seldom shows up and can be tolerated for the code to proceed.

Regarding your problem, I guess maybe you can put a serial flush after your a.ITG3200Read function instead of an actual serial write. But anyways, as long as it works for you, you can keep your way. Good luck with your project.

Comment only
29 Dec 2013 Ingmar

Ingmar (view profile)

Hi Zhuo, big thanks for the quick response. I'm looking forward to your update. In the mean time, I wrote an extra function to read out the gyroscope that I was talking about.

I found a library supporting the gyroscope so it was rather easy to supplement the arduino.m, the adiosrv.pde and simulink library with an ITG3200 (the gyroscope) Read function.

It is quite funny because in the beginning I received the same error. The first time I executed my a.ITG3200Read function I received the time out error. But when I executed a second fwrite right after the a.ITG3200Read function I received the values. Any fwrite function would do the trick (the values were also send if I did an a.analogRead).

So I fixed the problem by adding a second fwrite command in de arduino.m file right after the first one because it seemed that after the first fwrite, the values were stored in the arduino and by executing the second fwrite it was read by matlab.

I'm not much of a programmer but I can imagine that this solution is at least a bit unorthodox. But to be honest, it seems to do the trick so I am not going to worry about it.

I am still looking forward to your update though, because your block is far more general from what I have done.

Comment only
25 Dec 2013 Zhuo Li

Zhuo Li (view profile)

Hi Ingmar, sorry for the inconvinience. I remember I discovered that not long after I uploaded the block, then I made some improvement but never got it updated. Now, I've updated it, you should be able to see the corrected file days later after Mathworks process it. It works better, but I sometimes still recieve the serial reading timeout error, and don't know how to eliminate it.

Comment only
23 Dec 2013 Ingmar

Ingmar (view profile)

Also on my behalf a big thanks Zhuo. It would be great to see this block up and running in Simulink.

Unfortunately, I encounter the same problem as Mitch Torrens. I try to read out an IMU (https://www.sparkfun.com/products/10724) connected to an Arduino Uno via i2c block but no read out occurs and the workspace gives me the following warning.

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.

I am particularly interested in the gyro located on the IMU but I also tried to read out the accelerometer (ADXL345) which gave me the same problem.

Any of you guys found a solution to it yet?

Kind Regards

Comment only
24 Jun 2013 Mitch Torrens

First of all, thanks for this block! Once I get it to work it'll save me a ton of time.

At risk of asking a programming question, I'm having a bit of trouble implementing the block in conjunction with a motor encoder (Vex 269: http://www.vexforum.com/wiki/index.php/Intergrated_Motor_Encoders). I believe the problem is that the terminator address of the encoder (0x4C) is different from the one that simulink is trying to access, but I am having trouble finding where this address is specified in the provided source codes. The warning I get is:

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.

I can read the encoders with the I2CEncoder library implemented through the arduino IDE, so I am certain that the hardware is set up properly. The rest of the arduinoIO package works properly as well. Any insight?

Comment only
23 Jun 2013 Zhuo Li

Zhuo Li (view profile)

You're welcome Addie. I would say yes, it can be edited for Mega, but I imagine that to be a cumbersome job which includes augmenting the adiosrv.pde file, arduino.m file and the block files from Atmega328 to Atmega1280. I think it will be a better idea that you advise the developing team "MathWorks Classroom Resources Team" to release a separate package for Mega.

Comment only
19 Jun 2013 addie irawan

Thanks Zhuo. Another one question, do Arduino IO block able to be edited for Arduino Mega used. I'm currently searching for solution to expand the number of port on each block such Digital Writer and A/D inputs in order to make it suitable for Arduino Mega (available for all ports). Currently the block for Arduino IO package is limited for Arduino Mega

Comment only
17 Jun 2013 Zhuo Li

Zhuo Li (view profile)

Hi Addie, for now, this block cannot work with Arduino Target package because it is of different mechanism. The Target package supports realtime target while the ArduinoIO is not "real" real time, it's a host-target structure. Sorry for the inconvenience.

Comment only
15 Jun 2013 addie irawan

Is it possible to use with Arduino Target package because I'm now using Arduino Mega

Comment only
09 May 2013 Zhuo Li

Zhuo Li (view profile)

I haven't try it on 2013a, so I can't guarantee. If you don't mind, please tell me more about the error you get and the I2C device you are interfacing with. I'll try to improve this block.

Comment only
08 May 2013 efrianto tanto

Hello Zhuo Li, i have downloaded this submission and i have followed step by step instruction inside the downloaded file, but it doesnt work until now, is it compatible with 2013a actually? thanks

03 May 2013 Zhuo Li

Zhuo Li (view profile)

one thing I forgot to mention in the readme file is that, after replacing the adiosrv.pde, be sure to upload it to Arduino using Arduino IDE.

Comment only
Updates
20 May 2013 1.1

Added a parameter field for register address to make this block more general.

30 Jan 2014 1.7

Changed the serial waiting time longer from 0.0001 to 0.0014 so as to avoid the serial read timeout.

Contact us