0
votes

I have trouble using two sensors on two I2C interfaces.

The problem is, that I when I try to replace the Wire.begin() with something like this: I2C.begin(21, 22), my sensor QMC5883L returns for x, y and z 0. I included the Wire library manually and created a TwoWire object:

#include <Wire.h>   
TwoWire I2C = TwoWire(0);

When I tried this, I commented the _wire->begin() in the library, It worked with the commented line and with Wire.begin() in the sketch, but when I try to replace Wire.begin() with I2C.begin(21, 22) it doesn't work anymore.

The reason, why this should work is that I want to create 2 interfaces like that and then give them to the compass.begin(&I2C) method. In the library I have this:

void QMC5883LCompass::init(TwoWire *theWire){
  _wire = theWire;
  _wire->begin(); // this is the line I commented, when getting the zeros...
  _writeReg(0x0B,0x01);
  setMode(0x01,0x0C,0x10,0X00); 
}

Did I forget something?

1

1 Answers

0
votes

Problem is that when calling _wire->begin() is actually calling the default definition (from Wire.h):

bool begin(int sda=-1, int scl=-1, uint32_t frequency=0);

Default parameters -1 for SDA/SCL means default pins, wich overwrites the ones you added in the first begin(...)

Presuming you are using https://github.com/mprograms/QMC5883LCompass, the solution is to subclass QMC5883LCompass and override the void QMC5883LCompass::init() or create a new init(...) with your specific needs.