I2C Sensors together with ESP32

Hello,
I'm trying to connect three I2C Sensors, and for now I've been only able to connect and read one device ( MLX90614 ). Both the MAX30105 and the MAX30205 are being troublesome.
I've already tried for example the Protocentral and the ClosedCube libraries for the MAX30205 but both aren't working.

I've also tried using only one sensor at a time, but that also doesn't get me any further.

I'm using an ESP32 from Waveshare (https://www.waveshare.com/esp32-s3-lcd-1.28.htm) and the SDA / SCL GPIO are 6, 7 respectively.

#include <Wire.h> //I2C Header

//HeartRate Includes
#include "MAX30105.h"
#include "heartRate.h"
#include "spo2_algorithm.h"
//#include "MAX30102_PulseOximeter.h"
#include <Adafruit_MLX90614.h> //IR Temperature
#include "Protocentral_MAX30205.h" //Touch Temperature
//#include "ClosedCube_MAX30205.h"

void TemperatureSetup();
void TemperatureRead();
void IRTemperatureSetup();
void IRTemperatureRead();
void HeartRateSetup();
void HeartRateLoop();

//Temperature Variables
Adafruit_MLX90614 temperatureIRSensor = Adafruit_MLX90614();
MAX30205 tempSensor;
//ClosedCube_MAX30205 max30205;

//HeartRate Variables
MAX30105 heartRateSensor;

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;

void setup()
{
    Serial.begin( 115200 ); /* prepare for possible serial debug */
    Wire.setPins(6, 7);
    
    HeartRateSetup();
    BatterySetup();
    BatteryLevelImage();
    IRTemperatureSetup();
    TemperatureSetup();
   
    Serial.println( "Setup done" );
}

void loop()
{
unsigned long currentMillis = millis();

    /* The Arduino executes this part of the code once every second
    *  (interval = 1000 (ms) = 1 second).
    */
    if(currentMillis - previousMillis > interval)
    {
        BatteryLevelRead();
        IRTemperatureRead();
        TemperatureRead();

        // Don't forget to update the previousMillis value
        previousMillis = currentMillis;
    }

    HeartRateLoop();
}

void TemperatureSetup() {
    //max30205.begin(0x48);
    while(!tempSensor.scanAvailableSensors()){
    Serial.println("Couldn't find the temperature sensor, please connect the sensor." );
  }

  tempSensor.begin();   // set continuos mode, active mode*/
}

void TemperatureRead() {

    /*Serial.print("T=");
	Serial.print(max30205.readTemperature());
	Serial.println("C");*/
    
    float temp = tempSensor.getTemperature(); // read temperature for every 100ms
	Serial.print(temp ,2);
	Serial.println("'c" );
}

void IRTemperatureSetup() {
    if (!temperatureIRSensor.begin()) {
            Serial.println("Error connecting to MLX sensor. Check wiring.");
            while (1);
    };
} 

void IRTemperatureRead() {
    Serial.print("Ambient = "); Serial.print(temperatureIRSensor.readAmbientTempC());
    Serial.print("*C\tObject = "); Serial.print(temperatureIRSensor.readObjectTempC()); Serial.println("*C");
    Serial.print("Ambient = "); Serial.print(temperatureIRSensor.readAmbientTempF());
    Serial.print("*F\tObject = "); Serial.print(temperatureIRSensor.readObjectTempF()); Serial.println("*F");
}

void HeartRateSetup() {

    // Initialize sensor
    if (!heartRateSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
    {
        Serial.println("MAX30105 was not found. Please check wiring/power. ");
        while (1);
    }
    Serial.println("Place your index finger on the sensor with steady pressure.");

    heartRateSensor.setup(); //Configure sensor with default settings
}


void HeartRateLoop()
{
    long irValue = heartRateSensor.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);

    if (irValue < 50000)
        Serial.print(" No finger?");

    Serial.println();

    snprintf(heartRateText, sizeof(heartRateText), "#FFFFFF BPM: %d", beatAvg);
    lv_label_set_text(heartRateLabel, heartRateText);
}

ClosedCube Library Error:
[E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
256.00C

SparkFun MAX30100x Library Error:
Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
MAX30105 was not found. Please check wiring/power.

ProtoCentral Error:
Couldn't find the temperature sensor, please connect the sensor.

Thank you :slight_smile:

What are the I2C addresses of your devices?

If they are connected together on the same BUS, they will have to have different addresses, and I didn't see in your code where you inform the address of each device.

" Every MLX90614 has a default I2C address of 0x5A . However, it can be programmed to have one of 127 I2C addresses so that you can add up to 127 devices to the same bus to get a larger temperature map.

From the SparkFun header:
#define MAX30105_ADDRESS 0x57 //7-bit I2C Address

From the ProtoCentral header:
#define MAX30205_ADDRESS1 0x49 // 8bit address converted to 7bit
#define MAX30205_ADDRESS2 0x48 // 8bit address converted to 7bit

From the Adafruit MLX90614 header:
#define MLX90614_I2CADDR 0x5A

Using ClosedCube MAX30205 Library:
max30205.begin(0x48);

Only the MLX90614 works.

Start by running the Arduino I2C Address Scanner program, and checking whether each individual device is recognized at an expected I2C address. If not, fix that problem.

The next step is to get one of the library examples working for each device individually. If you run into problems, fix them before moving on.

When all devices are working properly with library examples, then start adding one at a time to your code, testing each step as you go.

1 Like

Running the I2C Scanner finds these:

I2C device found at address 0x48 !
I2C device found at address 0x57 !
I2C device found at address 0x5A !
I2C device found at address 0x6B !
I2C device found at address 0x7E !

Great! Try a library example with each individual sensor, with none of the others connected to the ESP32. If errors crop up, copy/paste those and post along with the example code, in your next post.

how do you power the I2C devices?

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