Arduino Pulse Sensor Tutorial | Arduino Heart Rate Sensor
by Utsource in Circuits > Arduino
871 Views, 0 Favorites, 0 Comments
Arduino Pulse Sensor Tutorial | Arduino Heart Rate Sensor
Hi guys in this instructables we will learn how to use Pulse / heart sensor with arduino and we will read heart beat / pulse on the serial monitor of arduino IDE.
Things You Need
For this instructables we will need following things :
Arduino uno
Jumper wires
LED
Pulse rate sensor / heart rate sensor
Sensor Working
When a heartbeat occurs blood is pumped through the human body and gets squeezed into the capillary tissues. The volume of these capillary tissues increases as a result of the heartbeat. But in between the heartbeats (the time between two consecutive heartbeats,) this volume inside capillary tissues decreases. This change in volume between the heartbeats affects the amount of light that will transmit through these tissues. This change is very small but we can measure it with the help of Arduino.
The pulse sensor module has a light which helps in measuring the pulse rate. When we place the finger on the pulse sensor, the light reflected will change based on the volume of blood inside the capillary blood vessels. During a heartbeat, the volume inside the capillary blood vessels will be high. This affects the reflection of light and the light reflected at the time of a heartbeat will be less compared to that of the time during which there is no heartbeat (during the period of time when there is no heartbeat or the time period in between heartbeats, the volume inside the capillary vessels will be lesser. This will lead higher reflection of light). This variation in light transmission and reflection can be obtained as a pulse from the ouptput of pulse sensor. This pulse can be then coditioned to measure heartbeat and then programmed accordingly to read as heartbeat count.
The pulse sensor module has a light which helps in measuring the pulse rate. When we place the finger on the pulse sensor, the light reflected will change based on the volume of blood inside the capillary blood vessels. During a heartbeat, the volume inside the capillary blood vessels will be high. This affects the reflection of light and the light reflected at the time of a heartbeat will be less compared to that of the time during which there is no heartbeat (during the period of time when there is no heartbeat or the time period in between heartbeats, the volume inside the capillary vessels will be lesser. This will lead higher reflection of light). This variation in light transmission and reflection can be obtained as a pulse from the ouptput of pulse sensor. This pulse can be then coditioned to measure heartbeat and then programmed accordingly to read as heartbeat count.
Connections
Connect the pulse sensor with Arduino as follows
GND pin of pulses sensor to GND of Arduino
VCC of pulse sensor to 5V of Arduino
A0 of pulse sensor to A0 of Arduino
After that, connect the LED to pin 13 and GND of Arduino as shown in the figure below. The LED will blink according to the heart beat.
GND pin of pulses sensor to GND of Arduino
VCC of pulse sensor to 5V of Arduino
A0 of pulse sensor to A0 of Arduino
After that, connect the LED to pin 13 and GND of Arduino as shown in the figure below. The LED will blink according to the heart beat.
Code
Please copy the following code and upload it to arduino board :
int sensor_pin = 0;
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();
}
int sensor_pin = 0;
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();
}
Output
After connecting everything together and uploading the code to the arduino Board. Then when we put our finger on the sensor and hold it there then it will start to print the heart beat or pulse rate of a person on the serial monitor of arduino IDE.