Vous êtes sur la page 1sur 5

ARDUINO AND MATLAB INTERFACING

Coding is one of the integral parts of the Robotics field. Arduino and
MATLAB are the most common and powerful tools used for this purpose. If
you are new to Arduino and MATLAB you may first look our tutorials on
them, as this would mainly concentrate on interfacing them.
Interfacing is establishing a sort of connection between two things, in this
case Arduino and MATLAB. A flow diagram would better explain this

MATLAB ARDUINO SYSTEM

So coding will be done in MATLAB and the instructions are communicated


to Arduino first through serial communication and then to the system (Ex:
a servo, led connected to pin 13 on Arduino, etc.). So, controlling the
system with the help of MATLAB.
WHY TO INTERFACE ARDUINO AND MATLAB?
MATLAB provides tools such as plotting functions, creating GUIs,
simulation and many more.
Ease of access and handling tedious calculations.

HOW TO INTERFACE ARDUINO AND MATLAB?(Windows)


Mathworks provides with a package called Arduino IO which needs to be
installed for interfacing. It is freely available in Mathworks website or you
can download the .zip file directly from the link provided below
http://goo.gl/3XFErp
Extracting the .zip file, folders named examples, pde, Simulink and
some .m files are visible. Copy the pde file and paste it in the libraries
folder of the Arduino file in your C/program files. Adding it a folder named
pde is visible in the File/examples of Arduino IDE. It shows five .pde files
named adio, adioe, adioes, motor_v1, and motor_v2, you have to
upload one of these files to your Arduino board depending on your
application.
adio - for basic serial commands (analog and digital)
adioe - adio + for handling encoders
adioes - adioe + for controlling servos
motor_v1 - adioes + for handling adafruit motor v1 shield
motor_v2 - adioes + for handling adafruit motor v2 shield

So for basic commands you have to upload adio to your board but if you
are trying to control encoders or servos it wont work and you must
definitely upload other pde files including server program required for
that application. In case, you are using adafruit motor shield, then you
must install one morelibrary (you can get all the above details in the read
me text available in Arduino IO folder).

Uploading the code, close the Arduino and move on to MATLAB. In


MATLAB open the folder containing the extracted files of Arduino IO and
drag the file install_arduino into the command window or simply double
click so that it opens in editor window andrun it. This will add the folders
to the path of MATLAB and you see a statement regarding that saying
Arduino folders added to the path
Saved updated MATLAB path
The above procedure has to be done only once. Now interfacing is done
and you can control your system through MATLAB. But you have to make
your MATLAB know to which port your Arduino board is connected and for
this the command used is

a = arduino(PORT)
Ex: a = arduino(COM4)
Sometimes it may return an error saying that the COM port is already in
use then we can use the below command to make it free and then
reconnect again using the command above
delete(instrfind({'Port'},{'COM4'}))
Functions used in Arduino can be used directly in MATLAB with a subscript
a. in the beginning of them. It means that we are referring to Arduino
board connected to COM port 4. (If ma = arduino(PORT), then the
subscript will be ma.). The following examples will make the point much
more clear
%for writing to a digital pin
a.digitalWrite(1,0);
%for reading from an analog pin
b = a.analogread(A0);
(Make sure that you are storing in variable different from one which you
have used for representing Arduino board, because this results in an
error)
%for reading value of a servo
C = a.servoread(2);
There are many examples in the examples folder of Arduino IO. You can
try these commands without actually connecting the Arduino by
establishing a demo mode running arduino.m file. You may delete the
Arduino connected to the port by using the command below
a.delete;
The above procedure is same independent of the version of Windows (7, 8
or 8.1 and 32-bit or 64 -bit) and MATLAB (R2013, R2014, R2015). One
more easy way of interfacing is installing the packages directly from the
MATLAB home. Go to Add-Ons and click on the
Get hardware support packages. This will contain a series of steps which
will directly install the packages from the internet. (But you need to have
a Mathworks account which you need to log in during the series of steps).
But Simulink must be used while they are interfaced from MATLAB add-
Ons while working with the code and in Simulink are possible if interfaced
using Arduino IO.
EXAMPLES
1. As a tradition, here is the blink led code that makes the led connected to pin 13 on
your Arduino board blink
Running above code will blink the led, but make sure that prior to this you have uploaded
The code adio.pde to the Arduino board from Arduino IDE.
2. Here is the code for servo sweep

The above code makes the servo sweep continuously, but make sure that
you have uploaded the adios.pde code to your Arduino board from the
Arduino IDE.

Vous aimerez peut-être aussi