Code covered by the MathWorks Limited License

Highlights from
MATLAB Support Package for Arduino (aka ArduinoIO Package)

4.0

4.0 | 1 rating Rate this file 796 Downloads (last 30 days) File Size: 310.1 KB File ID: #32374
image thumbnail

MATLAB Support Package for Arduino (aka ArduinoIO Package)

by Classroom Resources Team

 

28 Jul 2011 (Updated 24 Dec 2011)

MATLAB library and Simulink blocks for communicating with an Arduino microcontroller board

| Watch this File

File Information
Description

Arduino (www.arduino.cc) is a low-cost open-source electronics prototyping platform.

MATLAB Support Package for Arduino (also referred to as "ArduinoIO Package") allows you to communicate with an Arduino Uno or Duemilanove over a serial port. It consists of a MATLAB API on the host computer and a server program that runs on the Arduino. Together, they allow you to access Arduino analog I/O, digital I/O, and motor shield from the MATLAB command line.

The Support Package also includes Simulink blocks for communicating with the Arduino.

For more information about the Support Package, see:
 http://www.mathworks.com/academia/arduino-software/arduino-matlab.html

For Simulink blocks that support code generation, see:
 http://www.mathworks.com/academia/arduino-software/arduino-simulink.html

Sample usage:

 %-- connect to the board
 a = arduino('COM9')

 %-- specify pin mode
 a.pinMode(4,'input');
 a.pinMode(13,'output');

 %-- digital i/o
 a.digitalRead(4) % read pin 4
 a.digitalWrite(13,0) % write 0 to pin 13

 %-- analog i/o
 a.analogRead(5)
 a.analogWrite(9, 155) % write 155 to analog pin 9

 %-- motor shield
 a.motorRun(4, 'forward') % run motor forward
 a.servoWrite(1, 175); % move servo#1 to 175 deg position
 a.stepperStep(1, 'forward', 'double', 100); % move stepper motor

 %-- close session
 delete(a)

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (25)
09 Oct 2011 Cameron  
26 Oct 2011 Ernesto

I've been following the steps on the Readme file. I already added the arduino folders path successfully. Now I'm trying to run the a=arduino('port') in my case COM4 and I get the error:

??? Error: File: xx/xx/xx Line: 6 Column: 26
The expression to the left of the equals sign is not a valid target for an assignment.

Does anyone know what might be going wrong? I already also tried a=arduino('/dev/ttyUSB0') instead of COM4 but I got the same error.

30 Oct 2011 Tony Stark

Your problem seems like the one I ran into as well. What I did was double check my COM port in device manager. Opened up matlab and clicked in the "Current Folder" pane, navigated to where I unzipped the "ArduinoIO" package, right clicked the folder and added it and its subdirectories othe path, then execute the "savepath" command.

If that doesnt work, force your COM port to be COM8 or COM5. Also run matlab as admin (SHIFT + Right Click "Run as Adminstrator")

07 Nov 2011 Roman

I have installed all packages for Arduino support. In Matlab command line can successfully connect to Arduino and operate all its pins.

I want to use function msfun_arduino_analogread(block), link:
http://www.mathworks.com/matlabcentral/fileexchange/32374-matlab-support-package-for-arduino-aka-arduinoio-package/content/simulink/msfun_arduino_analogread.m

But when add in Simulink block "Level-2 MATLAB S-Function" I have the following error (screen shot): http://habrastorage.org/storage1/fdff41a7/f8668eac/77eb249a/38eb67f6.png

Can I go another way to read data from Arduino in Simulink? I just want to use my Arduino as Analog Input in Simulink.

05 Dec 2011 Fardin

I am having problem with loading the srv.pde file to Arduino Mega 2560.
When compiling, the error is on line 553:
        analogReference(INTERNAL);
and the error message is:
srv.cpp: In function 'void loop()':
srv:552: error: 'INTERNAL' was not declared in this scope

I have done all the steps before this step.
Please Help. And thanks in advance.

