Arduino serial communication with Matlab (DAQ, getting maximum sample rate)

조회 수: 1 (최근 30일)
Richard Omacka
Richard Omacka 2014년 4월 27일
답변: enes kuzucu 2017년 10월 20일
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

답변 (1개)

enes kuzucu
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.

카테고리

Help CenterFile 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!

Translated by