Using MAX30100 and MPU6050 with ESP32

Hello all

I have an ESP32 board, MAX30100 pulse sensor and an MPU6050 accelerometer. I’m trying to get data from both these sensors simultaneously. Problem is, they both use I2C and my ESP32 has only 2 dedicated pins (D21 and D22). Now after running the code, the MPU6050 sensor is giving the data but MAX30100 is showing 0 as data.

When you ran the I2C scanner with both devices connected what did it report?

Multiple I2C devices can be connected to the I2C pins.

@sajalsg08, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

there is no compiling error. It is giving the output from mpu6050 sensor only. for max30100 sensor it is displaying 0 as data.

Have you tried with only the MAX30100?

Is the MAX30100 chip or board a 3.3V or 5V device?

Does an I2C scanner pick the MAX30100 up?

Might it be an idea to show your code?

When you ran the I2C scanner was both devices detected?

hello
I am using max30100 and mpu6050 sensor. When i combined code for both the sensors. i'm getting only data of mpu6050 and max30100 data is coming 0 all time. please help me

if you test the sensors independently do they give correct results?
upload your code (using code tags </>) and Serial Monitor output

When you ran the I2C scanner was both devices detected?

I have merged your cross-posts @sajalsg08.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I am uploading my code

//Firebase
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
#include "time.h"

//I2C
#include <Wire.h>

//MAX30100
#include "MAX30100_PulseOximeter.h"

//OLED
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//MPU6050
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

//Firebase
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
#define API_KEY ""
#define DATABASE_URL ""
#define USER_EMAIL ""
#define USER_PASSWORD ""

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
unsigned long count = 0;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 16200;
const int daylightOffset_sec = 3600;

//MAX30100
#define REPORTING_PERIOD_MS 100
PulseOximeter pox;
uint32_t tsLastReport = 0;
void onBeatDetected()
{
Serial.println("Beat!");
}

//OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

//MPU6050
Adafruit_MPU6050 mpu;

//KY-013
int ThermistorPin = 0;
int Vo;
float R1 = 10000; // value of R1 on board
float logR2, R2, T;
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; //steinhart-hart coeficients for thermistor

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

//Firebase
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("Failed connection");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
Firebase.setDoubleDigits(5);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

//OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("OLED failed"));
for(;;);
}
delay(2000);
display.clearDisplay();

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Hello, world!");
display.display();

//MPU6050
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens

Serial.println("Adafruit MPU6050 test!");

// Try to initialize!
if (!mpu.begin(0x68)) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}

mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}

Serial.println("");
delay(100);

//MAX30100
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
} else {
Serial.println("SUCCESS");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
pox.setOnBeatDetectedCallback(onBeatDetected);

}

void loop()
{
//Time
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
char timeWeekDay[100];
strftime(timeWeekDay,100, "%d %B %Y, %H:%M:%S", &timeinfo);

//KY103
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0); //calculate resistance on thermistor
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2)); // temperature in Kelvin
T = T - 273.15; //convert Kelvin to Celcius
// T = (T * 9.0)/ 5.0 + 32.0; //convert Celcius to Farenheit
Serial.print("Temperature KY103: ");
Serial.print(T);
Serial.println(" C");

//MAX30100
if (pox.begin()){
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
tsLastReport = millis();
}}

//mpu.begin(0x69);

//MPU6050
if (mpu.begin(0x68)){

sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");

Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");

Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" degC");
Serial.println("");

FirebaseJson json;

json.add(String(timeWeekDay), a.acceleration.x);
Firebase.updateNode(fbdo, F("/new/json/acceleration/x"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), a.acceleration.y);
Firebase.updateNode(fbdo, F("/new/json/acceleration/y"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), a.acceleration.z);
Firebase.updateNode(fbdo, F("/new/json/acceleration/z"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), g.gyro.x);
Firebase.updateNode(fbdo, F("/new/json/rotation/x"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), g.gyro.y);
Firebase.updateNode(fbdo, F("/new/json/rotation/y"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), g.gyro.z);
Firebase.updateNode(fbdo, F("/new/json/rotation/z"), json) ? "ok" : fbdo.errorReason().c_str();

json.add(String(timeWeekDay), temp.temperature);
Firebase.updateNode(fbdo, F("/new/json/temperature"), json) ? "ok" : fbdo.errorReason().c_str();

}
// mpu.begin(0x61);

delay(5000);
//Wire.beginTransmission(0x00);
}

yes they gave results independently. I have uploaded my code please check it