06 Dec 2011 JULIO DE LUNA

I had the same problem, all we did was add a / * in
/ * Case 49:
         analogReference (INTERNAL);
         break;

then, I had no problems =)

07 Dec 2011 Michael

This is a total hack, but for those of you who are getting 'Connection unsuccessful' error and are sure that the serial port is working fine (i.e. by checking it with a program like TeraTerm and typing in '99') then you can set chk to be 1 (or 2 or 3), depending on which pde you had uploaded into the Arduino.

% query script type
fwrite(a.aser,[57 57],'uchar');
chk=fscanf(a.aser,'%d');
chk = 1; % SHOULDN'T HAVE TO DO THIS!

09 Dec 2011 Sotiris

Hello everyone, I am trying to Upload,"adiosrv.pde" on the arduino mega2560 but I get the following message:

"adiosrv.cpp: In function 'void loop()':
adiosrv:239: error: 'INTERNAL' was not declared in this scope"

Any suggestions??

Thank you in advance

20 Dec 2011 Alonso

For the INTERNAL problem with the mega2560 I found this: http://arduino.cc/en/Reference/AnalogReference

INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)

So just replace "INTERNAL" with "INTERNAL1V1" and it should work.

08 Jan 2012 Sun

Hello
When I try to call block analogread, everytime I build will pop:

The corresponding 'msfun_arduino_analogread.tlc' file for the MATLAB S-function 'msfun_arduino_analogread' in block 'test1/Arduino Analog Read' must be located in the current working directory, the MATLAB S-function directory 'C:\Users\Julio\Documents\MATLAB\ArduinoIO\simulink', or the directory 'C:\Users\Julio\Documents\MATLAB\ArduinoIO\simulink\tlc_c'

and I ingnore it matlab just quit. Even the demo cannot work. Anyone has ideas?

Julio

09 Jan 2012 Gautam Vallabha

Sun, the MATLAB Support Package does not support code generation. The included Simulink blocks communicate with the Arduino in Simulink "normal" (aka. simulation) mode only.

For blocks that support code generation, see http://www.mathworks.com/academia/arduino-software/arduino-simulink.html

10 Jan 2012 Giampiero Campa

UPDATE: in the new version (3.2) the problem due to the fact that INTERNAL is not declared for the MEGA has been solved (basically now there is a macro that defines INTERNAL as equal to INTERNAL1V1 when compiling for the MEGA).

Please consider downloading the last version if you often switch between the UNO and MEGA boards.

23 Feb 2012 alexander

using a.stepperStep() has a maximum number of steps of 255 and causes unexpexted results with for loops. How can i avoid this and get the number of steps i want.

20 Mar 2012 Abdullah

why ı can connect servo just pin 9 and 10. I have 4 more pwm pin.. ? anyone can edit the files and blocks to add 6 servos to the arduino.

22 Mar 2012 Classroom Resources Team

Alex, thanks for the feedback, i'll think about it.

Abdullah, it's because the servo methods were made to support the adafruit motor shield, which only supports servo connected to pins 9 and 10. I will remove this limitation sometime this summer. However if you look at the code it should not be too hard to modify it to extend it to 6 servos.

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.

03 Apr 2012 José Antonio Segura

Hello.

I'm actually using and Arduino Mega 2560 and API_Matlab. But I have a problem, I cant's access to all PWM outputs, because the library just recognize as PWM the digital pins 3 5 7 9 10 and 11, that are the PWM pins of Arduino Uno Board.

Did you know how can I access to all PWM pins of Arduino Mega 2560?

03 Apr 2012 Gautam Vallabha

Jose: The readme.txt file included with the support package has instructions for how to modify the code to work with Arduino Mega (search for "ARDUINO MEGA" in the readme.txt).

03 Apr 2012 Classroom Resources Team

General guidelines

* Please use this FileExchange page for general comments and ratings on the support package

