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 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

Pulse oximeter · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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("--");
  }
}

Parts list

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

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; MAX30102 pulse oximeter pin VIN
  • Ground: Arduino Uno R3 pin GND; MAX30102 pulse oximeter pin GND
  • Arduino Uno R3 pin A4; MAX30102 pulse oximeter pin SDA
  • Arduino Uno R3 pin A5; MAX30102 pulse oximeter pin SCL

Change it and keep it

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