Heart Beat Sensor with Arduino

SuperKitz.com ​Project Manual

Arduino Heart Rate Monitor DIY Kit

Measure your heartbeat with Arduino (Sensor XD-58C)

This Superkits  DIY project with Arduino using the XD-58C sensor. This sensor does nothing but measure heart rate of the heart, it can be found in any medical equipment that is used in measuring heart rate. It can be worn on the finger or on the earlobe.

In this article, we are going to interface a Pulse Sensor with Arduino. The pulse sensor we are going to use is a plug and play heart rate sensor. This sensor is quite easy to use and operate. Place your finger on top of the sensor and it will sense the heartbeat by measuring the change in light from the expansion of capillary blood vessels.

Suitable for all projects that require heart rate data.

Requirements :

  • Computer with an internet connection
  • Download and install Arduino IDE

Components

Arduino uno

Jumper wires

Pulse Sensor (Sensor XD-58C)

Hardware

Getting
Started with
Arduino UNO

Arduino Uno

Although there are many different types of Arduino boards available, this manual focuses on the Arduino Uno model. This is the most popular Arduino board around. So what makes this thing tick? Here are the specifications:

Arduino Uno is a microcontroller board based on the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz .a USB connection, a power jack, an ICSP header, and a reset button.

  • Input Voltage: 7-12V

  • Number of analog inputs:

     

Another wonderful feature of the Arduino is the ability to use what are called “shields”, or add-on boards. Although shields will not be covered in this manual, they are a really neat way to extend the features and functionality of your Arduino.

Pin out diagram
Arduino UNO

Pulse Sensor (Sensor XD-58C)

There is also a LED in the center of this sensor module which helps in detecting the heartbeat. Below the LED, there is a noise elimination circuitry which is supposed to keep away the noise from affecting the readings.

Working – Pulse Sensor

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.

 

Circuit Diagram

 

STEP1:

Pulse Sensor and Arduino – Interfacing

Wire up the data lines and power lines of the Pulse Sensor.  

  • Pulse Sensor GND: Ground Pin
  • Pulse SensorVCC: 5V or 3V Pin
    • Pulse Sensor Singal :A0 Analog Pin

Wire up the Pulse Sensor
With Arduino

Software

Getting started with Arduino

Before starting any project, you need to get your Arduino talking to your computer. This allows you to write and compile code for the Arduino to execute, as well as providing a way for your Arduino to work alongside your computer.

Installing The Arduino Software Package On Windows

Head over to the Arduino website superkitzs.com and download a version of the Arduino software suitable for your version of Windows. Once downloaded, follow the instructions to install the Arduino Integrated Development Environment (IDE).

Download Arduino IDE

 

Connect your Uno board with an A B USB cable; sometimes this cable is called a USB printer cable

 

If you used the Installer, as soon as you connect your board the Windows – from XP up to 10 – will install drivers automatically

If the board is not properly recognized when the zip package is downloaded and expanded, please follow the procedure below.

  • START menu> CONTROL PANEL MENU
  • From the control panel, check for System and Security
  • Select system
  • Select Device Manager from the System window
  • Select open port named “Arduino UNO (COMxx)”, under Ports (COM & LPT). If there is no COM & LPT section, check “Other Devices” for “Unknown Device”.
  • Choose the “Update Driver Software” option by right-clicking on the “Arduino UNO (COmxx)” port
  • Navigate to the “Browse my computer for Driver software” option.
  • Choose the driver file named “arduino. inf”, located in the “Drivers” folder of the Arduino Software download (not the “FTDI USB Drivers” sub-directory). If you are using an old version of the IDE (1.0.3 or older), Choose the Uno driver file named “Arduino UNO.inf”, If an old version of the IDE (1.0.3 or older) is used
  • Thus the windows driver installation is completed

Select your board type : Arduino UNO and select theport

Select the serial device of the board from the Tools | Serial Port menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.

Press CNTRL + A & press DELETE to clear the Arduino IDE page

Upload the program

