Monitor the Heart Rate using Pulse Sensor and Arduino

The Pulse Sensor is a well-designed low-power plug-and-play heart-rate sensor for the Arduino. It can be used by students, artists, athletes, manufacturers, and game & mobile developers who want to incorporate live heart-rate data into their projects.

And the best part is that this sensor plugs right into Arduino and easily clips onto a fingertip or earlobe. It is also super small (button-shaped) with holes, so it can be sewn into fabric.

Did you know?

The Pulse Sensor is an open source device originally created by PulseSensor.com. They started in 2011 as a Kickstarter project. As of 2013, 491 backers pledged $18,418 to help bring this project to life.

Hardware Overview

The front of the sensor is the side with the heart logo. This is where you place your finger. On the front side you will see a small round hole, from where the Kingbright’s reverse mounted green LED shines.

pulse sensor front side hardware overview

Just below the LED is a small ambient light photo sensor – APDS-9008 from Avago, similar to that used in cell phones, tablets and laptops, to adjust the screen brightness in different light conditions.

On the back of the module you will find the rest of the components including a microchip’s MCP6001 Op-Amp and a bunch of resistors and capacitors that make up the R/C filter network. There is also a reverse protection diode to prevent damage if the power leads are accidentally reversed.

pulse sensor back side hardware overview

The module operates from a 3.3 to 5V DC Voltage supply with a operating current of < 4mA.

Here are the technical specifications:

Maximum RatingsVCC3.0 – 5.5V
IMax (Maximum Current Draw)< 4mA
VOut (Output Voltage Range)0.3V to Vcc
WavelengthLED Output565nm
Sensor Input525nm
DimensionsL x W (PCB)15.8mm (0.625″)
Lead Length20cm (7.8″)

How Pulse Sensor Works?

Optical heart-rate sensors are very easy to understand in theory. If you’ve ever shined a flashlight through your finger tips and seen your heartbeat pulse, you have a good handle on the theory of optical heart-rate pulse sensors.

light sensor led pulse detection photoplethysmogram

A pulse sensor or any optical heart-rate sensor, for that matter, works by shining a green light (~ 550nm) on the finger and measuring the amount of reflected light using a photosensor.

This method of pulse detection through light is called Photoplethysmogram.

pulse detection heart rate sensor working photoplethysmogram

The oxygenated hemoglobin in the arterial blood has the characteristic of absorbing green light. The redder the blood (the higher the hemoglobin), the more green light is absorbed. As the blood is pumped through the finger with each heartbeat, the amount of reflected light changes, creating a changing waveform at the output of the photosensor.

As you continue to shine light and take photosensor readings, you quickly start to get a heart-beat pulse reading.

This signal from the photosensor is generally small and noisy, therefore the signal is passed through an R/C filter network and then amplified using an Op Amp to create a signal that is much larger, cleaner and easier to detect.

Pulse Sensor Pinout

The sensor comes with a 24” flat ribbon cable with 3 male header connectors. The following diagram shows the pinout.

pulse sensor pinout

S (Signal) is the signal output. Connects to analog input of an Arduino.

+ (VCC) is the VCC pin. Connects to 3.3 or 5V.

– (GND) is the Ground pin.

Warning:

Many times the cable is not color coded, so make sure you look at the markings on the back of the module to ensure that you have the correct identification of the three wires.

Wiring Pulse Sensor with Arduino

Hooking up the Pulse Sensor to an Arduino is super simple. You only need to connect three wires: two for power and one for reading the sensor value.

The module can be powered from 3.3 or 5V. The positive voltage connects to ‘+’ and ground connects to ‘-‘. The 3rd ‘S’ wire is the analog signal output from the sensor and this will connect to the A0 analog input of an Arduino.

Below is the hookup for the experiments with the Pulse Sensor:

wiring connecting pulse sensor with arduino

Library Installation

In order to run the subsequent sketches you’ll need to install a library called ‘PulseSensor Playground’.

