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

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