Posts Tagged ‘arduino matlab’
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!






