5.0

5.0 | 6 ratings Rate this file 636 Downloads (last 30 days) File Size: 1.88 MB File ID: #27843
image thumbnail

Arduino IO package: Slides and Examples

by Giampiero Campa

 

06 Jun 2010 (Updated 14 Sep 2010)

Slides for the webinar: "Learning Basic Mechatronics Concepts Using the Arduino Board and MATLAB".

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This zip file contains the slides (and some code examples) used for this webinar: http://www.mathworks.com/company/events/webinars/wbnr43537.html

The webinar introduces the ArduinoIO package, which allows the user to perform Analog and Digital Input and Output, as well as to control DC, Servo and Stepper Motors interactively from the MATLAB command window, without having to write, debug, compile, upload and run C programs.

NOTE: You can download the Arduino IO package from here:
http://www.mathworks.com/academia/arduino-software/arduino-matlab.html

Acknowledgements

This file inspired Teaching Mechatronics With Low Cost Hardware: Webinar Examples.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File  
Everyone's Tags
analog(10), arduino(6), class, data export(2), data import, digital, measurements(2), mechatronics(3), motor(4), pick of the week, potw, pwm, robotics(2), xbee
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (38)
01 May 2013 EDWIN ALVAREZ

greetings.
First, thank you very much to you, Giampiero Campa =).
and i am attempting to control a robot with Xbee modules. and for the wireless communication we are using 2 Xbee pro series 1. They have been configured and the adiosrv.pde file has been uploaded into the arduino. I have been using your code for a=arduino('com5') in Matlab and I am getting this error:

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
Error using arduino (line 101)
Connection unsuccessful, please make sure that the Arduino is powered on, running
either srv.pde, adiosrv.pde or mororsrv.pde, and that the board is connected to
the indicated serial port. You might also try to unplug and re-plug the USB cable
before attempting a reconnection.

Sometimes the error occurs at line 115, but I haven't done anything different to my procedure when I get this error.

ok, i have a solution for this,

is the xbee have a speed of 9600 bps by default. file has a default adiosrv 115200 and arduino.m file also. then I put them all at 9600 bps and it worked =)
is wonderful to make this interaction between matlab and arduino xbee modules through. again thank you very much Giampiero Campa

19 Mar 2013 Giampiero Campa

Patricia what did you put instead of that "12" in the fourth comment below ? You mentioned you tried higher values but perhaps it wasn't big enough.

I would suggest, with a testing program running on the Arduino which sends data continuously, to see if you can create a connection with the serial command from MATLAB. Try sending and receiving data using that command and possibly the serial block of the instrument control toolbox.

If all fails, another option could be trying to use the Simulink Support Package instead, which might even be more appropriate in your case since it does not require a MATLAB connection to work.

18 Mar 2013 Patricia

Giampiero,
I am using an Xbee USB Adapter for one Xbee and a Xbee 5V/3.3V Adapter connected to the arduino. In this configuration, I can send "testing" on the arduino and can view it with the IDE and a terminal program called XTCU.
When I try a=arduino('com5') in matlab I am still getting the same error at line 101, but I noticed that the RSSI light on the Xbee shield connected to the arduino comes on as matlab is trying attempting the connection. The other xbee is constantly transmitting the signal. Any suggestions?

15 Mar 2013 Giampiero Campa

Patricia, i can't think of anything. Other people were successful in using the BTShield, and, i think, the Xbee too. Does the same hardware setup work when using the IDE directly without matlab ? Can you establish a connection with a terminal program like hyperterminal ?
Let me know if you find anything.

Giampiero

15 Mar 2013 Patricia

After trying your suggestions, I am still getting the same time out error. I noticed that the Rx and Tx lights on the Xbee connected to the arduino are not even lighting up. When I tested the arduino and the Xbees it did? Any other suggestions?

15 Mar 2013 Classroom Resources Team

Hi Patricia and everybody else. If you get the timeout error one thing i would suggest is to go into the arduino.m
file, find these lines:

fprintf(1,'Attempting connection .');
for i=1:12,
pause(1);
fprintf(1,'.');
end

and increase that 12 up to 15 or more until it works. Another suggestion
is using the adiosrv.pde file instead of motorsrv.pde or srv.pde.

15 Mar 2013 Patricia

I am attempting to control an unmanned boat, but I am having difficulty getting the serial communication to work. For the wireless communication we are using 2 Xbee series 1. They have been configured and the srv.pde file has been uploaded onto the arduino. I have been using your code for a=arduino('com5') in Matlab and I am getting this error:

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
Error using arduino (line 101)
Connection unsuccessful, please make sure that the Arduino is powered on, running
either srv.pde, adiosrv.pde or mororsrv.pde, and that the board is connected to
the indicated serial port. You might also try to unplug and re-plug the USB cable
before attempting a reconnection.

