# MS5837 read i2c Problems

**URL:** <https://discuss.bluerobotics.com/t/ms5837-read-i2c-problems/14235>\
**Category:** General Discussion\
**Created:** [May 2, 2023, 6:30am UTC](https://discuss.bluerobotics.com/t/ms5837-read-i2c-problems/14235 "2023-05-02T06:30:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![allacmc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/allacmc/32/10615_2.png) [@allacmc](https://discuss.bluerobotics.com/u/allacmc)\
**Post date:** [May 2, 2023, 6:30am UTC](https://discuss.bluerobotics.com/t/ms5837-read-i2c-problems/14235/1 "2023-05-02T06:30:47Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/a/a2b1323ef9b292e95c5e3b9320a0f2d9a4a114a2.jpeg)

![image](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/3/3aab818832c1fd075cc3efccee8dce1d65dac2c1.jpeg)

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?

```auto
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.

```auto
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;
          }
       
      }
}

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/c/c0c1035ed39add5877127f9013040bfcd5539001.png)
