Raspberry Pi Zero Heart Rate Monitor

This blog post is an updated version of a chapter from my book “Wearable-Tech Projects with the Raspberry Pi Zero“.

Wearable-Tech Projects with the Raspberry Pi Zero

It has been updated for the latest Raspberry Pi OS Lite (Buster at the time of writing). You will need a copy of this on an SD Card, all of the parts listed in the bill of materials below and to be comfortable with working on the command line and over SSH to follow along.

Let’s begin…

Bill of parts

Setting up the hardware

To connect our 5V Pulse Sensor to our 3.3V Environ pHAT Analogue to Digital Converter we need to construct a voltage divider using the 3 x 330 Ω resistors. The two pictures below show you how we would do this using a breadboard and then how you can do this by soldering the parts directly.

When you have connected the Pulse Sensor to the voltage divider, you can trim as much of the excess wire from each joint as possible and then use hot glue to attach the divider into the lid of your Pi Zero case (use the lid with the long hole along the side).

We are going to stack the Scroll pHAT HD on top of the Enviro pHAT and solder them directly to the Pi Zero using only the connectors for the I2C bus. Below is a wiring diagram for how everything fits together.

Our complete wiring diagram

The next three pictures show you how it is all joined together. I used a little hot glue between the two boards to ensure that they did not touch each other. In the second picture you can see the voltage divider inside the lid of the Pi Zero case. Ensure that your cables are short enough that everything can neatly fit inside the case.

The final part of the hardware construction is to affix your sticky backed badge pin to the back of your Pi Zero case so you can clip it to your clothes.

Testing the hardware

Now we have connected all our hardware we need to test it… First, ensure that you have enabled I2C in the raspi-config tool. run the following command to check that our Pi Zero can see all of the devices on the I2C bus:

i2cdetect -y 1

You should see an output that matches this:

Output of the i2cdetect command

This is telling us that we can see the following five devices on the I2C bus:

  • 0x49: Enviro pHAT – ADS1015
  • 0x29: Enviro pHAT – TCS3472
  • 0x1d: Enviro pHAT – LSM303D
  • 0x77: Enviro pHAT – BMP280
  • 0x74: Scroll pHAT HD

If you do not see these five devices on the I2C bus, go back and check your wiring!

Software Install

First we will update our Raspberry Pi OS and install some basic packages we will need. Type the following command into your Pi:

sudo apt-get dist-upgrade -y && sudo apt-get install python3 python3-gpiozero python3-pip wget -y

Now we will install the Enviro pHAT software. Run the following command and perform a full install when prompted.

curl -sS https://get.pimoroni.com/envirophat | bash

Finally we will install the Scroll pHAT HD software. Enter the following command, again performing a full installation:

curl https://get.pimoroni.com/scrollphathd | bash

Test Program

First let’s create a short Python program to test that we can read the heart rate data from the analogue sensor. Type the following command to create a new directory, move into that directory and create a python file with in and open it for editing with the Nano text editor:

mkdir ~/heart && cd ~/heart && nano pulseTest.py

Now enter the following short Python program into the Nano text editor:

#!/usr/bin/python3

from envirophat import analog
from time import sleep

while True:
   pulse = analog.read(0)
   print("Pulse Voltage = {0}".format(pulse))
   sleep(0.25)

Press “Ctrl” and “X” to exit Nano, pressing “Y” to confirm saving the file. Attach your Pulse Sensor to your finger and run the program we just wrote to check we can read the voltage from the sensor:

python3 ./pulseTest.py

All being well you should see your program output something similar to this picture below. Press “Ctrl” and “C” together to stop the “pulseTest.py” program from running.

pulseTest.py example output

Final Program

Now we know that we can read data from our Pulse Sensor, let’s write our main program that will be used to read the heart rate from the sensor and display it on the Scroll pHAT HD. The following file is available on GitHub and is well commented throughout to explain what is happening. Have a read of the file over on GitHub to see what it is going to do and then download it into your “heart” directory by typing the following command:

cd ~/heart && wget https://raw.githubusercontent.com/jonwitts/Wearable-Tech-Projects-with-the-Raspberry-Pi-Zero/master/Chapter08/pulseRead.py

Once you have downloaded the file we need to make it executable by typing the following command:

sudo chmod +x ./pulseRead.py

Test the file by typing:

./pulseRead.py

The sensor should pick up your heart rate after a 30 seconds or so and display it on the Scroll pHAT HD. Once you are happy it is working, press “Ctrl” and “C” to stop the program from running.

Making our program run automatically

All that is left now is to make our program start automatically as soon as you switch on your Pi Zero. First, we will create our service definition file, so type this:

sudo nano /lib/systemd/system/pulseRead.service

Now, type the definition into Nano:

[Unit]
Description=Heart Rate Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/home/pi/heart/pulseRead.py
[Install]
WantedBy=multi-user.target

Save and exit Nano by typing “Ctrl” and “O“, followed by “Enter“, and then “Ctrl” and “X“. Now, change the file permissions, reload the systemd daemon and activate our service by typing this:

sudo chmod 644 /lib/systemd/system/pulseRead.service
sudo systemctl daemon-reload
sudo systemctl enable pulseRead.service

The last thing to do is to reboot your Pi Zero and check that the program starts automatically. Type the following command to reboot your Pi Zero:

sudo reboot now

When your Pi Zero restarts, place your finger upon your heart rate monitor, and you should
see your Scroll pHAT HD start to animate your heart beats and display your heart rate. You are now ready to power your Pi Zero from your portable power pack, and monitor
your heart rate while you are out and about!

Why not add a button to your Pi Zero so that you can shut it down safely too?


3 thoughts on “Raspberry Pi Zero Heart Rate Monitor

  1. Hey Jon,
    thx for the detailed blog post.
    I’m just left with one question.
    The Enviro pHat ist discontinued and i can get it any more.
    Does the project work as well with the newer Version of the Enviro Board?
    Regards
    Jonathan

Leave a Reply

Your email address will not be published. Required fields are marked *