To install the library, navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.

manage libraries

Filter your search by typing ‘pulsesensor’. There should be only one entry. Click on that, and then select Install.

pulse sensor library installation

PulseSensor Example Sketches

The PulseSensor library has a number of example sketches. We’ll go over few of them here but you can also try the other ones. You can use these example sketches as a basis for developing your own code.

To access the example sketches, navigate to the File > Examples > PulseSensor Playground You will see a selection of example sketches. Select any of them to load the sketch into your IDE. Let’s begin with the GettingStartedProject.

pulse sensor example sketches

Load the GettingStartedProject sketch from the example sketches into your Arduino IDE. This is a basic Arduino sketch. Go ahead and upload it to your Arduino, and place the Pulse Sensor on your fingertip. You should see Arduino’s built-in LED blink with your heartbeat!

int const PULSE_SENSOR_PIN = 0;   // 'S' Signal pin connected to A0

int Signal;                // Store incoming ADC data. Value can range from 0-1024
int Threshold = 550;       // Determine which Signal to "count as a beat" and which to ignore.

void setup() {
	pinMode(LED_BUILTIN,OUTPUT);  // Built-in LED will blink to your heartbeat
	Serial.begin(9600);           // Set comm speed for serial plotter window
}

void loop() {

	Signal = analogRead(PULSE_SENSOR_PIN); // Read the sensor value

	Serial.println(Signal);                // Send the signal value to serial plotter

	if(Signal > Threshold){                // If the signal is above threshold, turn on the LED
		digitalWrite(LED_BUILTIN,HIGH);
	} else {
		digitalWrite(LED_BUILTIN,LOW);     // Else turn off the LED
	}
	delay(10);
}

Code Explanation

The sketch is super simple. It begins with defining the pins used to connect the Pulse Sensor. Two variables are also defined; the Signal holds the incoming ADC data and the Threshold determines which signal to “count as a beat” and which to ignore.

int const PULSE_SENSOR_PIN = 0;

int Signal;
int Threshold = 550;

In the setup, we define the Built-in LED pin (pin 13) as output and set up the serial monitor.

void setup() {
	pinMode(LED_BUILTIN,OUTPUT);
	Serial.begin(9600);
}

In the loop, we read in the analog signal from the Pulse Sensor, and when the signal exceeds a threshold value, we turn the Built-in LED on.

void loop() {
	Signal = analogRead(PULSE_SENSOR_PIN); // Read the sensor value

	if(Signal > Threshold){                // If the signal is above threshold, turn on the LED
		digitalWrite(LED_BUILTIN,HIGH);
	} else {
		digitalWrite(LED_BUILTIN,LOW);     // Else turn off the LED
	}
	delay(10);
}

Trouble Seeing a Heartbeat?

If you’re having trouble seeing a heartbeat, here’s what to do.

  1. If you hold the sensor too hard, you will squeeze all the blood from your fingers and there will be no sign! If you hold it too lightly, you will invite noise from movement and ambient light. Sweatspot pressure (Not too hard, not too soft) on the pulse sensor will give a good clean signal.
  2. A varying pressure can cause blood to flow differently in your finger, causing the sensor readings to go wonky. Try to apply constant pressure by attaching the sensor to your finger using a rubber band or other tightening device.
  3. Try the sensor on different parts of your body that have capillary tissue (such as earlobe or lower lip).
  4. Try to adjust the threshold value. The threshold value tells Arduino when to find a pulse that is valid. The threshold can be any number between 0 to 1023, but try to adjust by 5 or 10 steps. Lowering the threshold increases sensitivity and vice-versa. See if you can find a better range for your finger than the default value.

Arduino Code – Heart Beat Plotting

The previous GettingStartedProject sketch is designed to work with the Arduino Serial Plotter, a cool tool that comes with the Arduino IDE for visualizing the analog signals in real-time. While the sketch is running and your Arduino board is connected to your computer USB, click on: Tools > Serial Plotter

