#include <Wire.h>
#include <MAX30105.h>
// Create an instance of the MAX30105 object
MAX30105 particleSensor;
// Define I2C pins (SDA and SCL)
#define SDA_PIN 21
#define SCL_PIN 22
void setup() {
// Start serial communication
Serial.begin(115200);
delay(1000);
// Initialize I2C with the correct pins for ESP32
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the sensor
if (!particleSensor.begin()) {
Serial.println("Could not find a valid MAX30102 sensor!");
while (1);
}
// Configure the sensor (default settings for heart rate and SpO2)
particleSensor.setup(); // Default setup for heart rate and SpO2
particleSensor.setPulseAmplitudeRed(0x0A); // Set red LED brightness
particleSensor.setPulseAmplitudeGreen(0x0A); // Set green LED brightness
}
void loop() {
long irValue = particleSensor.getIR(); // Read IR value (used for SpO2 and heart rate)
// If the sensor detects a finger (IR value > 5000), try to calculate the heart rate and SpO2
if (irValue > 5000) {
// Read the heart rate and SpO2
long heartRate = particleSensor.getHeartRate();
long spo2 = particleSensor.getSpO2();
// Print the readings to the serial monitor
Serial.print("Heart Rate: ");
Serial.print(heartRate);
Serial.print(" bpm\t");
Serial.print("SpO2: ");
Serial.print(spo2);
Serial.println(" %");
} else {
Serial.println("No finger detected.");
}
delay(1000); // Wait for a second before reading again
}