Sometimes the error occurs at line 115, but I haven't done anything different to my procedure when I get this error.

Do you have any ideas how to get past this problem?

25 Jan 2013 Kevin

please help I am also getting a timeout error, using matlab 2010b and arduino 0022

23 Nov 2012 André

Hi, I'm doing a experiment with arduino board that require port manipulation. I would like to know how to do that in Matlab. I have to connect 5 DC motors with the arduino simultaneously.

Thanks for your attention!

20 Oct 2012 Joseph

Good day guys. Can someone help me, because my gizDuino appears as "libusp" in device manager not as connected in any comport. Therefore I cant communicate with my board and matlab.

10 Sep 2012 Stefan

I am considering using Arduino coupled with Matlab for a project and would like to know what the maximum throughput for the interface between MATLAB and the Arduino board is. Is it a USB simulated serial connection, or does it support full USB speeds? I would be most grateful if someone could give me a number of pin reads/writes / sec if possible.
Thanks!

28 Jul 2012 MAYANK

Hi I´m using Matlab R2010a, target arduino , software arduino-0022,
I've uploaded the adiosrv, and had no errors uploading
When I try a=arduino('Com6') in MATLAB 2010a, I get a timeout error.
why is it happening?
what m i supposed to do in this case?

07 Jun 2012 Giampiero Campa

Hernan, No I have not included any command specific for the LCD library, so probably not.

If you are interested in a solution for LCD let me know though, I can guide you if you decide to try to modify the code so that it would work with it.

06 Jun 2012 Hernan

Hello, I was wondering if the arduino io would work with the liquid crystal library for the Arduino? I am trying to display a message from matlab on an LCD using the arduino.

06 Jun 2012 Hernan  
07 Apr 2012 Roger Su

hi, I am trying to read the analog input data from arduino in Matlab, but i don't know how to start. Can someone help me, please ?

26 Mar 2012 Giampiero Campa

