Arduino to Matlab – Read in sensor data!
Matlab is by far the best software I have ever seen when it comes to plotting data and showing it in a visual format. I figured it would be awesome if I could get Arduino and Matlab to work together!
EDIT JULY 4, 2011: Apparently if you add
as the very first line, it handles the serial object deletion issue! WOOT!
Here’s the code that I came up with that works reasonably fast. It doesn’t wait for the buffer to be filled to then store it to a variable. Here, it is asynchronous communication. ^_^
This is a basic screenshot of what you will get, minus the green stars.
Here is the code. Let me know if you use, it would be neat to see what everyone would come up with!
*Note: The code highlighting for Matlab was buggy, so the below is using C code highlighting. Once you paste it into Matlab, it will be fine.
-
% Basic Arduino and Matlab
-
% Communication with an external hardware device
-
% ———————————————-
-
%
-
% Erin Kennedy
-
% Jan. 18, 2010
-
%
-
-
-
clear all; clc; close all;
-
-
-
% Try-catch is to prevent Matlab from crashing when the program is finished
-
try
-
-
-
% Initialize serial port
-
s = serial(‘/dev/tty.usbserial-A4001lVG’);
-
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
-
set(s,‘BaudRate’, 9600);
-
set(s,‘DataBits’, 8);
-
set(s,‘StopBits’, 1);
-
fopen(s);
-
s.ReadAsyncMode = ‘continuous’;
-
-
-
% Various variables
-
numberOfDatas = 50;
-
data = zeros(1, numberOfDatas);
-
i = 1;
-
-
-
% Main graph figure
-
figure(1);
-
hold on;
-
title(‘Incomming Data from External Device’);
-
xlabel(‘Data Number’);
-
ylabel(‘Analog Voltage (0-1023)’);
-
-
-
% Start asynchronous reading
-
readasync(s);
-
-
-
while(i<=numberOfDatas)
-
-
-
% Get the data from the serial object
-
data(i) = fscanf(s, ‘%d’);
-
-
% Plot the data
-
figure(1);
-
plot(i, data(i), ‘m*’);
-
-
% Ensure there are always 10 tick marks on the graph
-
if(i>10)
-
xlim([i-10 i]);
-
set(gca,‘xtick’,[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
-
end
-
-
% Draw and flush
-
drawnow;
-
-
%Increment the counter
-
i=i+1;
-
-
-
end
-
-
-
% Give the external device some time…
-
pause(3);
-
-
return;
-
-
catch
-
-
% Some of these crash the program – it depends. The serial port is left
-
% open, which is not good.
-
stopasync(s);
-
fclose(s); % bad
-
%delete(s);
-
%clear s;
-
-
fprintf(1, ‘Sorry, you"re going to have to close out of Matlab to close the serial port\n‘);
-
-
return
-
-
end
The code for the Arduino is this:
-
//
-
// BubbleBoy -> Matlab
-
// ——————-
-
//
-
// Read LDR data, print them to Serial (where Matlab will receive them)
-
//
-
-
int LDRpin = 0;
-
-
void setup() {
-
-
Serial.begin(9600);
-
pinMode(LDRpin, INPUT);
-
-
}
-
-
void loop() {
-
-
int photocellReading = analogRead(LDRpin);
-
-
Serial.println(photocellReading, DEC);
-
delay(200);
-
-
}
Enjoy, happy matlabbing!







February 20th, 2010 at 2:19 AM
I’m used to specifying which serial port the program must use for Processing apps, is that done automatically here?
Is that what the “s = serial(‘/dev/tty.usbserial-A4001lVG’);” is about?
February 21st, 2010 at 3:01 PM
[...] Here you can find simple and fast asynchronous Arduino to Matlab communication sources. It can be handy for fast sensor data displaying in convenient graph and even do some analysis. [...]
February 24th, 2010 at 3:02 PM
Awesome! I can’t wait to try this tonight.
March 4th, 2010 at 4:06 AM
Awesome post, this was just what I was looking for – thank you! (Tho I’m using a PC, and to answer boolscott’s question, replace the contents of serial() with “COMX” where X is the number of your com port the Arduino is connected to)
March 5th, 2010 at 4:51 AM
Thanks Seb
October 5th, 2010 at 2:46 AM
Hey Erin,
Thank you for the code. Exactly what i need to build my signature verification project. Arduino to matlab communication
I’ll let you know of my progress.
Sam
December 12th, 2010 at 10:43 PM
ihad error like this ??? Error: File: Serial.m Line: 45 Column: 1
Illegal use of reserved keyword “catch”.??? Error: File: Serial.m Line: 45 Column: 1
Illegal use of reserved keyword “catch”.
can anyone help me?
December 17th, 2010 at 1:10 AM
EXCELLENT ….you came to my rescue while i am building my pulse oximeter
Thank you very much.
March 14th, 2011 at 9:28 AM
hello Erin,
you are a lifesaver..i am so glad i found this website..i got a few things that i wish you can help me with..
1st) while i executed the matlab code this error came out:
??? Undefined function or method ‘training’ for input
arguments of type ‘char’.
may i ask for your help to enlighten me?
i am really a newbie in matlab and also arduino..hope that someone can help me..
March 18th, 2011 at 10:43 AM
hello erin,
i wish to ask how something..since i have connected my potentiometer to arduino board..does the code you share here can read the data from the potentiometer and plot graph?
March 23rd, 2011 at 4:46 PM
Thanks for that! Now if I can only figure out a way to close the serial port without turning off Matlab…
Also have you seen this?
http://www.mathworks.com/matlabcentral/fileexchange/27843
Have you ever tried 2-way communication, like controlling a motor while logging sensor data in Matlab?
March 23rd, 2011 at 5:13 PM
By the way, there is a free Matlab interface to Arduino, which you can download from here:
http://www.mathworks.com/academia/arduino-software/arduino-matlab.html
This lets you control your Arduino board over USB from a MATLAB session, in either Windows, Mac or Linux. For example you can instantiate an Arduino object from MATLAB using:
>> a=arduino(‘COM5′);
and then do stuff like:
>> av=a.analogRead(5);
>> a.pinMode(13,’output’);
>> a.digitalWrite(13,1);
>> delete(a)
March 23rd, 2011 at 5:23 PM
Thanks Dustyn! I think my friend got the serial port to be able to close by declaring the port as a global variable (in that big window in the middle when you start MATLAB), and using that variable name throughout the code.
It was a weird solution though, and only worked sometimes.
I did indeed see that, but I wrote the above code before the Arduino toolkit came out hahaha ;D
Thanks for the comment, Giampy!
April 2nd, 2011 at 12:40 AM
hi..thanks for the source code..
can i ask is there any permanent solution that we do not need to close off matlab everytime after we obtain the sensor data from arduino to matlab?
April 3rd, 2011 at 12:51 AM
can someone teach me how to program the arduino to receive data from two potentiometer and plot them using matlab??