Make Every Week: Heartbeat LEDs

Visualizing one’s heartbeat is just cool. I’ve been into the idea since I learned that signals sent by the Polar heart monitor straps joggers use can be detected with a cheap device.

I’ve made a heartbeat hoodie, which was a lot of fun. But in the end, a bright, flashing sweatshirt starts to annoy the people around you. Now I’m working toward a more wearable wearable, one that changes subtly as my heart beats faster or slower.

This week I took a step on the way to that wearable by getting three LEDs — blue, green and yellow — to light up according to my heart rate. A calm heartbeat and blue glows, a little faster and you get green and really fast lights the yellow one.

We Got the Beat

For the hoodie, I used this Adafruit kit, which comes with one of the Polar strap transmitters. Every beat sent a pulse to the Arduino. But this board from Sparkfun is a little more sophisticated and provides heart rate readings in beats per minute. I decided to use that board.

It turns out that before I could use this board with an Arduino, I had to remove the connection between the two pads marked “SJ1” using a soldering iron and also add some solder to connect the two pads marked “OP0.” I also soldered four header pins to the four holes used for power and communication, and then wired it up like this:

(The instructions suggest using pull-up resistors, on the RX and TX pins, but I didn't and it worked fine.)

After a lot of trying, I couldn’t get the example program to work without crashing. Fortunately someone else had the same problem and rewrote the code.

That did the trick. With Arduino’s serial monitor, I saw a row of numbers come back. The first number was a status number, usually 1. The second was a counter that goes from 0 to 255 repeatedly. The remaining values are heart rate readings, with the most recent one first.

For my project, I’m using the three most recent heartrate readings and averaging them so the changes are slower. Remember: subtle.

Beats to Brightness

As my heart rate changed, I wanted to fade gracefully between the blue, green and yellow LEDs.

The first hurdle was to figure out how to control the brightness of an LED on. This is pretty easy with an Arduino, though I had never used this feature before.

The connectors on the Arduino marked with squiggle sign ( ~ ) are capaable of “pulse wave modulation,” or PWM. That means you can set them to blink an LED so fast they look dimmer, in a range from 0 (off) to 255 (full strength).

The Arduino command is pretty easy: analogWrite(9, 125); sets the LED on Pin 9 to 125, or about half-strength.

Here’s some example code to pulse an LED gracefully, which is also available as an the Arduino software under File > Examples > Basics > Fade.

To figure out how bright to make each LED, I wanted to do math on the heart rate. I imagined it working like this:

After some tinkering around with the equations in this graphing calculator, I came up with this:

The colors are off, but you get the idea: As my heart rate goes from 54 to 108 (the horizontal axis), each of the three LEDs change in brightness, peaking at heart rates of 60, 70 and 80, respectively.

I added a bit of code that deals with the negative numbers (makes them 0) and heart rates below 60 or above 80 (the blue or yellow LEDs just stay on full). My Arduino code is here. The wiring up to this point looked like this:

The Beat Blink

In the end, I also wanted one LED to flash at the rate of my heartbeat. A red one.

This presented a puzzle. The Arduino runs one loop of code over and over. In each loop, I had it checking the three latest heart rates, averaging them out, adjusting the LED’s brightness accordingly and pausing for a second before looping again. How could also flash a red LED at my heart rate?

My solution was add a quick red flash to each loop and then pause the entire loop at the rate of my heartbeat. If my heart rate is 75 beats per minute, that’s 0.8 seconds (or 800 milliseconds) per beat. So wait that long!

You can see that bit at the end of my code. My wiring, with the LEDs, looks like this:

Next steps

Soon I plan to sew a bunch of LEDs onto the front of a shirt, and then figure out how to make all of my electronics smaller and wearable, very likely using a littler version of the Arduino board.

#MakeEveryWeek is a challenge to myself to do just that for all of 2015. The original post on the idea is here, and the running list of projects is here.

Image of the heart rate monitor board in the Fritzing diagram is CC BY-NC-SA 3.0 by Sparkfun.