yes both sensors were detected

Please edit your post #11, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

I am uploading my code


//Firebase
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
#include "time.h"

//I2C
#include <Wire.h>  
                                                                                                            
//MAX30100
#include "MAX30100_PulseOximeter.h"

//OLED
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//MPU6050
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>



//Firebase
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
#define API_KEY ""
#define DATABASE_URL "" 
#define USER_EMAIL ""
#define USER_PASSWORD ""

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
unsigned long count = 0;
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 16200;
const int   daylightOffset_sec = 3600;

//MAX30100
#define REPORTING_PERIOD_MS 100
PulseOximeter pox;
uint32_t tsLastReport = 0;
void onBeatDetected()
{
Serial.println("Beat!");
}

//OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

//MPU6050
Adafruit_MPU6050 mpu;

//KY-013
int ThermistorPin = 0;
int Vo;
float R1 = 10000; // value of R1 on board
float logR2, R2, T;
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; //steinhart-hart coeficients for thermistor


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

//Firebase
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Failed connection");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  config.api_key = API_KEY;
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;
  config.database_url = DATABASE_URL;
  config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
  Firebase.setDoubleDigits(5);
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  
 
//OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("OLED failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello, world!");
  display.display(); 

//MPU6050
 while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin(0x68)) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  Serial.println("");
  delay(100);

   //MAX30100
  if (!pox.begin()) {
  Serial.println("FAILED");
  for(;;);
  } else {
  Serial.println("SUCCESS");
  }
  pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
  pox.setOnBeatDetectedCallback(onBeatDetected);

}

void loop()
{
//Time
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  char timeWeekDay[100];
  strftime(timeWeekDay,100, "%d %B %Y, %H:%M:%S", &timeinfo);


//KY103
 Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0); //calculate resistance on thermistor
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); // temperature in Kelvin
  T = T - 273.15; //convert Kelvin to Celcius
 // T = (T * 9.0)/ 5.0 + 32.0; //convert Celcius to Farenheit
  Serial.print("Temperature KY103: "); 
  Serial.print(T);
  Serial.println(" C"); 
    
//MAX30100
if (pox.begin()){
  pox.update();
  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
  Serial.print("Heart rate:");
  Serial.print(pox.getHeartRate());
  Serial.print("bpm / SpO2:");
  Serial.print(pox.getSpO2());
  Serial.println("%");
  tsLastReport = millis();
}}

//mpu.begin(0x69);

 

//MPU6050
if (mpu.begin(0x68)){

sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");
  
  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");
  
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");
  Serial.println("");

  FirebaseJson json;

  json.add(String(timeWeekDay), a.acceleration.x);
  Firebase.updateNode(fbdo, F("/new/json/acceleration/x"), json) ? "ok" : fbdo.errorReason().c_str();

  json.add(String(timeWeekDay), a.acceleration.y);
  Firebase.updateNode(fbdo, F("/new/json/acceleration/y"), json) ? "ok" : fbdo.errorReason().c_str();

  json.add(String(timeWeekDay), a.acceleration.z);
  Firebase.updateNode(fbdo, F("/new/json/acceleration/z"), json) ? "ok" : fbdo.errorReason().c_str();

  json.add(String(timeWeekDay), g.gyro.x);
  Firebase.updateNode(fbdo, F("/new/json/rotation/x"), json) ? "ok" : fbdo.errorReason().c_str();

  json.add(String(timeWeekDay), g.gyro.y);
  Firebase.updateNode(fbdo, F("/new/json/rotation/y"), json) ? "ok" : fbdo.errorReason().c_str();
  
  json.add(String(timeWeekDay), g.gyro.z);
  Firebase.updateNode(fbdo, F("/new/json/rotation/z"), json) ? "ok" : fbdo.errorReason().c_str();

  json.add(String(timeWeekDay), temp.temperature);
  Firebase.updateNode(fbdo, F("/new/json/temperature"), json) ? "ok" : fbdo.errorReason().c_str();

 }
 // mpu.begin(0x61);
  
  delay(5000);
//Wire.beginTransmission(0x00);
}

thanks, I have made the changes as mentioned by you.

have you solved the problem/
Can you help me with the code.please

Sajalsg08, I have same problem.. Can u help me for fix this problem.. i combined MAX30102 & MPU6050.. then MPU6050 reading ok.. but Max30102 connected well but it read is zero... plz help me to fix this..

have you found the solution for this problem, i am also facing same problem

1 Like

please what software did you use to build the circuit?