arduino-libraries / Arduino_LSM6DS3

LSM6DS3 Library for Arduino
GNU Lesser General Public License v2.1
30 stars 31 forks source link

Custom PCB Set SDA & SCL #48

Open digimatt22 opened 2 months ago

digimatt22 commented 2 months ago

Is there a method to set SDA and SCL pins for Wire to use in this library? I have a custom PCB and need to set the SDA and SCL pins to use on an ESP32.

per1234 commented 2 months ago

Hi @digimatt22. I see the relevant part of the library's API has not been documented.

The Arduino_LSM6DS3 library has a LSM6DS3Class constructor that takes a TwoWire class object:

https://github.com/arduino-libraries/Arduino_LSM6DS3/blob/25f3152d54b690f78a96f814effc3bcc09339d4c/src/LSM6DS3.h#L56

The library defines a default global instance of this class named IMU_LSM6DS3:

https://github.com/arduino-libraries/Arduino_LSM6DS3/blob/25f3152d54b690f78a96f814effc3bcc09339d4c/src/LSM6DS3.h#L95

https://github.com/arduino-libraries/Arduino_LSM6DS3/blob/25f3152d54b690f78a96f814effc3bcc09339d4c/src/LSM6DS3.cpp#L243

which is also usable via the IMU macro:

https://github.com/arduino-libraries/Arduino_LSM6DS3/blob/25f3152d54b690f78a96f814effc3bcc09339d4c/src/LSM6DS3.h#L97

The default instance uses the Wire instance of the TwoWire class. The TwoWire class is defined in the platform bundled Wire library (this one when using an ESP32 board).

So if you want to use an I2C interface other than the one provided by the Wire object then you can pass the TwoWire class object of the interface you do want to use to the library constructor.

You could instantiate another LSM6DS3Class object:

LSM6DS3Class MyCustomIMU(Wire1, LSM6DS3_ADDRESS);

(in this example, I am arbitrarily using the Wire1 TwoWire class object)

But it might be more efficient to reuse the existing instance created by the library:

void setup() {
  IMU = LSM6DS3Class(Wire1, LSM6DS3_ADDRESS);
  if (!IMU.begin()) {
    // ...

So, in answer to your question:

Is there a method to set SDA and SCL pins for Wire to use in this library?

No. This functionality would come from the Wire library. The Arduino_LSM6DS3 simply uses the standard interface of the Wire library for communication over the I2C bus to the LSM6DS3 chip.

So you should study the documentation for the ESP32 Wire library to learn what options you have for configuring the I2C interface:

https://docs.espressif.com/projects/arduino-esp32/en/latest/api/i2c.html

If you have any questions about doing that, you are welcome to post over on Arduino Forum:

https://forum.arduino.cc/c/using-arduino/microcontrollers/59

I'm sure we'll be able to help you over there.