Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

Arduino rewriting library to use 2 I2C Interfaces (ESP 32)

I want to use 2 QMC5883L sensors on a esp32 for my project, to do that, I need two I2C interfaces. That means I could make 2 TwoWire objects like that:

#include <Wire.h>
#include <QMC5883LCompass.h>
#define SDA_1 21
#define SCL_1 22
#define SDA_2 33
#define SCL_2 32

TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);

QMC5883LCompass compass;

and then, when I call the compass.init() function in the setup, I need to pass that wire object to my library. Maybe like that: compass.init(SDA_1, SDA_2, I2Cone);

The library code looks like that:

void QMC5883LCompass::init(){

Wire.begin();

_writeReg(0x0B,0x01);
setMode(0x01,0x0C,0x10,0X00);
}

What do I have to change, so that I can use the two sensors? Something like Wire.begin(SDA, SCL, theWire) in the library .cpp file?

Or is there generally an easier way?

thx Ferdi

Edit: There is NO easier way to do this and I don't know why this question got 2 dislikes, for it was not a stupid question because you can't change the address of a sensor (some can this, but not my QMC5883L...)

question from:https://stackoverflow.com/questions/65835034/arduino-rewriting-library-to-use-2-i2c-interfaces-esp-32

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...