* If you have questions about using this support package, post your question at http://www.mathworks.com/matlabcentral/answers (with tag "arduino", and product "matlab")

04 Apr 2012 Abhineet Mishra

Hello
First of all I must thank you for providing us with such a nice interface between MATLAB and Arduino.
Actually I was wondering if you have any function like Arduino's 'MAP' function which can actually map analoginput data into a specific range.
Thanks in Advance

05 Apr 2012 Gautam Vallabha

Abhineet, please post your question at http://www.mathworks.com/matlabcentral/answers, as noted above. I will be happy to answer it there.

10 Apr 2012 Nancy

Can I control a servo motor from Matlab without using a motor shield
I tried to use this code:
 
a=arduino('COM3');
a.pinMode(10, 'OUTPUT');
a.analogWrite(10, x); % where x= from 0 up to 255

but it did not work
can anyone help me??

17 Apr 2012 Muhammad Adeel khan

hello
i am trying to control the motion of a motor by using simulink blocks(of arduino matlab support package).i need an encoder(position) input as feed back in my control model.
can any one help me how can i read encoder value in my control model?

25 May 2012 Jean-Marc ALLENBACH

I successfuly used Arduino with R2011a

I tried the same in the classroom with R2007b
No problem with install_arduino, but with
arduino('COM5') I received an error message:
??? Error: File: arduino.m Line: 6 Column: 35
Undefined function or variable 'private'.

Error in ==> StartArd at 10
a=arduino('COM5')
Is it possible to solve the problem on this version R2007b of Matlab or have I to take time to install R2012a on all classrooms?

25 May 2012 Jean-Marc ALLENBACH

I successfully used Arduino I/O with MATLAB R2009b. Only some Port COM problems due to XP platform: return to default port number after a deconnexion of Arduino board.

Please login to add a comment or rating.
Updates
18 Aug 2011

* Minor changes to readme, Simulink example models, and .pde files
* Added images for Simulink model preview

26 Oct 2011

Minor fixes to demo mode for servo handling

24 Dec 2011

Minor fixes and improvements, mainly for the MEGA and ChipKIT32 boards.

Tag Activity for this File
Tag Applied By Date/Time
support package Classroom Resources Team 10 Aug 2011 08:18:36
arduino Classroom Resources Team 10 Aug 2011 08:18:36
duemilanove Classroom Resources Team 10 Aug 2011 08:18:36
uno Classroom Resources Team 10 Aug 2011 08:18:36
motor shield Classroom Resources Team 10 Aug 2011 08:18:36
hardware interface Classroom Resources Team 10 Aug 2011 08:18:36
simulink normal mode io Classroom Resources Team 10 Aug 2011 08:18:36
measurement Classroom Resources Team 10 Aug 2011 08:18:36
test and measurement Classroom Resources Team 10 Aug 2011 08:18:36
lab hardware Classroom Resources Team 10 Aug 2011 08:18:36
simulink Classroom Resources Team 10 Aug 2011 08:18:36
data acquisition Classroom Resources Team 10 Aug 2011 08:18:36
data acquisition Trushal 16 Sep 2011 16:48:36
arduino Trushal 16 Sep 2011 16:48:42
arduino Hector Rangel 24 Sep 2011 18:29:36
arduino Lee Gunsuck 12 Oct 2011 12:02:49
uno Guillaume MARTIN 18 Oct 2011 11:24:45
arduino Denis Rodríguez Ahumada 21 Nov 2011 21:53:58
arduino Marcos Liso Nicolas 17 Jan 2012 08:32:54
arduino Sandeep Sandhu 28 Jan 2012 13:34:29
duemilanove Euan Millar 12 Mar 2012 14:11:14
arduino Ryan 20 Mar 2012 20:50:34
arduino David Rivera 21 Mar 2012 02:32:41
data acquisition Laureate 05 May 2012 22:09:08
simulink YUE 29 May 2012 09:00:26
simulink normal mode io YUE 29 May 2012 09:00:38

Contact us at files@mathworks.com