DIY Oximeter with MAX30102 and ESP32

Protonest IoT
4 min readSep 5, 2024

--

If you’ve ever wanted to monitor your oxygen levels (SpO2) and heart rate from home, you’re in the right place!

In this article, we’re going to build a simple pulse oximeter using the MAX30102 sensor and ESP32.

Don’t worry, we will walk you through the steps, explain how SpO2 is calculated, and even provide code to get you started.

What is SpO2?

SpO2 stands for peripheral oxygen saturation and gives us an estimate of the oxygen level in your blood. Healthy people usually have a SpO2 between 95% and 100%.

Anything below that might require medical attention. You’ve probably seen pulse oximeters at hospitals. These devices measure SpO2 and heart rate by shining red and infrared light through your finger and measuring how much light is absorbed.

How SpO2 is Calculated

The MAX30102 sensor uses light to calculate the oxygen saturation of your blood. It shines both red and infrared light through your finger and measures how much light is absorbed by your blood.

Here’s a simplified formula for calculating SpO2,

R = (Red AC / Red DC) / (IR AC / IR DC)
SpO2 = 104.0–17.0 * R

AC is the fluctuating signal due to your pulse.

DC is the constant baseline signal.

Components Needed

Setting Up MAX30102 with ESP32

  • First, you need to solder the header pins to Max30102.

Note: After that solder jumper on the 3V3 side. If you forget this, I2C does not work and can not find MAX30102, saying “MAX30102 was not found.”

Image for soldering jumper on the 3V3 side
  • Now, you can wire the circuit as below,
Connecting Max30102 with esp32. Vcc, GND, SCL and SDA. SDA to pin 21, SCL to pin 22

Install the Library

  • Download the library from the Google Drive link, unzip it, and add it to the Documents/Arduino/Libraries folder.

Code for Measuring SpO2 with MAX30102

Here’s the code you can use to calculate SpO2 and get readings for Red, Infrared, and Greenlight intensities,

#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h" // Includes algorithm for calculating heart rate

MAX30105 particleSensor;

#define debug Serial // Use Serial for debugging with ESP32 or Arduino Uno

void setup()
{
debug.begin(9600); // Initialize serial communication at 9600 baud
debug.println("Initializing MAX30102...");

// Initialize sensor
if (particleSensor.begin() == false)
{
debug.println("MAX30102 not found. Check wiring!");
while (1); // Halt the program if sensor isn't detected
}

particleSensor.setup(); // Default settings for MAX30102 sensor
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low power
particleSensor.setPulseAmplitudeGreen(0); // Turn off Green LED, we don't need it for SpO2
}

void loop()
{
long red = particleSensor.getRed(); // Get Red light reading
long ir = particleSensor.getIR(); // Get Infrared light reading

if (ir < 50000) // If IR is too low, there's no finger on the sensor
{
debug.println("No finger detected");
return;
}

// Calculate SpO2 based on red and infrared values
float R = (float)red / (float)ir;
float spo2 = 104.0 - 17.0 * R; // Updated SpO2 formula for more realistic values

// Display the results
debug.print("Red: "); debug.print(red);
debug.print("\tIR: "); debug.print(ir);
debug.print("\tSpO2: "); debug.println(spo2);

delay(1000); // Wait for 1 second before next reading
}

Explanation of the Code

  • ‘particleSensor.getRed()’ and ‘particleSensor.getIR()’: These functions retrieve the Red and Infrared light readings from the sensor.
  • SpO2 Calculation: Using the ratio of Red to Infrared light (R value), the SpO2 value is calculated with the equation ‘SpO2 = 104.0–17.0 * R’.
  • Finger Detection: If the IR reading is less than 50,000, it’s a good indicator that your finger isn’t properly on the sensor, so we skip the calculation.

Viewing the Data

To see your SpO2 and heart rate readings:

  • Upload the code: Connect your ESP32 to your computer and upload the code using the Arduino IDE.
  • Open Serial Monitor: In the Arduino IDE, go to ‘Tools > Serial Monitor’, and make sure the baud rate is set to 9600. Once your sensor is running, you’ll see readings for Red, Infrared, and SpO2.
  • Place Your Finger on the Sensor: Hold the MAX30102 sensor against your fingertip and watch the values appear in real time
  • When you keep the finger, you will see the output as below.

Final Thoughts from Protonest

And that’s it! You now have a fully functional DIY pulse oximeter using MAX30102 and ESP32. This setup is a great way to understand how SpO2 is calculated and how sensors like these work in medical devices.

You can expand this project by adding a display, sending data to the cloud, or creating a health monitoring app.

Hope you enjoyed the article. Please comment below or send us an email to info@protonest.co, if you face any issues when implementing.

Contact us for any consultations or projects related to IoT.

Email: info@protonest.co

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

Cheers!

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet