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

1 visualización (últimos 30 días)
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

Respuestas (1)

enes kuzucu
enes kuzucu el 20 de Oct. de 2017
it is possible to read at 100 hz .I dont know the limits tho. But quality and the resolition is changing.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by