RobTillaart / TCA9548

Arduino library for TCA9548 8 channel I2C multiplexer and compatibles.
MIT License
20 stars 7 forks source link

question about using the library #10

Closed wBrhy2 closed 1 year ago

wBrhy2 commented 1 year ago

Hello, when I run the scan on a given address (in my case 0x29 for a LiDAR module), do I need to change which address the i2c port is communicating on by setting the wire.begin() / MP.begin() / lidar.begin() / display.begin() every time I start commands that are meant to go to their respective device?

Thanks!

RobTillaart commented 1 year ago

Thanks for the question, Will come back to it later today.

wBrhy2 commented 1 year ago

Thank you, for a bit more context, when I use

 MP.selectChannel(chan);
    //bool b = MP.isConnected(LIDAR_ADDRESS);
    bool b = MP.isConnected(OLED_ADDRESS);
    Serial.print("CHAN: ");
    Serial.print(chan);
    Serial.print("\t");
    Serial.println( b ? "found!" : "x");

I get an X on channel 4 (which is where my OLED is), but in the same sketch,

MP.begin();
MP.enableChannel(4);

  //OLED setup
  if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(F("Hello, world!"));
  Serial.println("Hello, world!");
  display.display();

works just fine and displays

Sorry for not adding this earlier, some of that code was added after my initial message as I've been troubleshooting my code and design.

RobTillaart commented 1 year ago

Hello, when I run the scan on a given address (in my case 0x29 for a LiDAR module), do I need to change which address the i2c port is communicating on by setting the wire.begin() / MP.begin() / lidar.begin() / display.begin() every time I start commands that are meant to go to their respective device?

wire.begin() needs to be called once, it is the initialization of the hardware in the MCU/CPU for I2C communication.

MP,begin() needs to be called once as the CPU/MCU has constant connection with the multiplex module.

lidar.begin() must be called when the multiplexer has selected the channel that connects to the LIDAR. The reason is that possibly (probably) the MCU sends some commands to the Lidar during initialization. Or read from it.

display,begin() same as lidar.begin. The multiplexer need to have an opened channel to the display.

If the multiplexer has not the right channel open there is a serious chance the initialization goes (partial) wrong.

RobTillaart commented 1 year ago

Maybe you misaligned the channel numbers, they go from 0..7 not from 1..8

what is the datatype of chan in MP.selectChannel(chan);?

Note that enableChannel(chan) opens chan as a 2nd channel, while selectChannel(chan) closes all except chan.

A trick that helps debugging this kind of problems is to strip (a copy of) the sketch to a minimal one that shows the problem.

wBrhy2 commented 1 year ago

Thank you for the clarification, I will do the wire.begin statement only in my setup as per usual then, but make sure the correct channels are active at the right time!

As per your example, the datatype is for (int chan = 0; chan < 8; chan++)

I do go from 0 to 7 as per the code.

It's good to know that enableChannel(chan) closes the others, I was manually adding code to close them redundantly! Glad I can drop that... Especially since I have 4 LiDAR sensors that use the same address (it's changeable, but difficult and resets on every reboot, which is why I preferred using the MUX).

after each enable, I am using if (! sensorXP.begin()) as my wire.begin statement to initialize the i2c devices. It works fine on the OLED, but does not work just yet on my LiDAR. I think it's a hardware problem on my side though. From comparing to Pololu's module diagram, the only thing I can see is that the datasheet's advice of letting the interrupt pin float may have been bad advice. I'm about to test that now. These clarifications and other testing I've done have given me confidence that my outstanding issue is with my hardware design on the LiDAR side of things.

RobTillaart commented 1 year ago

It's good to know that enableChannel(chan) closes the others,

selectChannel(chan) closes the others, maybe I should rephrase that in the readme.md / .h file..

Especially since I have 4 LiDAR sensors that use the same address (it's changeable, but difficult and resets on every reboot, which is why I preferred using the MUX).

Do you have a link to the specific device / datasheet?

wBrhy2 commented 1 year ago

Sure! the board of my own design is using these directly

https://www.st.com/en/imaging-and-photonics-solutions/vl6180x.html

But to troubleshoot I've bought (and successfuly used) one of these https://www.pololu.com/product/2489