Matlab not reading data fast enough from arduino.
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Devin Mortenson
el 28 de Oct. de 2021
Comentada: Devin Mortenson
el 29 de Oct. de 2021
My code is able to read data from the serial port correctly (it seems), however for some reason my dataIndex variable is not keeping up with the speed that I am trying to read data at (I think). While I should have 10000 data points using this code my dataIndex variable is ending at way short at about 816 instead. Has anybody else ever had an issue like this?
Another but not as important issue I have been having is this occassional error code:
Warning: Matching failure in format. The format specified for conversion, '%d', is incorrect.
Unable to perform assignment because the left and right sides have a different number of elements.
It only comes up sometimes, and it seems like its because my value variable occasionally becomes a char.
Any advice that could be offered on any of this would be greatly appreciated!
%% Matlab code
clc;
clear;
close all;
% % Closes serial ports
fclose(instrfind());
% % Defines serial connection
arduino = serial('COM11','BaudRate',9600);
% % Initializes serial connection
fopen(arduino);
% % Duration to record data(in seconds).
duration = 10;
% % Time interval between consecutive data reads.
stepTime = .001;
% % Total number of data samples to be recorded.
samples = duration/stepTime;
% % Initialize arrays for storing data and timestamps.
valueArray = zeros(1,samples);
dataIndex = 1;
tObj = tic;
while(toc(tObj) <= duration)
% % Gets data value from serial port
value = fscanf(arduino,'%d');
% % Store the read data in the corresponding data array.
valueArray(dataIndex) = value;
dataIndex = dataIndex + 1;
% % Read data from Arduino pins in intervals specified by the variable 'stepTime'.
pause(stepTime);
end
% % Closes serial ports
fclose(instrfind());
%% Arduino code
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.println(sensorValue);
delay(10);
}
0 comentarios
Respuesta aceptada
Walter Roberson
el 28 de Oct. de 2021
analogRead() returns an integer value in the range 0 to 1023. My calculation is that the mean number of digits required to represent that is 2.91; you have to add a newline to each. So on average you need to transmit about 4 characters per reading.
You have configured 9600 baud. Each character requires 10 baud. So your maximum throughput in items per second is 9600/10/4 = 240.
So if everything was working perfectly, you would be able to read a maximum of 240 samples per second. Not 1000 samples per second like you hoped. For 1000 samples per second, under best circumstances, you would need about 1000*4*10 = 40000 baud, such as 57600.
Provided, that is, that you are using the true serial ports on your arduino, and you are using true serial ports on your computer. (Do you even have true serial ports on your computer?). Especially given the COM11 reference, it is far more likely that you are connecting through the ardunio's serial to USB bridge.
The ardunio serial to USB bridge is limited to at most 1000 transactions per second, but cannot reach that in practice.
See also these (older) benchmarks: https://hackaday.com/2013/06/03/benchmarking-usb-transfer-speeds/
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!