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
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’, ‘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 won’t 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 more library (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 and run 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
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
%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:
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.