Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 14: Pulse Oximeter and Heart Rate Sensor Module (MAX30102)
In this lesson, you will learn how to measure heart rate using an ESP32 Development Board and MAX30102 Pulse Oximeter and Heart Rate Sensor. We’ll cover setting up the sensor, reading infrared values, and accurately calculating beats per minute (BPM). This project is ideal for those interested in health monitoring systems and provides a valuable introduction to working with biomedical sensors using the ESP32.
Warning
This project detects heart-rate optically. This method is tricky and prone to give false readings. So please DO NOT use it for actual medical diagnosis.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Code
Note
To install the library, use the Arduino Library Manager and search for “SparkFun MAX3010x” and install it.
Code Analysis
Including Libraries & Initializing Global Variables:
The essential libraries are imported, the sensor object is instantiated, and global variables for data management are set.
Note
To install the library, use the Arduino Library Manager and search for “SparkFun MAX3010x” and install it.
#include <Wire.h> #include "MAX30105.h" #include "heartRate.h" MAX30105 particleSensor; // ... (other global variables)
Setup Function & Sensor Initialization:
The Serial communication is initialized at a baud rate of 9600. The sensor’s connection is checked, and if successful, an initialization sequence is run. An error message is displayed if the sensor isn’t detected.
void setup() { Serial.begin(9600); if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) { Serial.println("MAX30102 not found."); while (1) ; // Infinite loop if sensor not detected. } // ... (further setup)
Reading IR Value & Checking for Heartbeat:
The IR value, which is indicative of the blood flow, is fetched from the sensor. The
checkForBeat()function assesses if a heartbeat is detected based on this value.long irValue = particleSensor.getIR(); if (checkForBeat(irValue) == true) { // ... (heartbeat detected actions) }
Calculating Beats Per Minute (BPM):
Upon detecting a heartbeat, the BPM is calculated based on the time difference since the last detected heartbeat. The code also ensures the BPM falls within a realistic range before updating the average.
long delta = millis() - lastBeat; beatsPerMinute = 60 / (delta / 1000.0); if (beatsPerMinute < 255 && beatsPerMinute > 20) { // ... (store and average BPM) }
Printing Values to the Serial Monitor:
The IR value, current BPM, and average BPM are printed to the Serial Monitor. Additionally, the code checks if the IR value is too low, suggesting the absence of a finger.
//Print the IR value, current BPM value, and average BPM value to the serial monitor Serial.print("IR="); Serial.print(irValue); Serial.print(", BPM="); Serial.print(beatsPerMinute); Serial.print(", Avg BPM="); Serial.print(beatAvg); if (irValue < 50000) Serial.print(" No finger?");