MS5837.h, Arduino, and Wire.h - how to specify which I2C to use?

Hi everyone,

I’m wondering if there’s an easy way to specify which of Wire.h’s “Wires” an MS5837 pressure sensor uses in Arduino code.

I’m trying to connect four different MS5837s, each to their own respective I2C port on a Teensy 3.6 (which conveniently has four I2C ports). The libraries make it very easy to connect one of them, however, the MS5837 library always specifies the use of just “Wire” in its function calls. Meanwhile, the Wire.h library allows for the use of multiple ports by using Wire, Wire1, Wire2, etc. in the function calls.

Has anyone encountered this problem and can offer a quick solution?

Ok, perhaps i’m talking out of my depth here (pun) but doesn’t I2C act as a serial connection, with each device having it’s own address on the line? Why plug each device into a separate port?

I use a 32 channel ADC for analog input AND a 32-channel SSC32U servo controller on the same I2C port…

Unless I’m mistaken, the MS5837’s I2C addresses are fixed, meaning they can’t be communicated with individually on the same bus without an I2C multiplexer (which I don’t have on hand).

In case anyone’s interested, I just ended up modifying the MS5837 library itself (and also replaced the “Wire.h” library with the more Teensy 3 I2C friendly “i2c_t3.h” library).

The only thing I changed was to pass i2c_t3 objects as arguments for the init() and read() functions to replace “Wire” with the argument name (to be declared in the actual Arduino code).

For example, in MS5837.cpp, I changed…

MS5837::init() {
Wire.beginTransmission(MS5837_ADDR);
}

to…

MS5837::init(i2c_t3 WireNum) {
WireNum.beginTransmission(MS5837_ADDR);
}

And did the same for all instances of Wire.xxx() in MS5837.cpp. Now in my Arduino code, when I would have called…

sensor.init();

I now call…

sensor.init(Wire);

This allows me to instantiate different MS5837 objects and communicate with them via Wire, Wire1, Wire2, or Wire3 respectively.

I also added the “Arduino.h” header to at least one of my source files along the way - can’t recall which… but worth noting.

Much easier than expected!

2 Likes

Glad you figured it out. Youtube suggested this video to me this morning, oddly enough… Solving I2C Address Conflicts - TCA9548A I2C Multiplexer - YouTube

And i found the multiplexer he suggests for $7… Overview | Adafruit TCA9548A 1-to-8 I2C Multiplexer Breakout | Adafruit Learning System

Hi Zurn,
Could you somehow post or send your .cpp .h and arduino code? -I tried to implement what you wrote but I can’t make it compile for Teensy 3.2 (or any other)
I am using just one of the MS5837 for the Teensy 3.2
Thanks