Built-in project · Arduino Uno R3

Pulse oximeter on an Arduino Uno

A MAX30102 over I2C with the SparkFun library: the red and infrared ripple of a pulse through a fingertip, turned into a heart rate and a blood oxygen estimate. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino MAX30102 Pulse Oximeter: Code and Wiring

The circuit runs here once the page has loaded.

What you'll learn

  • How light absorption through a fingertip shows a heartbeat
  • Why red and infrared light together can estimate oxygen level
  • Collecting samples into buffers and processing them in a batch
  • Reading a sensor over I2C with a library
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The MAX30102 shines red and infrared light into a fingertip and measures how much comes back. Each heartbeat pushes a little more blood into the finger, so both readings dip once per beat. The gap between dips gives the heart rate.

Oxygen-rich blood absorbs less red light than oxygen-poor blood. Comparing the red dip to the infrared dip gives the SpO2 estimate. The sensor connects to A4 and A5 over I2C.

The sketch collects 100 samples of each color, then runs the SparkFun library's algorithm on them. It prints the heart rate and SpO2, or Place your finger on the sensor if it cannot find a pulse. This is for learning, not a medical reading.

Parts list

3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
MAX30102 pulse oximeter pin VINArduino Uno R3 pin 5V
MAX30102 pulse oximeter pin GNDArduino Uno R3 pin GND
MAX30102 pulse oximeter pin SDAArduino Uno R3 pin A4
MAX30102 pulse oximeter pin SCLArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Pulse oximeter: a MAX30102 shines red and infrared light into a fingertip
// and measures what comes back, read with the SparkFun MAX3010x library.
//
//   MAX30102   VIN -> 5 V   GND -> GND   SDA -> A4   SCL -> A5
//
// Each heartbeat pushes a little more blood into the finger, which absorbs a
// little more light, so both readings dip once a beat: the spacing of the dips
// is the heart rate. Blood rich in oxygen absorbs less red than blood poor in
// it, so how deep the red dip is compared with the infrared one tells the
// oxygen level: R = (red swing / red level) / (IR swing / IR level), and
// SpO2 is about 110 - 25 R.
//
// The LEDs run at 4 mA so that a reading fits the 16 bits the Uno has room
// to store a hundred of. This is for learning how it works, not a medical
// measurement.
//
// Drag the sliders on the sensor, and press F to take the finger off.

#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"

MAX30105 sensor;

uint16_t irBuffer[100];
uint16_t redBuffer[100];

void setup() {
  Serial.begin(9600);
  if (!sensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println("MAX30105 was not found. Please check wiring/power.");
    while (1) {
    }
  }
  // 4 mA, 4 samples averaged, red and IR, 100 a second, 411 us, 4096 nA:
  // 25 samples a second reach the FIFO, the rate the algorithm expects.
  sensor.setup(20, 4, 2, 100, 411, 4096);
  Serial.println("Pulse oximeter ready");
}

void loop() {
  for (byte i = 0; i < 100; i++) {
    while (sensor.available() == 0) {
      sensor.check();
    }
    redBuffer[i] = sensor.getFIFORed();
    irBuffer[i] = sensor.getFIFOIR();
    sensor.nextSample();
  }

  int32_t spo2;
  int8_t spo2Valid;
  int32_t heartRate;
  int8_t heartRateValid;
  maxim_heart_rate_and_oxygen_saturation(irBuffer, 100, redBuffer, &spo2, &spo2Valid, &heartRate, &heartRateValid);

  if (!heartRateValid && !spo2Valid) {
    Serial.println("Place your finger on the sensor");
    return;
  }
  Serial.print("heart rate ");
  if (heartRateValid) {
    Serial.print(heartRate);
    Serial.print(" bpm");
  } else {
    Serial.print("--");
  }
  Serial.print(", SpO2 ");
  if (spo2Valid) {
    Serial.print(spo2);
    Serial.println(" %");
  } else {
    Serial.println("--");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the serial port, then calls sensor.begin. If the sensor does not answer, it prints a wiring message and stops. Otherwise sensor.setup sets the LED current, averaging, color mode and sample rate to give 25 samples a second.

void setup() {
  Serial.begin(9600);
  if (!sensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println("MAX30105 was not found. Please check wiring/power.");
    while (1) {
    }
  }
  // 4 mA, 4 samples averaged, red and IR, 100 a second, 411 us, 4096 nA:
  // 25 samples a second reach the FIFO, the rate the algorithm expects.
  sensor.setup(20, 4, 2, 100, 411, 4096);
  Serial.println("Pulse oximeter ready");
}

loop sketch.ino

Fills redBuffer and irBuffer with 100 samples each, waiting on sensor.available. Then it calls maxim_heart_rate_and_oxygen_saturation, which fills in the heart rate, SpO2 and two valid flags. It prints only what is valid.

void loop() {
  for (byte i = 0; i < 100; i++) {
    while (sensor.available() == 0) {
      sensor.check();
    }
    redBuffer[i] = sensor.getFIFORed();
    irBuffer[i] = sensor.getFIFOIR();
    sensor.nextSample();
  }

  int32_t spo2;
  int8_t spo2Valid;
  int32_t heartRate;
  int8_t heartRateValid;
  maxim_heart_rate_and_oxygen_saturation(irBuffer, 100, redBuffer, &spo2, &spo2Valid, &heartRate, &heartRateValid);

  if (!heartRateValid && !spo2Valid) {
    Serial.println("Place your finger on the sensor");
    return;
  }
  Serial.print("heart rate ");
  if (heartRateValid) {
    Serial.print(heartRate);
    Serial.print(" bpm");
  } else {
    Serial.print("--");
  }
  Serial.print(", SpO2 ");
  // ... 7 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It prints MAX30105 was not found. This project
    Check SDA on A4, SCL on A5, VIN on 5V and GND. The code names the chip MAX30105 because the library covers both.
  • It keeps asking you to place your finger. This project
    The sensor needs a finger on it, and a steady one. In the simulator, drag the sliders on the sensor and press F to take the finger off.
  • No readings or the sensor is not found. MAX30102 pulse oximeter
    Check SDA, SCL, power and ground. The I2C address is 0x57. Most breakouts need 3.3 V for the logic pull-ups; check your board before using 5 V.
  • Pulse and oxygen values are noisy. MAX30102 pulse oximeter
    Rest the fingertip on the window with light, steady pressure. Pressing hard cuts blood flow, and moving or bright ambient light add noise. Give it a few seconds to settle.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.