MATLAB: Pulse Sensor data from Arduino to Matlab

arduino real-time plotting matlab

Hi
I'm new to matlab but a beginner in using arduino. So I'm using a pulse sensor to detect the heart rate and want real-time plotting. The input is A0 and the output is a led light at pin 13. I get the data from the serial monitor but I'm not sure how to transfer into matlab. I have download the arduinoIo package and so far my code is this:
if true
% code
end
clear a;
a = arduino('COM1')
a.pinMode(13, 'OUTPUT');
a.pinMode(A0, 'INPUT')
Can anyone please help thanks

Best Answer

Hi,
Embed this code in yours. This code will read serial data from port COM1.
arduino = serial('COM1', 'BaudRate', 9600) % Baud rate=9600, which can be altered accordingly.
i=0;
while (i<100) % For 100 times execution
fopen(arduino);
fprintf(arduino,'Your serial data');
outp = fscanf(arduino) % output is displayed. Data type is char.
numout= str2double(outp) % For output in double data type.
i=i+1;
end
Use Serial.print(/ sensor data here /) in arduino code instead of Serial.println() and select the data useful for you using the below line of code. (This is useful if sensor gives multiple outputs)
Eg.:
sensor_data= str2double(outp(1:5)) % For first 5 characters
Related Question