Copy the Sketch Code given below And try to understand 

Now, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.

Sketch Code


int pulsePin = A0;
int blinkPin = 13;


volatile int BPM;
volatile int Signal;
volatile int IBI = 600;
volatile boolean Pulse = false;
volatile boolean QS = false;
static boolean serialVisual = true;

volatile int rate[10];
volatile unsigned long sampleCounter = 0;
volatile unsigned long lastBeatTime = 0;
volatile int P = 512;
volatile int T = 512;
volatile int thresh = 525;
volatile int amp = 100;
volatile boolean firstBeat = true;
volatile boolean secondBeat = false;

void setup()
{
  pinMode(blinkPin, OUTPUT);
  Serial.begin(115200);
  interruptSetup();
}


void loop()
{
  serialOutput();

  if (QS == true)
  {

    serialOutputWhenBeatHappens();
    QS = false;
  }

  delay(20);
}


void interruptSetup()
{

  TCCR2A = 0x02;
  TCCR2B = 0x06;
  OCR2A = 0X7C;
  TIMSK2 = 0x02;
  sei();
}

void serialOutput()
{
  if (serialVisual == true)
  {
    arduinoSerialMonitorVisual('-', Signal);
  }
  else
  {
    sendDataToSerial('S', Signal);
  }
}

void serialOutputWhenBeatHappens()
{
  if (serialVisual == true)
  {
    Serial.print("BPM: ");
    Serial.println(BPM);
  }
  else
  {
    sendDataToSerial('B', BPM);
    sendDataToSerial('Q', IBI);
  }
}

void arduinoSerialMonitorVisual(char symbol, int data )
{
  const int sensorMin = 0;
  const int sensorMax = 1024;
  int sensorReading = data;
  int range = map(sensorReading, sensorMin, sensorMax, 0, 11);

}


void sendDataToSerial(char symbol, int data )
{
  Serial.print(symbol);
  Serial.println(data);
}

ISR(TIMER2_COMPA_vect)
{
  cli();
  Signal = analogRead(pulsePin);
  sampleCounter += 2;
  int N = sampleCounter - lastBeatTime;

  if (Signal < thresh && N > (IBI / 5) * 3)
  {
    if (Signal < T)
    {
      T = Signal;
    }
  }

  if (Signal > thresh && Signal > P)
  {
    P = Signal;
  }



  if (N > 250)
  {
    if ( (Signal > thresh) && (Pulse == false) && (N > (IBI / 5) * 3) )
    {
      Pulse = true;
      digitalWrite(blinkPin, HIGH);
      IBI = sampleCounter - lastBeatTime;
      lastBeatTime = sampleCounter;

      if (secondBeat)
      {
        secondBeat = false;
        for (int i = 0; i <= 9; i++)
        {
          rate[i] = IBI;
        }
      }

      if (firstBeat)
      {
        firstBeat = false;
        secondBeat = true;
        sei();
        return;
      }

      word runningTotal = 0;

      for (int i = 0; i <= 8; i++)
      {
        rate[i] = rate[i + 1];
        runningTotal += rate[i];
      }

      rate[9] = IBI;
      runningTotal += rate[9];
      runningTotal /= 10;
      BPM = 60000 / runningTotal;
      QS = true;

    }
  }

  if (Signal < thresh && Pulse == true)
  {
    digitalWrite(blinkPin, LOW);
    Pulse = false;
    amp = P - T;
    thresh = amp / 2 + T;
    P = thresh;
    T = thresh;
  }

  if (N > 2500)
  {
    thresh = 512;
    P = 512;
    T = 512;
    lastBeatTime = sampleCounter;
    firstBeat = true;
    secondBeat = false;
  }

  sei();
} 

Output

Opening Serial Monitor

Tools –> Serial Monitor

HeartBeat Pulse Monitor

When the pulse sensor sense the heartbeat by measuring the change in light from the expansion of capillary blood vessels. it will display  the Heart Beat in s  Serial Monitor (Make sure that baud rate is 115200). 

Leave a Reply