Arduino serial communication with Matlab (DAQ, getting maximum sample rate)
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone
I am trying to create DAQ software in Matlab which reads data from Arduino 2560 analog pin at maximum possible speed and plot data in real time. However I cant get sample rate higher than 50 Hz. I have been browsing the internet and trying to find a solution without succes so far.
Here is the Arduino code. It should be able sample at rate aroun 8 kHz (serial monitor). I kept it simple so I would achieve maximum speed.
void setup() {
// start the connection to the device over the USB host:
//Serial.begin(500000); I have tried different baud rates suggested at forums
Serial.begin(115200);
}
void loop() {
int analogValue = analogRead(A0) ;
Serial.println(analogValue);
// delay(15); without delay(15) error occurred in matlab while reading data
}
Here is simplified Matlab code. I'm just trying to collect as much data as i possibly can. So far i got 642 samples / 10s.
clear all
close all
clc
priorPorts = instrfind; % finds any existing Serial Ports in MATLAB
delete(priorPorts); % and deletes them
%User Defined Properties
serialPort = 'COM3'; % define COM port #
s = serial(serialPort,'BaudRate',115200);
fopen(s);
figure
pause(1)
i=1;
tic;
while toc<=10 %collect data for 10s
dat = fscanf(s, '%d')';
data(i)=dat;
time(i)=toc;
%dat = fread(s,100)'; % fread didnt work (oscilating from 13 to 50)
plot(time,data,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
drawnow;
i=i+1;
end
priorPorts = instrfind; % finds any existing Serial Ports in MATLAB
delete(priorPorts); % and deletes them
disp('end');
Is there a way to read all samples sent by Arduino or are there any inner Matlab limits?
Any help would be greatly appreciated
2 个评论
回答(1 个)
enes kuzucu
2017-10-20
it is possible to read at 100 hz .I dont know the limits tho. But quality and the resolition is changing.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!