How to get pulse sensor reading using matlab?

5 views (last 30 days)
Hello im a diploma student doing a project base on a pulse sensor with gui for my final year i am new to matlab im only able to use arduino ide to get pulse reading .im not sure how to get the pulse sensor reading using matlab can some one help. here is a coding for the pulse sensor i used using arduino ide.
int led_pin = 13;
volatile int heart_rate;
volatile int analog_data;
volatile int time_between_beats = 600;
volatile boolean pulse_signal = false;
volatile int beat[10]; //heartbeat values will be sotred in this array
volatile int peak_value = 512;
volatile int trough_value = 512;
volatile int thresh = 525;
volatile int amplitude = 100;
volatile boolean first_heartpulse = true;
volatile boolean second_heartpulse = false;
volatile unsigned long samplecounter = 0; //This counter will tell us the pulse timing
volatile unsigned long lastBeatTime = 0;
void setup()
{
pinMode(led_pin,OUTPUT);
Serial.begin(115200);
interruptSetup();
}
void loop()
{
Serial.print("BPM: ");
Serial.println(heart_rate);
delay(200); // take a break
}
void interruptSetup()
{
TCCR2A = 0x02; // This will disable the PWM on pin 3 and 11
OCR2A = 0X7C; // This will set the top of count to 124 for the 500Hz sample rate
TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
TIMSK2 = 0x02; // This will enable interrupt on match between OCR2A and Timer
sei(); // This will make sure that the global interrupts are enable
}
ISR(TIMER2_COMPA_vect)
{
cli();
analog_data = analogRead(sensor_pin);
samplecounter += 2;
int N = samplecounter - lastBeatTime;
if(analog_data < thresh && N > (time_between_beats/5)*3)
{
if (analog_data < trough_value)
{
trough_value = analog_data;
}
}
if(analog_data > thresh && analog_data > peak_value)
{
peak_value = analog_data;
}
if (N > 250)
{
if ( (analog_data > thresh) && (pulse_signal == false) && (N > (time_between_beats/5)*3) )
{
pulse_signal = true;
digitalWrite(led_pin,HIGH);
time_between_beats = samplecounter - lastBeatTime;
lastBeatTime = samplecounter;
if(second_heartpulse)
{
second_heartpulse = false;
for(int i=0; i<=9; i++)
{
beat[i] = time_between_beats; //Filling the array with the heart beat values
}
}
if(first_heartpulse)
{
first_heartpulse = false;
second_heartpulse = true;
sei();
return;
}
word runningTotal = 0;
for(int i=0; i<=8; i++)
{
beat[i] = beat[i+1];
runningTotal += beat[i];
}
beat[9] = time_between_beats;
runningTotal += beat[9];
runningTotal /= 10;
heart_rate = 60000/runningTotal;
}
}
if (analog_data < thresh && pulse_signal == true)
{
digitalWrite(led_pin,LOW);
pulse_signal = false;
amplitude = peak_value - trough_value;
thresh = amplitude/2 + trough_value;
peak_value = thresh;
trough_value = thresh;
}
if (N > 2500)
{
thresh = 512;
peak_value = 512;
trough_value = 512;
lastBeatTime = samplecounter;
first_heartpulse = true;
second_heartpulse = false;
}
sei();
}
  1 Comment
Swapnil Dhage
Swapnil Dhage on 23 Jun 2021
here ,
same problem
plz help me ...
i do copy this code but , pulse sensor is not yet coonected to thingsepak..
this is my number- 8668835936

Sign in to comment.

Answers (1)

Aniket
Aniket on 30 Jan 2025
To read pulse sensor data using MATLAB, you can use the MATLAB Support Package for Arduino, which allows you to interface with Arduino hardware directly from MATLAB. Kindly follow the below steps to setup your MATLAB environment to read pulse sensor data:
  1. Install MATLAB Support Package for Arduino: Navigate to "Add-Ons" under Home Tab and search for "MATLAB Support Package for Arduino".
  2. Set-up your Arduino in MATLAB: Connect arduino board to computer.
  3. Write MATLAB code to read Pulse Sensor data:
% Define the COM port and the baud rate
arduinoObj = arduino('COM3', 'Uno', 'Libraries', 'ExampleAddon');
% Define the pin where the pulse sensor is connected
sensorPin = 'A0';
% Initialize variables
heartRate = 0;
samplePeriod = 0.5; % Sampling period in seconds
% Start reading data
disp('Reading pulse sensor data...');
while true
% Read analog data from the pulse sensor
analogData = readVoltage(arduinoObj, sensorPin);
% Process the analog data to compute heart rate
% Note: Implement your signal processing logic here
% Display the heart rate
fprintf('Heart Rate: %.2f BPM\n', heartRate);
% Pause for the sample period
pause(samplePeriod);
end
4. Implement Signal Processing Logic to compute the heart rate from analog data
Kindly make sure to replace 'COM3' with the actual COM port your Arduino is connected to.
Refer to the following page for extensive information on Arduino Programming in MATLAB: https://www.mathworks.com/discovery/arduino-programming-matlab-simulink.html
Hope this helps!

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!