Andrey, the MATLAB support package for arduino, (ArduinoIO), and the Simulink Support Package, (now renamed Embedded Coder support package), are completely different and are not supposed to work with one another. Either you use one or the others. If you have problems regarding the Embedded Coder Package, (that's what it looks like) i suggest that you can try looking where you actually downloaded that package.

25 Mar 2012 Andrey Zagvazdin

Hello! I MatLAB 7.12 (R2011a) 64-bit. OS Windows 7
Fee clone Arduino, Russian assembly Freeduino (http://freeduino.ru/arduino/index.html), a complete analog Ardruino Uno / Duemilanove w/ATmega328.
My actions:
1 - Connection "Arduino" to MatLAB
* Set ArduinoIO (http://www.mathworks.com/matlabcentral/fileexchange/32374);
* Unpack the package into C: \ arduinoIO;
* >> Cd c: \ arduinoIO
* >> Install_arduino
* >> Savepath
* Flashed "Arduino" core srv.pde (https://github.com/adafruit/Adafruit-Motor-Shield-library/zipball/master);
* >> A = arduino ('COM4');
At this point, everything is OK! From the window MatLAB, board responds and takes command.

2 - Connection "Arduino" to SIMULINK
* Downloading the package Simulink Support Package for Arduino (http://www.mathworks.com/matlabcentral/fileexchange/30277);
* "Simulink Support Package for Arduino" unpacked to C: \ arduino_simulink
* Downloading Wednesday Arduino IDE (arduino-1.0) and extract it to c: \ ArduinoTarget
* >> Cd c: \ arduino_simulink
* >> Addpath (fullfile (pwd, 'arduino'), fullfile (pwd, 'blocks'), fullfile (pwd, 'demos'))
* >> Savepath
* >> Sl_refresh_customizations
* Plug-in board "Arduino" to your computer.
* Indicates the path to the environment Arduino IDE >> arduino.Prefs.setArduinoPath ('c: \ ArduinoTarget')

That's all up to this point NORMALLY PUT.

* Determine the current platform team
>> Arduino.Prefs.setBoard ('atmega328') or >> arduino.Prefs.setMcu ('atmega328')
Throws an error
>> Arduino.Prefs.setBoard ('atmega328')
?? Reference to non-existent element of a cell
array.
Error in ==> Prefs> Prefs.parseBoardsFile at 227
lhs = parsedLines {i} {1} {1}%
can be of the form xx.yy.zz
Error in ==> Prefs> Prefs.setBoard at 66
boards =
arduino.Prefs.parseBoardsFile (boardsFile);
>> Arduino.Prefs.setMcu ('atmega328')
?? Error using ==> Prefs> Prefs.setMcu at 129
This function is deprecated. Use
arduino.Prefs.setBoard instead, e.g.
arduino.Prefs.setBoard ('uno')

TRIED TO IGNORE THIS OPTION AND KEEP SETTING, BUT IN MODELING, DEMANDS categorically specify the model.

25 Mar 2012 Andrey Zagvazdin

Hello! I MatLAB 7.12 (R2011a) 64-bit. OS Windows 7
Fee clone Arduino, Russian assembly Freeduino (http://freeduino.ru/arduino/index.html), a complete analog Ardruino Uno / Duemilanove w/ATmega328.
My actions:
1 - Connection "Arduino" to MatLAB
* Set ArduinoIO (http://www.mathworks.com/matlabcentral/fileexchange/32374);
* Unpack the package into C: \ arduinoIO;
* >> Cd c: \ arduinoIO
* >> Install_arduino
* >> Savepath
* Flashed "Arduino" core srv.pde (https://github.com/adafruit/Adafruit-Motor-Shield-library/zipball/master);
* >> A = arduino ('COM4');
At this point, everything is OK! From the window MatLAB, board responds and takes command.

2 - Connection "Arduino" to SIMULINK
* Downloading the package Simulink Support Package for Arduino (http://www.mathworks.com/matlabcentral/fileexchange/30277);
* "Simulink Support Package for Arduino" unpacked to C: \ arduino_simulink
* Downloading Wednesday Arduino IDE (arduino-1.0) and extract it to c: \ ArduinoTarget
* >> Cd c: \ arduino_simulink
* >> Addpath (fullfile (pwd, 'arduino'), fullfile (pwd, 'blocks'), fullfile (pwd, 'demos'))
* >> Savepath
* >> Sl_refresh_customizations
* Plug-in board "Arduino" to your computer.
* Indicates the path to the environment Arduino IDE >> arduino.Prefs.setArduinoPath ('c: \ ArduinoTarget')

That's all up to this point NORMALLY PUT.

* Determine the current platform team
>> Arduino.Prefs.setBoard ('atmega328') or >> arduino.Prefs.setMcu ('atmega328')
Throws an error
>> Arduino.Prefs.setBoard ('atmega328')
?? Reference to non-existent element of a cell
array.
Error in ==> Prefs> Prefs.parseBoardsFile at 227
lhs = parsedLines {i} {1} {1}%
can be of the form xx.yy.zz
Error in ==> Prefs> Prefs.setBoard at 66
boards =
arduino.Prefs.parseBoardsFile (boardsFile);
>> Arduino.Prefs.setMcu ('atmega328')
?? Error using ==> Prefs> Prefs.setMcu at 129
This function is deprecated. Use
arduino.Prefs.setBoard instead, e.g.
arduino.Prefs.setBoard ('uno')

TRIED TO IGNORE THIS OPTION AND KEEP SETTING, BUT IN MODELING, DEMANDS categorically specify the model.

31 Jan 2012 David Batterbee

I'm having similar problems with the UNO described above. In particular, typing "arduino.Prefs.searchForComPort" returns an empty results even though my Device Manager shows that the Arduino Uno is connected to COM11.

I have no problem at all with the Arduino Duemilanove and the searchForComport function works fine.

How can I get Matlab to see the UNO?

14 Dec 2011 Deepak Mohan

can i implement all the functions in an arduino when interfaced with matlab....

06 Sep 2011 Giampiero Campa

You can try to use Hyperterminal (or the IDE) to communicate with adiosrv.

There are some instructions at the top of the adiosrv.pde file that tells you how to do it. For example if you establish serial connection and type "2n1" in the IDE (or in Hyperterminal) you should set pin #13 high and the led should light up.

This will allow you to further narrow the problem down.

03 Sep 2011 Justin

I'm having trouble getting MATLAB to communicate w/t my Arduino Uno.

I've uploaded the adiosrv (I'm not using the motor shield), and had no errors uploading (I tried the srv file, got an error, but realized that since i'm not using the motor shield I should be using the adiosrv file anyways).

When I try a=arduino('Com6') in MATLAB 2009a, I get a timeout error. Using TMtool, I can't find the Arduino using matlab. So it seems that the adiosrv is not doing its job, but I'm not sure where to go from here!

Any help would be much appreciated!

18 Aug 2011 Imran

very informative but the Arduino Uno target files don't work.

15 Aug 2011 Héctor Corte

Simon. I can´t find the line you are saying with addpath on Arduino.m ¿Anyone have the same problem and solve it the same way?

29 Jul 2011 Mihir Shah  
15 Jul 2011 UMAGON

hi, is there any video for this? why cant i access to it?

15 Jul 2011 Giampiero Campa

The Arduino IO (works fine on 64 bit windows platforms).

The Arduino Target works with the UNO, but you have to use arduino.Prefs.setMcu('atmega328p-pu')
in the instructions above (in general this is the name printed on the microcontroller).

A newer version with a better readme file is coming out soon.

07 Jul 2011 Ryan Stafford

I'm having exacting the same problem as Mario... a connection issue when using the Arduino Uno with Simulink. The odd thing is, I also have the older Arduino board (Duemilanove) and this works absolutely fine with the Simulink examples provided. As far as I am aware, these two boards use exactly the same chip (328P) but the UNO now uses ATmega8U2 instead of the FTDI chip. I'm not sure how to resolve this issue or why this is causing a problem... any ideas? The Uno works fine within the Arduino environment and I can also make a connection when using the arduino.m file, but when I try to download a Simulink model to the board I receive the same error message Mario has listed above. Thanks,
-Ryan Stafford

09 Jun 2011 Dave Rowland

Thanks for the very helpful submission! I was curious if there are files for 64bit windows? For example, when I try to run serial('COM3'), I get an error saying:
"The serial port object is supported on the Solaris, 32-bit Windows and
32-bit Linux platforms only."
Thanks,
Dave Rowland

19 May 2011 mario

Hi I´m using Matlab R2010a, target arduino UNO, software arduino-0022, but i have a problem when I use my board in SIMULINK,
I follow steps in README

Arduino Target for Use With Simulink Software, Version 1.0 (R2010a) 01-July-2010
>> cd c:\ARDUINO_TARGET
>> addpath(fullfile(pwd,'arduino'),fullfile(pwd,'blocks'),fullfile(pwd,'demos'))
>> savepath
>> sl_refresh_customizations
>> arduino.Prefs.setArduinoPath('c:\Program Files\ArduinoPRGM\arduino-0022')
>> arduino.Prefs.setMcu('atmega328p')
>> comPorts=arduino.Prefs.searchForComPort;
>> arduino.Prefs.setComPort(comPorts{1});
??? Cell contents reference from a non-cell array object.

but i don´t know why this problem
I try to use arduino.Prefs.setComPort('COM1')
but demo_arduino_blink.mdl doesn´t work

Download failed. Check your serial connection and and run the command arduino.Prefs.getMcu to check you have specified the correct MCU.

The call to arduino_make_rtw_hook, during the after_make hook generated the following error:
Download failed. Check your serial connection and and run the command arduino.Prefs.getMcu to check you have specified the correct MCU.
The build process will terminate as a result.

### If your target board has TX and RX LED you should see them flashing ...
avrdude: stk500_getsync(): not in sync: resp=0x30
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

What can I do?
Thankyou
Mario Camacho

06 Apr 2011 Giampiero Campa

Thanks Simon !

Tee, the connection is automatically set up as 9600baud, 8,N,1, on both the MATLAB and Arduino (adiosrv.pde) sides.

If you want to change the baudrate you will need to do that in both the arduino.m and adiosrv.pde files.

06 Apr 2011 Tee

I have some question on obtain data from arduino which connect to optical encoder. I have using following code to obtain data from Arduino

function encoder1pushbutton9_Callback(hObject, eventdata, handles)
handles.a.pinMode(2,'input');
handles.a.digitalRead(2)
numberOfDatas = 200;
data = zeros(1, numberOfDatas);
i = 1;
% Main graph figure
figure(1);
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel(' output (0-1)');
for i=1:numberOfDatas
% Get the data from the serial object
data(i) = handles.a.digitalRead(2);
% Plot the data
figure(1);
plot(i, data(i), 'm*');
drawnow;
end
Did i have to set the Baudrate, databits and stopbits to acquire data from arduino???

22 Mar 2011 fremond khoo

can i ask how do i plot a graph in MATLAB GUI using the data i obtained through arduino?

20 Mar 2011 Simon

To run in R2008a, line 21 of arduino.m needs to be:
addpath(fullfile(pwd,''));
instead of:
addpath(fullfile(pwd));

I sussed it out and thought I'd share it here. I have yet to test it with a more up to date version of Matlab. I guess the function could have changed(?).

Without this modification the fullfile function returns:
??? Error using ==> fullfile at 21
Not enough input arguments.

09 Mar 2011 fremond khoo

yeah..same prob..can u tell us how to solve it?

09 Feb 2011 Mohd Ismail

Hi,

Found your link from Arduino blog. I just started to work on interfacing Arduino with Matlab. But unable to run the example file. Matlab connection unsuccessful when send command a=arduino('com7'). Any idea?

??? Error using ==> arduino>arduino.arduino at 100
Connection unsuccessful, please make sure that the Arduino is powered on, running
either adiosrv.pde or mororsrv.pde, and that the board is connected to the indicated
serial port. You might also try to unplug and re-plug the USB cable before attempting
a reconnection.

Cheers

09 Feb 2011 Mohd Ismail  
Updates
27 Aug 2010

Inserted link to download arduino class files.

09 Sep 2010

Uploaded figure, put copyrights in the two example files, and referenced the Arduino page in the description.

10 Sep 2010

Mainly i have updated the description text.

14 Sep 2010

Removed redundant license file.

Contact us