Matlab not reading data fast enough from arduino.

28 visualizaciones (últimos 30 días)
Devin Mortenson
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);
}

Respuesta aceptada

Walter Roberson
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.
  2 comentarios
Devin Mortenson
Devin Mortenson el 28 de Oct. de 2021
Thank you this was very informative! You are correct I am using a USB port because I do not have a serial port on my computer.
It looks like I am sometimes recieving as many as 7 characters per reading, any idea what could be causing this? I think solving this may also help explain my sampling issue too.
As for the sampling issue I have reconfigured my sampling rate to 100 Hz (which if I am understanding you correctly should still be doable), and while it helped somewhat it still seems like I am still sampling well bellow 100Hz. For example when I ran the duration for 5 seconds at 100Hz it was only able to achieve 240 of the expected 500 samples.
Thanks in advance for your advice!
Devin Mortenson
Devin Mortenson el 29 de Oct. de 2021
Hey don't worry about replying to this, I found a workaround to grab my data through excel. I looked into it more and it seems like the root of my problem was really the fact that Matlab is just not designed for the real time data aquistion I was attempting to do. Thanks for your initial comment it was very helpful!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by