Slow communication speed with Arduino

24 views (last 30 days)
Hi,
I am using a MAX6675 to measure the temperature of thermocouple. I tried to measure the temperature in two ways. Measure ways: 1. Arduino IDE and 2. Matlab read temperature by using a MAX6675 connected to Arduino. Their execution time is quite different.
%Arduino
void loop() {
unsigned long timeBegin = micros();
float temperature = module.readCelsius();
unsigned long timeEnd = micros();
unsigned long duration = timeEnd- timeBegin;
Serial.print("Temperature: ");
serial.print(temperature);
Serial.println(F("c"));
serial.print("Duration time (us):");
serial.println(duration);delay(1000);
}
The exercution time for Arduino IDE is about 520 microseconds.
%Matlab read temperature by using a MAX6675 connected to Arduino
tic;
T=readCelcius(a,cs,sclk);
toc;
disp(T);
The Matlab elapsed time is 1.616992 seconds.
The execution speed of these two ways are different by 3100 times!! While they are using the same function readCelcius.
How can I make the speed of connecting Arduino to the Matlab execute much more faster?
Any suggestion will be appreciated. Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 24 Dec 2022
To use readCelcius from MATLAB you need to have used arduino() first. The function then places a spiread() call on the arduino object and manipulates the results.
The arduino() interface works by having a command monitor on the arduino side, and the MATLAB side sends a command to the arduino side, which the arduino side has to interpret and place the appropriate commands locally, and then the arduino side has to package the results and send them back, and the MATLAB side has to decode and return appropriate results.
This requires multiple encoding and decoding steps, and requires two or more trips across the USB/serial interface, which involves a packetized command-and-response protocol.
This is, of course, much slower than native code on the arduino that can make direct calls.
I would not expect the slowdown to be as high as you are observing, but it is going to be notably slower.
You can reduce the time by having a custom sketch on the arduino side that listens for requests (anything i/o will do) and runs the spi commands directly and decodes to a temperature that you send back. This would reduce encoding and decoding time but not bus time.
  3 Comments
Walter Roberson
Walter Roberson on 24 Dec 2022
It would be similar to what you already have, but you would add code that looped waiting for input, calling module.readCelsius() and writing out the response. To reduce decoding time, just send the temperature back without the text header. For technical reasons if you can get the response to be 4 bytes or fewer then the exchange can be more efficient (at least in theory)
gdz
gdz on 25 Dec 2022
you would add code that looped waiting for input, calling module.readCelsius() and writing out the response
Everything of the code is in Matlab now. Is this means I have to adding code in the arduino loop, that calling for module.readCelcius? Also the code in the arduino have to run first (module.readCelcius is reading now) and the matlab will read the reading on arduino side and display on Matlab.
However, this is different from understood. From my understanding, it is not able to run both arduino and matlab at the same time, because there is only one port for my PC.
Please correct me if I'm wrong.

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Dec 2022
Edited: Sulaymon Eshkabilov on 24 Dec 2022
Note that in Arduino IDE: delay() command does not give a fixed sampling time.
You had better use: millis()
You can also try to use Data Streamer MS Excel app to record your Arduino Data.
  2 Comments
gdz
gdz on 24 Dec 2022
I have tried to replace micros() to millis(), millis() gives 0 or 1 only.
With the same function readCelcius, they both give a very different elaspse time.Weird....
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Dec 2022
Use Data Streamer MS Excel app to record your Arduino Data that gives very accurate data export from Arduino into MS Excel.

Sign in to comment.

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!