Вы находитесь на странице: 1из 2

Interfacing the Arduino with Matlab Using Events

12th February 2010

Opening a basic serial connection to the Arduino in Matlab is not difficult and is documented.
Data is sent to the device using the fprintf() function and received using fscanf().

1s = serial('COM3', 'BaudRate', 9600);

3fprintf(s, 'Hello World!');
4line = fscanf(s);

If we have data continually being sent by our device there are two ways of capturing it. We
can create a loop within Matlab to constantly check if any data has been received however
this doesnt seem like brilliant coding practise. Alternatively we can make use of the
Matlabs BytesAvailable event and read the input buffer only when data is received.

Unfortunately, unlike languages like C++, Matlab is only able to pass variables by value and
not by reference. So if we create an array in our workspace to hold the received data, our
callback function will be unable to update it. However in Matlab it is possible to nest
functions within the body of another. The variables defined in the outer function will be
accessible to the inner function.

2 % Calling the function myFunc will display the number 10.
% Demonstrates that nested function has access to variables.
3 function myFunc
5 myVar = 10;
7 innerFunc()
9 function innerFunc
10 end

The code to establish a serial connection is written into a Matlab function which also contain
an array of all received data. The callback function of the BytesAvailable event is defined as
a nested function and hence can add to the data array when new data is received. This allows
us to perform analysis on the data in parallel to it being read.

The full sourcecode of my function is shown below:

Data Acquisition and Processing through MATLAB AND ARDUINO

In previous POST, you used MATLAB to analyze data and the Arduino with various sensors to gather
information from the environment. In this POST, you will learn how to interface the Arduino with

One of the most useful things about the Arduino is how widely it is used. The Arduino platform is so
popular that even companies such as MathWorks, which produces MATLAB, have developed software
to support it. In the first part of the lab, you will use the MATLAB Arduino support package to connect
the Arduino to MATLAB, allowing you to obtain and plot accelerometer data directly from within the
MATLAB command window.
In the second part of the lab, you will use MATLAB to do some simple, real-time processing on the
accelerometer data that you gather. This will have important applications for the final projects.


1. Interfacing Arduino with MATLAB and plotting accelerometer data

- Go to http://www.mathworks.com/matlabcentral/fileexchange/32374 to download the MATLAB
support package for Arduino. Click the blue Download Submission button on the right.
- Extract the ArduinoIO folder and save to C:/user/MATLAB.
MATLAB R2012a Mathematics All Programs - Open MATLAB: Start
- On the left in the Current Folder tab, right click ArduinoIO and select Add to Selected Folders and
Subfolders (Figure 1). This makes the files Path accessible to MATLAB.

Image shows Adding folder to path in MATLAB (note that your folder will be called ArduinoIO, not

- Connect the Arduino to the computer and open the Arduino IDE. Check which COM port the
Arduino is using and select that option (Tools Board). Serial Port). Also select the correct board
- C:/user/MATLAB/ArduinoIO/pde/adiosrv/adiosrv.pde Open Go to Files to open the .pde file.
This program tells the Arduino to send data to and receive data from the computer such that it can be
controlled by MATLAB. Upload this code to the Arduino.
- In the MATLAB command window, create a new Arduino object a by entering

a = arduino(COMX);
where X should be replaced by the number of the COM port that the Arduino is using. Once the
Arduino has successfully connected, you should see the following text: