Raspberry Pi Heartbeat / Pulse measuring

24. March 2017

Not only with fitness watches, but also with the Raspberry Pi it is possible to measure pulse / heart rates. With a small module – the Raspberry Pi pulse sensor – you can easily measure your own pulse and thus monitor permanently your health condition. This sensor can also be used in mobile Raspberry Pi applications (fitness tracker project? ;-)) thanks to low power consumption.

What is still needed and which libraries can be used are shown in this tutorial.

Heart Rate Monitor Accessories

The Raspberry Pi pulse sensor can not be read out digitally, thus we need an analog-to-digital converter. Such an ADC makes it possible to read out analog signals on the Raspberry Pi because. Unlike the Arduino, the Pi has no integrated analog IO pins.

I used the following components in the video below:

  • Heart rate Pulse sensor: US / UK
  • MCP3008 ADC: US / UK
  • Small Breadboard: US / UK
  • Jumper Wire: US / UK

Some sensors are shipped with a Velcro fastener for the finger, which is advantageous for the measurement, since there is not too much pressure on the finger.


Raspberry Pi Heart Rate Monitor – Connection

Since this sensor has also been designed for the Arduino, it does not provide a digitally readable signal. In order to be able to read the analogue signal, an ADC such as the MCP3008 is required. It doesn’t matter which channel of the ADC you are using, as long as you adjust it later in the code.

The positive pole of the pulse sensor is connected to 3.3V from the Raspberry Pi, as well as Minus to Ground. I have connected the signal / data pin (marked with an “S”) to channel 0 of the MCP3008.


The ADC uses the SpiDev library, which must be installed.


Code for reading the Heart / Pulse Rate

If you don’t use a current Raspbian / NOOBS operating system, you may need to update and install git.

sudo apt-get update
sudo apt-get upgrade sudo apt-get install git

On GitHub I have provided a library for recording the heart rate. You can download it as follows:

git clone https://github.com/tutRPi/Raspberry-Pi-Heartbeat-Pulse-Sensor

Also attached is a sample file, which reads the heart rate every second. In the video below you can see this visualized.
To start the example we switch to the folder and call the example.py.

cd Raspberry-Pi-Heartbeat-Pulse-Sensor/
sudo python example.py

If you want to stop, press CTRL+C.

If you look at the example more closely, you will find that with p.startAsyncBPM() a thread is started, which measures the pulse in the background. By p.stopAsyncBPM() you can stop this thread – and thus the measurement. Feel free to customize the code.


Raspberry Pi Pulse Sensor Example and Extension

In the following video I have run the sample file and the measurement of my pulse is shown. Before that, I measured my heart rate with my smartphone (integrated heart rate monitor), which showed me a resting pulse of 55 BPM.


As you can see, the measurement is not always accurate, especially if the finger does not touch the pulse sensor. Here, one could use an additional pressure sensor or something similar. Then, it should only measure if you are sure that the finger touches the pulse sensor. It also takes a few seconds for the pulse to be recorded correctly, but this is not the case with commercially available pulse meters.


For the module (originally developed for Arduino), there is also some sample code. In addition, there is a nice visualisation of the values on Github. It should not be too much effort to customize these files for my library. If you want to change the code, you may also change my library according to the requirements. I would be happy about a comment 🙂

Raspberry Pi Puls Heartbeat Monitor

The data provide a better overview of the pulse history, when visualized.


15 Responses

  1. Hi, thanks for the data!! just a small question.. i didnt see how can i create window with average BPM value( didnt see any printing in code).

    how did you do it?


    • I haven’t used it in this tutorial. But there are libraries on GitHub (for arduino), which visualize it. It should be easy to change the code and run it on the Pi.

      • on your breadboard. I mean the VCC is from the pulse sensor right but I do not see any male to female jumper to connect it from the breadboard to 3.3v pin. Newbie here

  2. Hi there. I made one but the bpm does not seems to be accurate because it will still get the reading even if I don’t put any finger on the sensor. Please help me

  3. thanks to code its very help me. but, i want to ask because there are some error on my code.
    in examples.py “i find AttributeError : pulsesensor instance has no attribute ‘stratAsyncBPM'”


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!