Operating MAX30102 sensor with Arduino

Hello, I have previously asked for help with my problem in another thread. I still have a few problems with the same project. Under normal conditions, I aim to get data by operating MAX30102, MPU6050 and GSR sensors simultaneously. But I can't do this right now. First of all, I aimed to see the BPM and SPO2 values ​​from the MAX30102 sensor at the same time. However, while I can measure the BPM value on its own, I cannot run the code I wrote to work with SPO2. I assume I have installed all the libraries, checked for errors multiple times. I leave the relevant codes and errors. Thank you if you can help.

#include <Wire.h>                 // I2C Library
#include "MAX30105.h"             // SparkFun MAX3010x library
#include "spo2_algorithm.h"       // SpO2 algorithm from SparkFun

MAX30105 particleSensor;

const byte RATE_SIZE = 4;         // Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE];            // Array of heart rates
byte rateSpot = 0;
long lastBeat = 0;                // Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;

int32_t spo2;                     // SpO2 value
int8_t validSPO2;                 // SpO2 valid flag
int32_t heartRate;                // Heart rate value
int8_t validHeartRate;            // Heart rate valid flag

void setup() {
  Wire.begin();                                       // Initialize I2C communication
  Wire.setClock(400000);                              // Set I2C speed to 400kHz

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {  // Initialize MAX30105
    Serial.println("MAX30105 was not found. Please check wiring/power.");
    while (1);
  }

  particleSensor.setup();                             // Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A);          // Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeIR(0x0A);           // Set IR LED to measure SpO2

  // Initialize serial communication
  Serial.begin(9600);                                 // Set the baud rate to your desired value
}

void loop() {
  long irValue = particleSensor.getIR();              // Reading the IR value
  long redValue = particleSensor.getRed();            // Reading the RED value for SpO2

  if (irValue > 50000)  // Finger is detected
  {
    if (checkForBeat(irValue) == true)                // If a heart beat is detected
    {
      long delta = millis() - lastBeat;               // Measure duration between two beats
      lastBeat = millis();
      beatsPerMinute = 60 / (delta / 1000.0);         // Calculating the BPM

      if (beatsPerMinute < 255 && beatsPerMinute > 20) // To calculate the average, store some values then average them
      {
        rates[rateSpot++] = (byte)beatsPerMinute;     // Store this reading in the array
        rateSpot %= RATE_SIZE;                        // Wrap variable

        beatAvg = 0;
        for (byte x = 0; x < RATE_SIZE; x++)
          beatAvg += rates[x];
        beatAvg /= RATE_SIZE;

        Serial.print("BPM: ");
        Serial.println(beatAvg);
      }

      // Calculate SpO2
      maxim_heart_rate_and_oxygen_saturation(particleSensor.getIR(), particleSensor.getRed(), &spo2, &validSPO2, &heartRate, &validHeartRate);
      if (validSPO2 && validHeartRate)                // Check if valid readings obtained
      {
        Serial.print("SpO2: ");
        Serial.println(spo2);
      }
    }
  }
  else
  {
    Serial.println("No finger detected");
  }
}

Error text:C:\Users\Mahmut\AppData\Local\Temp.arduinoIDE-unsaved202456-19040-14jrhun.swpy\sketch_jun6a\sketch_jun6a.ino: In function 'void loop()':
C:\Users\Mahmut\AppData\Local\Temp.arduinoIDE-unsaved202456-19040-14jrhun.swpy\sketch_jun6a\sketch_jun6a.ino:43:9: error: 'checkForBeat' was not declared in this scope
if (checkForBeat(irValue) == true) // If a heart beat is detected
^~~~~~~~~~~~
C:\Users\Mahmut\AppData\Local\Temp.arduinoIDE-unsaved202456-19040-14jrhun.swpy\sketch_jun6a\sketch_jun6a.ino:64:141: error: cannot convert 'int8_t* {aka signed char*}' to 'int32_t* {aka long int*}' for argument '4' to 'void maxim_heart_rate_and_oxygen_saturation(uint32_t*, int32_t, uint32_t*, int32_t*, int8_t*, int32_t*, int8_t*)'
maxim_heart_rate_and_oxygen_saturation(particleSensor.getIR(), particleSensor.getRed(), &spo2, &validSPO2, &heartRate, &validHeartRate);
^
Multiple libraries were found for "MAX30105.h"
Used: C:\Users\Mahmut\Documents\Arduino\libraries\SparkFun_MAX3010x_Pulse_and_Proximity_Sensor_Library
Not used: C:\Users\Mahmut\Documents\Arduino\libraries\MAX3010x_Sensor_Library
exit status 1

Compilation error: 'checkForBeat' was not declared in this scope

Hi @mahmut2021.

The checkForBeat function is declared by the "SparkFun MAX3010x Pulse and Proximity Sensor Library" library's heartRate.h header file. So you can fix this error by adding the following line at the top of your sketch:

Select File > Examples > SparkFun MAX3010x Pulse and Proximity Sensor Library > Example8_SPO2 from the Arduino IDE menus. The "Example8_SPO2" example sketch will open. You can study the code of that sketch to learn how to use the maxim_heart_rate_and_oxygen_saturation function.

Hi,
I compiled a code like this. At first it only measured BPM as 59. But with the corrections I made later, I cannot measure the BPM value at all. The MAX30102 sensor light is on. I can also take measurements when I operate the sensor alone. But when combining 3 sensors, MAX30102 does not give any value.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include "MAX30105.h"
#include "heartRate.h" // Nabız hesaplama algoritması

Adafruit_MPU6050 mpu;
MAX30105 particleSensor;

const int GSR_PIN = A0;

const byte RATE_SIZE = 4; // Ortalama almak için boyutu artırın. 4 iyi bir değerdir.
byte rates[RATE_SIZE];    // Nabız değerlerinin dizisi
byte rateSpot = 0;
long lastBeat = 0;         // Son atışın gerçekleştiği zaman
float beatsPerMinute;
int beatAvg;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  if (!mpu.begin()) {
    Serial.println("MPU6050 sensorü başlatılamadı!");
    while (1);
  }

  Wire.setClock(400000); // I2C hızını 400kHz olarak ayarlayın

  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println("MAX30102 sensörü başlatılamadı!");
    while (1);
  }

  particleSensor.setup(); // Sensörü varsayılan ayarlarla yapılandır
  particleSensor.setPulseAmplitudeRed(0x0A); // Kırmızı LED'i düşük seviyeye ayarla
  particleSensor.setPulseAmplitudeGreen(0);  // Yeşil LED'i kapat
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.print("MPU6050 Hareket: ");
  Serial.print(a.acceleration.x);
  Serial.print(", ");
  Serial.print(a.acceleration.y);
  Serial.print(", ");
  Serial.println(a.acceleration.z);

  int gsrValue = analogRead(GSR_PIN);

  Serial.print("GSR Değeri: ");
  Serial.println(gsrValue);

  long irValue = particleSensor.getIR(); // IR değerini al

  if (irValue > 50000) {
    if (checkForBeat(irValue) == true) { // Bir kalp atışı algılandıysa
      long delta = millis() - lastBeat; // İki atış arasındaki süreyi ölç
      lastBeat = millis();
      beatsPerMinute = 60 / (delta / 1000.0); // BPM'yi hesapla
      if (beatsPerMinute < 255 && beatsPerMinute > 20) { // Ortalama hesaplamak için bazı değerler saklayalım ve ortalamayı hesaplayalım
        rates[rateSpot++] = (byte)beatsPerMinute; // Bu okumayı dizide sakla
        rateSpot %= RATE_SIZE; // Değişkeni sarmala

        // Okumaların ortalamasını al
        beatAvg = 0;
        for (byte x = 0 ; x < RATE_SIZE ; x++)
          beatAvg += rates[x];
        beatAvg /= RATE_SIZE;

        // BPM'yi seri monitöre yazdır
        Serial.print("BPM: ");
        Serial.println(beatsPerMinute);
      }
    }
  } else {
    // Parmak algılanmadı, bu durumu farklı şekilde ele alabilirsiniz.
  }

  delay(1000);
}

image
WhatsApp Görsel 2024-06-08 saat 22.58.56_bfcd8bf0

You have configured your sketch to only use the sensor when irValue > 50000:

So if particleSensor.getIR() returns a value of less than or equal to 50000 then that is exactly the behavior we would expect from your sketch program.

What should I do?

Is a value of <= 50000 from particleSensor.getIR() expected?

Is there a reason you configured your sketch code so that the MAX30102 will only be read if irValue > 50000?

Whether it is smaller or larger than 50000 does not change anything. But the code does not work without this condition. I don't have much idea about what happens next.

Please provide a detailed description of what you mean by "does not work" in a reply on this forum thread, including:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?

Make sure to include the full and exact text of any error or warning messages you might have encountered.

Actually, I mentioned the general purpose of my project in a different title. Let me talk about it here for understanding. Our project is a smart watch project based on receiving data from 3 different sensors simultaneously. Here we aim to measure BPM and SPO2 with MAX30102. We aim to see human movements with MPU6050 and emotional changes with GSR.
I activated the project's sensors one by one and took measurements. So I measured BPM and SPO2 separately from the MAX30102 sensor. I ran GSR separately. MPU is the same way. When it came to combining the codes, I got help from chatgpt. However, there are errors I get in the merging part. Some of those;
1- MPU and GSR sensors are printing data at the same time. But the MAX30102 sensor does not print.
2-BPM is always the same in codes where all sensors work at the same time.
3- While the MAX30102 sensor measures BPM, it cannot measure SPO2. The SPO2 value is constantly distorted or incorrect.
In general terms, this is the situation.

You did not answer my question. Please try again:

I am sure that I gave clear answers to your questions. Please do not give automatic answers. Thanks.

Since I am not able to understand the problem you are having with your project I won't be able to offer further assistance. Best wishes for success in your Arduino endeavors!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.