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
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/
One way to measure a heartbeat: Photoplethysmography
CPX PPG Waveform
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.
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.
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.
A peek under the hood
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.
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.
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.
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
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!
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?
CPX and Micropython with Mu Editor
Check out the code.
Lagniappe: micro:bit with Pulse Sensor