I want to connect mpu6050 as well as MAX30100 heart rate sensor to my Esp 32 board but the problem here is both sensors have SDA and SCL pins as it works on I2C protocol. So, is it right to define like this for MAX30100 sensor? And for mpu6050 i can connect SDA → 21 & SCL → 22. What will be the effective way to handle this situation?
I2C is a bus system that allows up to 127 devices to be connected to the same two pins on your MCU.
Each device needs to have a different address, and most devices allow their address to be changed via jumpers or solder pads. You should start by using an I2C scanner sketch to identify the addresses of the devices on your bus.
I’m naive to electronics because I’m a mechanical engineer who work on IOT. By the term I2C scanner sketch you wanna say that I have to use a breadboard and connect let’s say SDA of both MPU 6050 & MAX 30100 to 21 and similarly SCL of both sensors to 22 of ESP 32 board. Please correct me if I’m wrong. Just like in the case where if we’re using multiple sensors, we’ll use breadboards to supply VCC and GND to every sensors.
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
//#define MAX30100_SDA_PIN 23
//#define MAX30100_SCL_PIN 24
PulseOximeter pox; //This creates an instance of the PulseOximeter class, to interact with the MAX30100 sensor.
void setup()
{
Serial.begin(9600);
Wire.begin();
if (!pox.begin()) // if initialization of pulse oximeter fails it prints an error msg & enters an infinite loop
{
Serial.println("Error: Unable to initialize Pulse Oximeter");
while (1);
delay(3000);
}
}
void loop()
{
pox.update();
Serial.print("Heart rate: ");
Serial.print(pox.getHeartRate());
Serial.print("bpm & SpO2: ");
Serial.print(pox.getSpO2());
Serial.println("%");
delay(20000); // You can adjust the delay to control the reporting frequency
}
In this above code, I can’t able to read bpm & Spo2, it shows 0.00 in the serial monitor and even if it reads it is showing data like 0.57 or 3.22 bpm. What may be the issue do i have to change delay time or is there any problem which arises because of lack of contact of skin with the sensor? please help me out.
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
int error;
int address;
int devices = 0;
Serial.println("Devices found:");
for(address = 1; address < 127; address++){
Wire.beginTransmission(address);
error = Wire.endTransmission(); // when mcu communicate with I2C device it is gonna respond with a code we're storing it in here
if (error == 0){
Serial.print("0x");
if (address < 16) // if it is less than 16 it's gonna be a single character like f or a ie., 0a or 0f
Serial.print("0");
Serial.println(address, HEX); // lets say if address value is 22 we're gonna convert that 22 into hexadecimal number
devices++;
}
else if (error == 4){
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (devices == 0)
Serial.println("No I2C devices found");
delay(5000);
}
Done and dusted buddy, for MPU6050 → 0x68 & for MAX 30100 → 0x57. Please answer to the above question what might be the reason I can’t able to get correct values when i’m trying to run MAX30100 alone with the above code.
I wasn’t really interested in the scanner sketch, just the results.
Read the documentation for the libraries you are using for these two devices and see how to specify the device address when you initialise the object in your code.
I’ve read documentation and about I2C protocol as well. In I2C protocol we can interconnect every scl & sda pins from esp32. But one shortcoming is that let’s say I’m using two mpu 6050 in a same project then if both of em having same address we can’t connect them in a same sda & scl line. So, it is perplexing to me whether I have to do any modifications in code or not.
No I just tried to change the delay timing here and there to get readings and now I’m able to run max30100 alone with esp32. I’m glad that you have pointed out this.
Which is why you ran an I2C scanner to establish that both devices have different addresses - which they do.
That’s why I told you to read the documentation for the two libraries you are using, to see how to tell that library which address the device is at on the bus.
Yesterday I tried to execute both of the sensor with blynk but only mpu 6050 data were getting updated max 30100 sensor is not showing any changes in dashboard.
We’re going around in circles here.
Your issue isn’t Blynk related, and you’d be better to use a non-Blynk sketch to begin with.
Until you read the documentation for whichever MPU6050 and MAX 30100 libraries you are using, and understand how to address both devices on the same bus, you’ll keep going over the same ground and getting nowhere.
You need to go back to basics! Forget Blynk for now. Read the documentation for one of your sensors, say mpu6050 and any documentation for the module containing the mcp6050. As Pete mentioned often these devices have selectable I2C addresses, if that is the case then you can set a different I2C address for each mcp6050 module. The max30100 may also have options to set its I2C address. I believe it is possible on an ESP32 to set multiple i2C buses using different pin pairs.
As you are new to ESP’s and electronics I highly recommend you go to www.randomnerdtutorials.com, there you will find lots of imformation to help you get started with the sensors you want to use and ESP’s in general. Only when you have a sketch runnung that gets data from your sensors should you consider incorporating Blynk into the set up. Blynk is a very good platform but it doesn’t deal with getting the data to send to Blynk in the first place.