Raspberry Pi Heartbeat / Pulse measuring
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:
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
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 🙂