I managed to set up the I2C connection, but for some reason, the sensor won't pick up the heart pulse signal. Can anybody help?
my code
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
#define debug Serial
void setup() {
debug.begin(115200);
debug.println("=== MAX30105: Heart Rate & SpO2 ===");
if (!particleSensor.begin()) {
debug.println("❌ Error: MAX30105 not found. Check connections!");
while (1);
}
// Configure sensor
particleSensor.setup();
// You can experiment with changing the LED values if needed (e.g., 0x7F)
particleSensor.setPulseAmplitudeRed(0x3F); // Red LED brightness
particleSensor.setPulseAmplitudeIR(0x3F); // Infrared LED brightness
debug.println("\nTimestamp (ms) | IR Value | Heart Rate (BPM) | SpO2 (%)");
debug.println("-----------------------------------------------------------");
}
void loop() {
long irValue = particleSensor.getIR(); // Read IR value
// Lower the threshold if the signal is weak
if (irValue < 30000) {
debug.print("⚠️ Sensor not in contact or weak signal. IR = ");
debug.println(irValue);
} else {
float bpm = checkBPM(); // Read heart rate
float spo2 = estimateSpO2(); // Estimate SpO2
debug.printf("%10lu ms | %10ld | %16.2f | %8.2f\n", millis(), irValue, bpm, spo2);
}
delay(1000);
}
// Function to calculate heart rate BPM
float checkBPM() {
static float heartRate = 0;
// If checkForBeat function detects a beat, calculate the time interval between beats
if (checkForBeat(particleSensor.getIR())) {
static long lastBeat = 0;
long delta = millis() - lastBeat;
lastBeat = millis();
// Avoid division by 0 or unrealistic values
if (delta > 300 && delta < 2000)
heartRate = 60.0 / (delta / 1000.0);
}
return heartRate;
}
// Function to estimate SpO2 (simple estimation)
float estimateSpO2() {
static float spo2 = 98.0; // Default value
return spo2 - (random(0, 5) / 10.0);
}