Maximum sampling frequency of ANALOG INPUT through Arduino UNO
151 views (last 30 days)
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)
voltage (j) = readVoltage(a,'A0'); %analog in pin 'A0'
end % for j
b = toc (clock);
disp 'time: '
. . .
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)
Thanks in advance!!
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.
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:
- analogRead in Arduino environment
- Matlab in a while loop, read the data of serial port coming from Arduino
- 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.