MATLAB Answers

Arduino communication with MATLAB is too slow

69 views (last 30 days)
Pavlos Triantafyllou
Pavlos Triantafyllou on 26 Sep 2014
Commented: Kiki on 29 Mar 2016
I recently tried to connect my arduino Uno with MATLAB R2014a. In mathworks.com they suggest the use of the following new package http://www.mathworks.com/hardware-support/arduino-matlab.html
Now it seems that many commands that existed in previous versions are no longer supported (e.g. analogRead is now readVoltage) My problem is that the communication between MATLAB and Arduino is VERY slow. I timed a readVoltage() command and it takes about 25-35 ms to read the voltage of an analog pin. Is that as fast as it can get? I hope not! But what am I missing here?
You can see what I mean in the attempted real time plot that I'm trying to do below...
a=arduino();
interv=50;
elapsed_time=0;
figure
initial_time=tic;
while(elapsed_time<interv)
elapsed_time=toc(initial_time);
b=readVoltage(a,0);
plot(elapsed_time,b)
hold on
end
clear a

  0 Comments

Sign in to comment.

Answers (2)

Klont
Klont on 16 Mar 2015
i get the same ~30 ms latencies on my setup (windows 7, asus n550jv laptop, aduino uno r3, matlab 2014b). very disappointing. does anyone know how fast the equivalent calls are in when using compiled C-code?

  0 Comments

Sign in to comment.


Maarten
Maarten on 21 May 2015
I have the same problem if I read multiple analog ports. However your code could be faster if you do not update your plot within the loop. Without plotting I have a sample time of around 70 Hz.
a = arduino(serialPort);
input = nan(1,round(time/dt));
sampleRate = nan(1,round(time/dt));
i = 1;
h = waitbar(0,'Reading sensor output');
tic
while toc < time
tstart = tic;
waitbar(toc/time,h);
input(1,i) = readVoltage(a,0);
sampleRate(1,i) = toc(tstart);
i = i+1;
end
close(h)
plot(sampleRate)

  5 Comments

Show 2 older comments
Kiki
Kiki on 23 Mar 2016
Hi Walter
Thank you for your advice. Here is my code. It works, but the sampling rate is only around 8Hz. Would it be possible to get a higher one?
a = arduino();
input=zeros(1, 500);
sampleRate=zeros(1,500);
interv=5;
i=1;
tic
while(toc<interv)
tstart = tic;
input(1,i) = readVoltage(a,0); %0.114-0.125s
i = i+1;
end
figure(7)
plot(input(1:42))
i
mean(input, 'omitnan')
clear a
Walter Roberson
Walter Roberson on 23 Mar 2016
I recommend that you do not name a variable "input" as that interferes with using the input() function.
What speed (baud) do you have the arduino configured to communicate at?
Kiki
Kiki on 29 Mar 2016
Hi
The baud I use is 115200. Do I just set the baud rate for the arduino, or do I need to write other functions in my arduino code in order to use the readVoltage() function?

Sign in to comment.

Tags


Translated by