1 of 16

Visualize your Heartbeat

Measuring heart rate with an microcontroller and pulse sensor

Jimmy Newland 2018 Expeditions in Computing RET intern https://wp.me/P3rYuP-6SAshp

2 of 16

Science Through Coding: Computational Science

“Computational Science. Here numerical models and computer simulations are created. These can be used to compare to experimental data and the theoretical models.” -Rhett Allain, WIRED Magazine Writer and Physics Professor https://www.wired.com/2014/01/what-kind-of-science-is-computational-science/

3 of 16

One way to measure a heartbeat: Photoplethysmography

  • Photoplethysmography: using light to measure the blood volume.
  • Transmissive PPG: light source & detector are on opposite sides of appendage; often uses red & infrared LED sources
  • Reflective PPG: source & detector are at same skin location; often uses green LED source
  • Both PPG types give pulmonary information but are processed in different ways.
  • Systole: highest arterial pressure of a heartbeat (tallest peak in PPG)
  • Diastole: lowest arterial pressure of heartbeat (lower peak in PPG)
  • Dicrotic notch: point where heartbeat transitions from lub (heart doing work) to dub (heart relaxing temporarily)

CPX PPG Waveform

4 of 16

Choose your own adventure: 3 different PPG with Microcontroller Labs

Short version using Arduino UNO and Pulse Sensor can be done in a single 50 minute class period. Works with LMS like ItsLearning and Google Classroom. Used in AP Physics C class.

Longer version using CircuitPlayground Express and Pulse Sensor takes probably two 50 minute class periods. More like an engineering lesson. Requires more software and hardware setup. Could work in physics, comp sci, or engineering class.

Coding version uses CircuitPlayground w/ Micropython with more coding requirements and uses Python-based visualization and Mu editor.

5 of 16

Short Version: Gather data and analyze

Student Instructions: Build this circuit. Make sure the LED is at least 220 Ω. Use any color LED you’d like. Next, open the Arduino IDE. Make sure you have installed the Pulse Sensor Amped library. You will also get some starter code by installing the library. Once the library is done installing, open up the “GettingStartedProject” from the PulseSensorAmped examples.

Simple circuit with single LED, breadboard, sensor, and Arduino UNO board

The short version is very light on coding, although there is a lot of computational thinking.

6 of 16

Modifying the code

Student Instructions: In order to get data for your heartbeat, we need to modify the GettingStartedProject code a bit. Find the line in the code where the Signal is gathered from the sensor and printed. Add the current time in milliseconds to print statement and make sure to separate the 2 variables by a tab character so it’s easier to copy and paste into a spreadsheet.

7 of 16

A peek under the hood

8 of 16

Gathering the data

Student instructions: Make sure to gather about a minute of data for each person. Then copy and paste the data into a spreadsheet and generate a plot of the signal vs time. Be sure to have at least 10 seconds of data for each person at your table. Next, plot milliseconds on the x-axis and signal on the y-axis for 10 seconds of clean data.

9 of 16

Plotting your pulse

Student instructions: Then make a column called “peak times” and manually inspect the plot for the time of each diastolic peak. Next create a column called IBI for interbeat interval and subtract the current row’s peak time from the previous one. This is your heart rate in real time. Plot the IBI vs milliseconds as well. Everyone at your table should gather their pulmonary data and generate a plot of signal versus time. That spreadsheet will be your end-product.

10 of 16

Long version: engineer a PPG system

The basic challenge here is to turn on an LED and then read the light reflected off your finger into the light sensor. Then display the data in the plotter tool and see your pulse in real-time!

Step 1: experiment with coding the Circuit Playground Express (CPX) to make LEDs turn certain colors.

11 of 16

Using the CPX Light Sensor

Get the light level reading from the CPX and print it to the serial console. Next, we add a 20-millisecond delay to allow the device to reset.

Access the serial console by selecting the Tools menu and then selecting Serial Monitor. Or plot the values using Serial Plotter.

Step 2: Familiarize yourself with the light sensor

12 of 16

Putting it all together

Step 3: Put together the various pieces: turn on the LED closest to the light sensor, read the light reflected back into the sensor, and print the values to the serial plotter.

Students should start by setting up, just as we did before. The comments in Figure 1 are meant to be incomplete, so the students can complete them!

  • Line 13 should have a line that turns on the LED at position 1 (that is the second LED since they start counting at 0). Starting counting at 0 is very common with computing.
  • Line 18 needs to be finished so that the light sensor can be read directly.
  • Line 22 is important! The light we get back from the sensor is the light that WAS NOT absorbed by the person’s tissue. We need to know how much light WAS absorbed. The maximum light value is 1024 (a power of 2 since this is a computer using binary). How can you change the number to represent how much light was absorbed rather than reflected back? Use the provided variable called PPG that was declared on line 4 to store your answer.
  • Line 25 is where you print the result from line 22. This allows you to see the data in a raw format or to see the data plotted over time.

13 of 16

Scrolling heartbeat

Once students load the program and it is running and plugged into their computer, go to Tools -> Serial Plotter and see the pulse in real time!

While testing the device, have students find a comfortable position and hold still for a full minute. Notice that signal calms down and changes. Suggest that the students try it out on different fingers and with a neighbor. Note that how hard a student presses does matter to the output. Have the students experiment with different positions and pressure levels. Ask the students: Which finger works best?

  • Wrap Up:
    • Students should share their code and waveforms with the one another.
  • As a class:
    • Discuss where difficulties arose and tackle questions about how students’ algorithms compared with one another.
  • Ask the students:
    • Now that you have used a microcontroller as a health device, how could this system be made more portable? What other sorts of data could sensors access that might tell us something about a person’s health?

14 of 16

CPX and Micropython with Mu Editor

15 of 16

Check out the code.

16 of 16

Lagniappe: micro:bit with Pulse Sensor