361 views (last 30 days)
Valentin Crainic on 3 Mar 2017
Edited: Alberto Mora on 2 Feb 2021
Hi,
I am wondering what is the maximum frequency at which Matlab can sample analog inputs through an Arduino Uno board. The current code that I have been using is below.
. . .
clock = tic;
for j = 1 : (1000)
end % for j
b = toc (clock);
disp 'time: '
disp (b)
. . .
Using this code, it only samples at ~100Hz. Is there any way go get it to sample faster, as Arduino states that the board should be able to sample at 10 000Hz (https://www.arduino.cc/en/Reference/AnalogRead)

Sam McDonald on 6 Mar 2017
Although the maximum reading rate for the analog pin is 10 kHz, this speed cannot be guaranteed with serial communication via Arduino and MATLAB. In other words, there is overhead involved by calling the "readVoltage" function in MATLAB, collecting the signal value from the Arduino pin, and sending it back to MATLAB. It is difficult to say what the theoretical maximum frequency is for data collection, other than performing tests similar to what you have done here.
You can perform a more in-depth analysis on performance by running the profiling tool:
That may help you gain more insight into how much time each function takes to execute, such as the "readVoltage" function.
Valentin Crainic on 7 Mar 2017
Thank you! I will give this a try!

Alberto Mora on 29 Dec 2018
Edited: Alberto Mora on 17 Dec 2020
Dear Valentin, I notice the same problem, and I'm also looking for a solution.
At the moment I have adopted the follow approach with discrete results:
2. Matlab in a while loop, read the data of serial port coming from Arduino
3. Arduino print the analog value on the serial port, like:
Serial.print( millis() ); Serial.println( signal1_column )
In this last case, the speed is limited to the serial bus. In this way I reached a sampling of about 1-2kHz (even 3kHz but not very stable).
Note: a nice tip to speed up the serial bus is to increase the BaudRate, probably 9600 is too small! Anyhow you will not reach higher frequency of few kHz.
Anyhow I am not sure that is the best approach.
Regards,
A
Walter Roberson on 2 Feb 2021
I mentioned about USB. If you are connecting the arduino to the matlab host using USB, then it is impossible to get more than 1000 transactions per second, as that is the USB 2 limit and arduino do not have USB 3. Each transaction can be a full buffer of up to 1020 (iirc) bytes with USB 2.1, slightly different for other USB versions. That is bytes, not samples. Each sample will typically need multiple bytes. If you are using print or println then the data is formatted as text, which can take roughly 4 times as much space as the number of bytes per sample. You also need delimiters between the samples if you are using text; println uses newline delimiter. println is often not as efficient to process as packing multiple text samples per line before a newline. Sending text is never as efficient as sending binary instead.
If you are using USB then the baud rate is mostly ignored.
It is also possible to connect arduino to matlab host using a true rs232 serial port. If that is done then the serial port speed does matter, and the latency is lower and you can get higher transactions per second. Noise can be more of a problem though: USB is more immune to noise because of the hardware specifications and ways it transmits.