Adafruit I2C Multiplexer Code

Hello, our team is trying to use multiple Rev Robotics 2m Distance tof sensors on our robot this year for sensing purposes (http://www.revrobotics.com/rev-31-1505/). When doing research, I found that these have static I2C addresses and so only one can be directly wired to the RoboRio. If using a multiplexer, the addresses can be changed so we can wire multiple of the same sensor. The adafruit multiplexer which seems to the most common (https://www.adafruit.com/product/2717) seems like it should work but I haven’t been able to find any sample code or resources as to how to program this with FRC and any libraries that may be needed for it. Any help would be appreciated.

Thanks,
Rajin

https://www.chiefdelphi.com/search?q=i2c%20mux



You’re off to a good start. When you figure out your solution, please do the rest of CD a big favor and share what you’ve learned. Many other OPs have abandoned their threads, where any one of those links could’ve been the solution to your problem.

I am currently working on this very thing for team 4061. I hope to have working code tonight or tomorrow. You might have noticed that the REV ColorSensor V3 uses the same I2C address as the 2m Distance sensor so a multiplexer is needed to have that on the same bus as a distance sensor, as well.

I am not planning on exposing the full API of either sensor, but it should be easy to add whatever you determine that you need for the color sensor. The Automatic Mode of the distance sensor will absolutely not work with the approach that I am using, but I don’t think that is necessarily a bit drawback. We can discuss later if you think that you need something like it – there are other ways to accomplish what it does.

I’ll post here and leave a pointer to our code when it is finished.

Thank you very much we would largely appreciate code to show how to use the multiplexer.

What do you mean by automatic mode? I don’t imagine we’d be using it as we just want basic distance tracking but I am still curious.

Automatic mode is one of the features of the REV 2m Distance Sensor Library. It reads the sensor asynchronously in a separate thread. You can look at the REV library to learn more.

Ohh yeah we probably don’t need an asynchronous task running for the sensor.

Our preliminary code (C++) is here: https://bitbucket.org/sciborgs4061/i2cmultiplexer. Unfortunately I ran out of time yesterday for debugging, so as explained in the README, it compiles but does not fully work.

I’d appreciate hearing from anyone who attempts to get this working and what results you achieve. I will be able to work on it again starting this Wednesday evening. I think it is close, but you never know :slight_smile:

We’ve updated the repository with working code. We’ve tested this successfully for 2 distance sensors and one color sensor. We hope you find it useful. Get the code here: https://bitbucket.org/sciborgs4061/i2cmultiplexer.