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
  2 件のコメント
Sonomatic Australia
Sonomatic Australia 2015 年 8 月 31 日
Any luck with this?
Walter Roberson
Walter Roberson 2015 年 8 月 31 日
Are you using a real serial port all the way, or are you using USB?

サインインしてコメントする。

回答 (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 Center および File ExchangeMATLAB 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