Slow communication speed with Arduino
19 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
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
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)
More Answers (1)
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
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.
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!