MS5837 read i2c Problems

Could someone kindly help me?

I am using the sensor from Blue Robotics MS5837-BA

I’ve tested this sensor many times on the raspberry PI and it works great.

But when I go to ESP32 I always have some kind of problem.

My problems are related to initialization and sensor reading.

It just doesn’t work or gives a completely out-of-range reading.

The worst thing is that it’s not always, sometimes it works perfect.

I’m using this microcontroller: WT32-ETH01

image

I’ve tried everything to stabilize the readings. I’ve added 1K and 10K pull-up resistors and I’ve also tested without a resistor.

I can’t find a pattern that works well without going offline.

Is there anything I can do in the code? What can improve this situation?

void f_SetupSensor(){
     if (!Modo_LeitorRS485){
         Wire.begin(2,4);
         AtivarSensor = true;
         while (!sensor.init()){oErro.insertErro(10);}
         sensor.setModel(MS5837::MS5837_30BA);
         sensor.setFluidDensity(1029); // kg/m^3 (freshwater, 1029 for seawater)
     }
}

This microcontroller doesn’t have a standard i2c port so I have to choose which port I’m going to use.

void f_SlaveSensor(){
      if (AtivarSensor){
          sensor.read();
          oRS485.prof = oConf.round2(sensor.depth());
          if (oConf.Confere_Prof(oRS485.prof)){
              Sensor_ok = true;
              push(prof_Array, oRS485.prof);
              oRS485.pressu = sensor.pressure();
              oRS485.temp = sensor.temperature();
              oRS485.prepPubli();
              printSensor();
              if (AtivarMQTT){
                  f_publica(PubTopic, oRS485.prof_C);
                  f_publica(PubTopic2, oRS485.temp_C);
              }
              if (AtivarRS485){
                  if (oRS485.f_LerSerial() == true){
                      RS485_ok = true;
                      //displayOled(prof, pressu, temp);
                      oRS485.prepEnvio();
                      oRS485.f_Envia(oRS485._prof);
                      oRS485.f_Envia(oRS485._pres);
                      oRS485.f_Envia(oRS485._temp);
                   }else{RS485_ok=false;}
              }
           }else{
              oErro.insertErro(12);
              oRS485.prof = 0;
              oRS485.pressu = 0;
              oRS485.temp = 0;
              push(prof_Array, oRS485.prof);
              Sensor_ok=false;
          }
       
      }
}