Using MAX30102 and MLX90614 Sensors

Hello everyone, I am attempting to use a MAX30102 heartbeat and SO2 sensor along with a MLX90614 temperature sensor on a raspberry pi pico. Using the sensors individually, I have no issues, but when I attempt to use both at the same time, I can only get the MAX30102 sensor to work. Doing some research, I think it's due to how MLX90614 sensor uses the wire protocol. I know that I can use Wire and Wire1, which I think would allow me to use both sensors, but I am not sure how to enable this on the pi pico. Any ideas on how I can get both to work?

Here is the code I am using:

#include <Wire.h>
#include "MAX30105.h"
#include <SparkFunMLX90614.h> //Click here to get the library: http://librarymanager/All#Qwiic_IR_Thermometer by SparkFun
#include "heartRate.h"
extern TwoWire Wire1;

MAX30105 particleSensor;
IRTherm therm; // Create an IRTherm object to interact with throughout

const byte RATE_SIZE = 8; //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
unsigned long previousMillis = 0;
long interval = 550;

#define numReadings 10
float readings[numReadings];
uint8_t readIndex = 0;
bool fullArray = false;

float readings1[numReadings];
uint8_t readIndex1 = 0;
bool fullArray1 = false;

float beatsPerMinute;
int beatAvg;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  therm.begin();
  therm.setUnit(TEMP_F);

  // Initialize Heartrate sensor
  particleSensor.begin(Wire, I2C_SPEED_FAST);
  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void temp() 
{  
  if (therm.read()) // On success, read() will return 1, on fail 0.
  {
    if (readIndex == numReadings) {
      fullArray = true;
      readIndex = 0;
    }
    readings[readIndex] = therm.object();
    readIndex++;

    uint8_t total;
    if (fullArray) total = numReadings;
    else total = readIndex;
    float average = 0;
    for (uint8_t i = 0; i < total; i++){
      average += readings[i];
    }
    average = average / total;

    if (readIndex1 == numReadings) {
      fullArray1 = true;
      readIndex1 = 0;
    }
    readings1[readIndex1] = therm.ambient();
    readIndex1++;

    uint8_t total1;
    if (fullArray1) total1 = numReadings;
    else total1 = readIndex1;
    float average1 = 0;
    for (uint8_t i = 0; i < total1; i++){
      average1 += readings1[i];
    }
    average1 = average1 / total1;
    
    Serial.print(average);
    Serial.print(",");
    Serial.println(average1);
  }
}

void loop(){
  
  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20)
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.print(beatAvg);
  delay(1000);
 
  if (irValue < 50000)
    Serial.print(" No finger?");

  Serial.println();
  }
  temp();

}

first of all you should use names that are self-descriptive

MAX30105 particleSensor;
what is the "particleSensor" ?? A sensor detecting SO2-paricles???

What does the IRTherm therm; object do
measuring temperatures???

Where is this temperature-sensor defined used in your code???

You are initialising the "therm"-object with no parameters.
I assume that then the term-object uses the standard I2C-bus and not the wire1

I guess you have taken just the example-codes delivered with the library.
These demo-codes are another example of how lazy the code-writer was in typing down a quick & dirty example-code

best regards Stefan

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