Wearables are absolutely blowing up right now. From smart watches and jewelry to devices implanted in your clothes to armbands that allow you to navigate your computer, everything will soon be loaded with sensors. A big application of these wearables that has become very popular is fitness tracking, so I figured I’d talk about putting together and customizing your own heart rate monitor using a Raspberry Pi!
I invested in Adafruit’s Heart Rate Starter Pack which comes with a wireless sensor from Polar. It took me mere minutes to set up a system that blinked a large red LED in time with my heartbeat while streaming that beat for visualization of beats over time.
This tutorial is going to show you how to…
I used Adafruit’s Heart Rate Starter Pack so a lot of this was included:
- Raspberry Pi 2 w/ Raspbian loaded SD card
- 1 Polar T34 Heart Rate Transmitter + Polar Heart Rate Receiver
- HDMI cable + HDMI Monitor + Bluetooth Keyboard/Mouse (unless you already know your Pi’s IP address and can SSH in)
- WiFi Adapter/Dongle
- 1 Breadboard
- 40-pin GPIO Ribbon Cable (can use 5 individual male-to-female jumper cables instead)
- 40-pin GPIO to Breadboard Interface Board (not necessary if using jumper cables)
- 4 Breadboard Tie Lines
- 1 10mm Red LED
- 1 1k Ohm Resistor
- 1 Breakout Pin Header with at least 3 pins (mine came with the receiver)
You need to:
- Attach the Polar receiver to the breakout pins
- Connect GND to ground on the Pi (pin 39)
- Connect VIN to the voltage source (3.3V, pin 1)
- Connect OUT to GPIO 23 (Pin 16)
- Connect the short leg of the red LED to GPIO 24 (pin 18)
- Connect the long leg of the red LED to a 1k resistor; the other end of the resistor should be connected to the voltage source
Either hook up the Pi to your monitor and keyboard or SSH in from your computer. The GPIO library should be pre-installed if you’re running Raspbian. If you just opened up your Raspberry Pi you need to make sure that the software is up-to-date or you might get a GPIO error:
sudo apt-get update
We need to install one more thing before we get started. The Initial State streamer will let us stream a message every time your hear beats. You can use this to get your actual heart rate and have a record of how it changed over time! It’s super easy and takes less than 2 minutes.
Make sure you have an internet connection.
Type this command into the command line:
\curl -sSL https://get.initialstate.com/python -o - | sudo bash
Follow the prompts – if you say “Y” to the “Create an example script?” prompt, then you can designate where you’d like the script and what you’d like to name it. Your Initial State username and password will also be requested so that it can autofill your Access Key. If you say “n” then a script won’t be created, but the streamer will be ready for use.
Either use your example script to get your Access Key or create one from scratch and start streaming!
With everything installed, we can create our script! I called mine heartrate.py.
First, we need to import and initialize everything.
import RPi.GPIO as io # import the GPIO library we just installed but call it "io" from ISStreamer.Streamer import Streamer # import the IS Streamer we just installed but call it "Streamer" ## name the bucket and individual access_key ## the bucket_key will send all of our messages to the same place ## the access_key tells Initial State to send the messages to you streamer=Streamer(bucket_name="Heart Rate Monitor",bucket_key="heartrate",access_key="Your_Access_Key_Here") ## set GPIO mode to BCM ## this takes GPIO number instead of pin number io.setmode(io.BCM) io.setwarnings(False) receiver_in = 23 # this is the GPIO number our receiver is connected to LED_in = 24 # GPIO number the LED is connected to io.setup(receiver_in, io.IN) # initialize receiver GPIO to take input io.setup(LED_in, io.OUT) # initialized LED GPIO to give output io.output(LED_in, io.HIGH) # start with LED off
Now an important thing to know about the Polar transmitter is that it only outputs High or Low (which can also be 0 or 1 in Python), meaning that every time it detects a beat it outputs a High value (1). We take advantage of this in our next block of code by setting sample equal to whichever value the transmitter sends and lighting up the LED if a 1 is sent.
## this try block looks for 1 values (indicate a beat) from the transmitter try: firstBeatTime = time.time() sampleCounter = 0 while True: if sampleCounter == 0: firstBeatTime = time.time() ## sample will either be 1 or 0 sample = io.input(receiver_in) if sample == 1: io.output(LED_in, io.LOW) # turn LED on sampleCounter = sampleCounter + 1 if sampleCounter == SAMPLE_COUNT: sampleCounter = 0 # reset the sample counter # calculate beats per minute given the SAMPLE_COUNT sampleMinutes = (time.time() - firstBeatTime)/60 bpm = sampleMinutes * SAMPLE_COUNT # stream streamer.log("bpm", bpm) else: io.output(LED_in, io.HIGH) # turn LED off # Set the previous sample to the current sample so that it can be used to # evaluate if at the front edge of the heartbeat and not count it more than # once if already in the high position previousSample = sample ## this allows you to end the script with ctrl+c except KeyboardInterrupt: streamer.log("msg", "Received Interrupt") streamer.close() # safely close the streamer
In order to know that the script is running, we stream “Waiting for heartbeat”.
Now try it out! The LED should flash in time with your heartbeat. It’s neat to see how it changes when you hold your breath or breathe very quickly. The only problem with the LED is that you can’t see your heartbeats over time. That’s why we streamed to Initial State!
You can see that my resting heart rate was 72 at that point in time. Over the span of 10 minutes I averaged right around 70 which falls within the average resting heart rate range of 60-100 beats per minute. The little spike in the middle was when I did a quick minute of exercise and my heart rate increased to 120 beats per minute
One of the great things about this project is that the Polar sensor works at a range of up to 4 feet, so you can have your Raspberry Pi set up somewhere near your exercise equipment and gauge your heart rate just by looking at the LED. Or you could put the Pi into your backpack along with a wifi dongle and a portable battery to stream your heart rate to Initial State on the move!
Another cool project would be to hook up an LCD screen like the 5″ one I reviewed earlier that could flash up your current heart rate.