The signal may take a moment to settle, but you should see something like this.

pulse sensor heart rate plotting on arduino serial monitor

Feel the pulse in your wrist and watch it mimic the blips.

Arduino Code – Measuring Heart-Rate (BPM)

For our second example, we will try to measure heart rate (Beats Per Minute or BPM). Look in the PulseSensor Playground examples menu for Getting_BPM_to_Monitor and load it into your Arduino IDE.

pulse sensor bpm heart ratemeasurement sketch

This sketch calculates the time between pulses to obtain the heart rate and displays it on the Serial Monitor.

Warning:

This sketch detects heart-rate optically. This method is tricky and prone to give false readings. So please DO NOT use it for actual medical diagnosis.

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library

const int PulseWire = 0;       // 'S' Signal pin connected to A0
const int LED13 = 13;          // The on-board Arduino LED
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore
                               
PulseSensorPlayground pulseSensor;  // Creates an object

void setup() {
	Serial.begin(9600);

	// Configure the PulseSensor object, by assigning our variables to it
	pulseSensor.analogInput(PulseWire);   
	pulseSensor.blinkOnPulse(LED13);       // Blink on-board LED with heartbeat
	pulseSensor.setThreshold(Threshold);   

	// Double-check the "pulseSensor" object was created and began seeing a signal
	if (pulseSensor.begin()) {
		Serial.println("PulseSensor object created!");
	}
}

void loop() {
	int myBPM = pulseSensor.getBeatsPerMinute();      // Calculates BPM

	if (pulseSensor.sawStartOfBeat()) {               // Constantly test to see if a beat happened
		Serial.println("♥  A HeartBeat Happened ! "); // If true, print a message
		Serial.print("BPM: ");
		Serial.println(myBPM);                        // Print the BPM value
		}

	delay(20);
}

After uploading the sketch, keep your finger as steady as possible and wait a few seconds for the readings to make sense. You will see a result like this.

pulse sensor heart rate bpm measurement sketch output

Processing Visualizer

The makers of Pulse Sensor have put together a bit of software to visualize the Pulse Sensor data on your computer. It is written in Processing. This software displays all of the data that the Arduino receives from Pulse Sensor. It graphs the user’s pulse in real time. It also graphs the BPM (Beats Per Minute) and IBI (Interbeat Interval) over time.

This Processing sketch does not perform any calculations! They are all done on the Arduino board, so you need an Arduino running the PulseSensor_BPM sketch to use the visualizer. This software simply reads the Serial Port and visualizes the data it receives.

pulse sensor processing visualizer output

Upload Sketch

Look in the File > Examples > PulseSensor Playground examples menu for PulseSensor_BPM and load it into your Arduino IDE.

pulse sensor processing visualizer arduino sketch

Before running the PulseSensor_BPM sketch, in order for your Arduino board to talk to the visualizer, you will need to change a variable called outputType in the Arduino code. By default it is set to SERIAL_PLOTTER. You need to change it to PROCESSING_VISUALIZER.

changes in processing visualizer arduino sketch

Then, upload the Arduino Sketch and your board is ready to send data the way the Processing Visualizer likes to get it!

Installation

Download the Processing code from github. Unzip the download, and take the folder called PulseSensorAmpd_Processing_Visualizer and place it in your Documents/Processing folder.

Then open Processing to access the code through your Sketch folder. Select File > Sketchbook… and select PulseSensorAmped_Processing_Visualizer

pulse sensor processing visualizer sketch

Setup

The Sketch will launch, and you will be prompted to select the USB port associated with your Arduino board. Use the Refresh Serial Ports button to update the ports list if you don’t see your Arduino.

pulse sensor processing visualizer port selection

Once you select the right port, you will start seeing hearbeat data!

Functions

There are a couple of cool things you can do while the sketch is running:

  1. Press ‘s’ to take a screenshot of the program window. The image will be saved in the sketch folder as a .jpg
  2. Press ‘r’ to reset the data windows to zero.