Arduino and Matlab Interfacing
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
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).
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.