Reading Arduino Serial Port into Matlab and 50% of the time getting characters instead of digits.
Exactly as the title says.
Trying to read serial values from an Arduino serial port into a matlab array. The sensor we’re reading from is a heartrate sensor from https://pulsesensor.com/. This is for a science fair project where a full collection is about 30 seconds long and then it is broken into chunks of 500 datapoints. So 6 trials from one collection. 60% of the time we’ll get a sensor reading that is in a doubles format but that other 40% the code seems to read extra characters and defaults back into characters. These characters aren’t random letters either but have plenty of accents and such. (example below the code)
We dont have trouble connecting to the board and when the data is being printed into the matlab command window it shoes numberical values ranging from 0 – 750 depending on the signal voltage. Its only after the loop that the file converts into a char array (usually 3005×1).
Any suggestions on how to ensure that the file stays in double and doesnt convert into a character array? I think it might have to do with the sampling frequency and aligning the arduino with the matlab code reading it but I’m not very familar with the process. Any suggestions would be greatly appreciated.
% Define the port where Arduino is connected
arduinoPort = ‘COM3’; % Replace ‘COM3’ with your actual port
baudRate = 9600; % Set the baud rate to match the Arduino’s baud rate
s = serial(arduinoPort, ‘BaudRate’, baudRate); % Create a serial object for the Arduino
% Initialize an empty array to store PPG data
ppgData = [];
numSamples = 3000; % Set the number of samples to collect (can be adjusted)
disp(‘Collecting PPG data…’); % Display message indicating data collection has started
j=1
fopen(s); % Open the serial port for communication
while j < numSamples+1
% Check if there is data available in the serial buffer
if s.BytesAvailable > 0
i = 1:numSamples; % Loop to collect the specified number of samples
% Read data from the serial port as a string and convert to an integer
data = fscanf(s, ‘%d’);
disp(data); % Display the received data to verify if values are being read
ppgData = [ppgData, data]; % Append the new data to the PPG data array
j=j+1;
%pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
else
disp(‘No data available in buffer.’); % Warn if no data is available
end
pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
end
ppgData = ppgData’;
data = reshape(ppgData, [500, 6]);
‘¼’
‘Ç’
‘Ì’
‘Ê’
‘à’
‘à’
‘ç’
‘ó’
‘ñ’
‘Ā’
‘ý’
‘û’
‘Ă’
‘ó’
‘ò’
‘ì’
‘×’
‘ß’
‘Î’
‘Ç’
‘¿’
‘«’
‘«Exactly as the title says.
Trying to read serial values from an Arduino serial port into a matlab array. The sensor we’re reading from is a heartrate sensor from https://pulsesensor.com/. This is for a science fair project where a full collection is about 30 seconds long and then it is broken into chunks of 500 datapoints. So 6 trials from one collection. 60% of the time we’ll get a sensor reading that is in a doubles format but that other 40% the code seems to read extra characters and defaults back into characters. These characters aren’t random letters either but have plenty of accents and such. (example below the code)
We dont have trouble connecting to the board and when the data is being printed into the matlab command window it shoes numberical values ranging from 0 – 750 depending on the signal voltage. Its only after the loop that the file converts into a char array (usually 3005×1).
Any suggestions on how to ensure that the file stays in double and doesnt convert into a character array? I think it might have to do with the sampling frequency and aligning the arduino with the matlab code reading it but I’m not very familar with the process. Any suggestions would be greatly appreciated.
% Define the port where Arduino is connected
arduinoPort = ‘COM3’; % Replace ‘COM3’ with your actual port
baudRate = 9600; % Set the baud rate to match the Arduino’s baud rate
s = serial(arduinoPort, ‘BaudRate’, baudRate); % Create a serial object for the Arduino
% Initialize an empty array to store PPG data
ppgData = [];
numSamples = 3000; % Set the number of samples to collect (can be adjusted)
disp(‘Collecting PPG data…’); % Display message indicating data collection has started
j=1
fopen(s); % Open the serial port for communication
while j < numSamples+1
% Check if there is data available in the serial buffer
if s.BytesAvailable > 0
i = 1:numSamples; % Loop to collect the specified number of samples
% Read data from the serial port as a string and convert to an integer
data = fscanf(s, ‘%d’);
disp(data); % Display the received data to verify if values are being read
ppgData = [ppgData, data]; % Append the new data to the PPG data array
j=j+1;
%pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
else
disp(‘No data available in buffer.’); % Warn if no data is available
end
pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
end
ppgData = ppgData’;
data = reshape(ppgData, [500, 6]);
‘¼’
‘Ç’
‘Ì’
‘Ê’
‘à’
‘à’
‘ç’
‘ó’
‘ñ’
‘Ā’
‘ý’
‘û’
‘Ă’
‘ó’
‘ò’
‘ì’
‘×’
‘ß’
‘Î’
‘Ç’
‘¿’
‘«’
‘« Exactly as the title says.
Trying to read serial values from an Arduino serial port into a matlab array. The sensor we’re reading from is a heartrate sensor from https://pulsesensor.com/. This is for a science fair project where a full collection is about 30 seconds long and then it is broken into chunks of 500 datapoints. So 6 trials from one collection. 60% of the time we’ll get a sensor reading that is in a doubles format but that other 40% the code seems to read extra characters and defaults back into characters. These characters aren’t random letters either but have plenty of accents and such. (example below the code)
We dont have trouble connecting to the board and when the data is being printed into the matlab command window it shoes numberical values ranging from 0 – 750 depending on the signal voltage. Its only after the loop that the file converts into a char array (usually 3005×1).
Any suggestions on how to ensure that the file stays in double and doesnt convert into a character array? I think it might have to do with the sampling frequency and aligning the arduino with the matlab code reading it but I’m not very familar with the process. Any suggestions would be greatly appreciated.
% Define the port where Arduino is connected
arduinoPort = ‘COM3’; % Replace ‘COM3’ with your actual port
baudRate = 9600; % Set the baud rate to match the Arduino’s baud rate
s = serial(arduinoPort, ‘BaudRate’, baudRate); % Create a serial object for the Arduino
% Initialize an empty array to store PPG data
ppgData = [];
numSamples = 3000; % Set the number of samples to collect (can be adjusted)
disp(‘Collecting PPG data…’); % Display message indicating data collection has started
j=1
fopen(s); % Open the serial port for communication
while j < numSamples+1
% Check if there is data available in the serial buffer
if s.BytesAvailable > 0
i = 1:numSamples; % Loop to collect the specified number of samples
% Read data from the serial port as a string and convert to an integer
data = fscanf(s, ‘%d’);
disp(data); % Display the received data to verify if values are being read
ppgData = [ppgData, data]; % Append the new data to the PPG data array
j=j+1;
%pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
else
disp(‘No data available in buffer.’); % Warn if no data is available
end
pause(0.01); % Pause for 10 milliseconds to allow time for new data to arrive
end
ppgData = ppgData’;
data = reshape(ppgData, [500, 6]);
‘¼’
‘Ç’
‘Ì’
‘Ê’
‘à’
‘à’
‘ç’
‘ó’
‘ñ’
‘Ā’
‘ý’
‘û’
‘Ă’
‘ó’
‘ò’
‘ì’
‘×’
‘ß’
‘Î’
‘Ç’
‘¿’
‘«’
‘« arduino, fscanf, char, arduino serial MATLAB Answers — New Questions