MS5837 library not working

I am trying to use a MS5837 pressure sensor, but continue to get the message that the sensor cannot initialize. The library was intended for use with Arduino, but a friend told me that if I changed all of the instances of D1/D2 to P1/P2 it should work. The SCL goes to D1 and the SDA goes to D0. I personally don’t understand how to port Arduino libraries (or really just use libraries) and am rather new to Particle, so I would appreciate any help. The error message I am receiving is in the setup function for the .ino file

The wiring layout:

You can see my code here: https://go.particle.io/shared_apps/5ad77ac492e8e6384b0012bc
And the original library is here: https://github.com/bluerobotics/BlueRobotics_MS5837_Library

Are these wires and the Electron soldered into the perma proto?
Doesn’t look like it.

Since we can’t see whether you are using the sensor alone or a breakout, chances are that you need to add pull-up resistors to D0/D1 too.

I’m sorry, I don’t know what a breakout is. However, considering I don’t know I would guess I am using the sensor alone.

They are soldered:

What would the pull-up resistors do and why would I need them?

Apologies for my lack of knowledge.

I see where you did this in the library and I'm not sure how this would "port" the library. All this does is change the name of a variable which has no effect on the actual code.

Pull-up resistors keep the signal at a "high" voltage when the source of the signal is not applying an voltage to them. The "Wire" library that your MS5837 library uses is a wrapper for the I2C module on the electron. I2C uses an "open-drain" output so it only pulls the signal low, and doesn't drive it high. There are lots of resources on I2C that for more information like:
https://www.i2c-bus.org/i2c-primer/how-i2c-hardware-works/
https://learn.sparkfun.com/tutorials/i2c/

A "breakout" refers to a breakout board which takes your small, difficult to prototype, (SMD/SMT) sensor and makes it more friendly. If your sensor is on a PCB, it is most likely on a breakout board

1 Like

The pull-up resistors are a good place to start. Connect one side of a resistor the wires going into D0/D1 and the other side to 3V3. You’ll need one resistor for each wire.

Particle has gone through a lot of effort to make most Arduino libraries directly transferable to Particle devices without porting.
https://docs.particle.io/guide/tools-and-features/libraries/

You might try starting fresh with the Arduino library and the pull-up resistors.

Also, while your at it, check continuity between the sensor pins and the pins on your electron.

1 Like

Actually this does matter in this case. Since D1 & D2 in the Particle world are defined as the pin designators having these used as variable names too will create problems. So renaming them is a crucial part in "porting" the library.
However, since 0.6.3 not a lot has to be done in order to port Arduino libraries that don't rely on the AVR hardware specifics.

1 Like

Thank you all so much, this is part of my project for the FLL World Championship which is in a couple days, so these quick responses saved my life :grin:

One more question: how many ohms resistor should I use & do I need to change my program to compensate?

10k is a good place to start. You can go higher (100k or 1M) if you have really low power requirements but the trade-off is the current/voltage response time and thus the bandwidth. If power draw doesn’t matter, and you want really fast speeds or have really long wires you could drop the resistance to 5k or even 1k.

https://learn.sparkfun.com/tutorials/pull-up-resistors

Great! The speed doesn’t really matter, so it’s all good. So I don’t need to alter my code, right?

You may not need to change your code. @ScruffR correctly pointed out that D1, and D2 variable names are incompatible with the Particle DeviceOS so you will want to leave those as P1/P2.

If you’re still having issues after installing the pull-up resistors, you may want to start looking at your code. The libraries handle a lot of the heavy lifting for you but I2C can be fickle.

Good luck!

Thanks! The resistors did the trick, but that fickleness is coming into play - the readings for the sensor are dropping by 2-10 every time it logs to the serial monitor. Do you know what the cause/fix for this could be?

As mentioned in the PM - this is now the second time (first about the fact about needed pull-ups and then the sensor drift) that the same question was posted here and via PM simultaneously - that’s not appreciated.