How to increase processing speed of disital input using Arduino

10 views (last 30 days)
I want to disital input using Arduino Uno on MATLAB Support Package for Arduino Hardware
So, I made following code.
a= arduino('COM4','Uno');
for i = 1:100
inA = readDigitalPin(a, 'D5');
However, the processing time was very slowly at "readDigitalPin"
This code spend about 4 s.
Can I speed up the reading disital input?
Best regards.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Feb 2020
You are getting about 25 Hz. Most people report that they cannot do better than about 40 Hz when using this style of interaction with arduino.
If you want something faster you will need to create code for the arduino that continually does the reading of the digital pin and sending the results back. Even if you only do that when prompted MATLAB sending something to the arduino serial port, you would get better performance; best performance would be if the arduino just continually sent data.
Note that the arduino limits the rate at which it sends serial packets over USB, and that rate is pretty far down and difficult to overcome. There is an arduino-like device that uses a different USB interface that can do better, but the device is not as flexible as arduino in other ways. USB itself limits the number of transactions per second.
USB is the wrong interface to use for real-time serial work; it is a bit better for streaming audio or streaming video, which use different parts of the USB protocol that can guarantee time allocations and bandwidth.

More Answers (1)

Yuta Hanazawa
Yuta Hanazawa on 4 Feb 2020
Thank you, Walter.
I understood that this problem is not easy.
In this case, I select another method without connection between arduino and matlab.
  1 Comment
Walter Roberson
Walter Roberson on 4 Feb 2020
A digital I/O card for your system, such as from National Instruments, can potentially get much higher rates... if you use the Data Acquisition Toolbox.
If I understand correctly, Measurement Computing makes some fairly affordable cards that could be used (again, with Data Acquisition Toolbox)
In some cases you can also do hacks such as overlaying the signal onto an audio stream and using the microphone input.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!