Specify alternate Raspberry Pi i2c bus in python

Hello,

I have a SparkFun Qwiic Single Relay (COM-15093) connected to a Raspberry Pi 3B+. It worked fine on the rpi’s default i2c bus (1). I had to add extra busses for another project so now the relay is connected to bus 5.

When I run ```
sudo i2cdetect -y 5


How do I create a relay object in Python that specifies using bus 5? 

I currently have something like this:

import qwiic_relay
relay = qwiic_relay.QwiicRelay()


This doesn't work because the relay is not connected to bus 1. I see that qwiic_relay relies on smbus2 and I know how to change the bus directly - ```
bus = smbus2.SMBus(5)
``` - but I don't know how to make the functions in qwiic_relay use bus 5.

Hi, have you tried these examples? https://github.com/sparkfun/Qwiic_Relay … n/examples

Yes I have, thanks. Those are good examples of changing the device address but not the bus. I’m using the relay’s default i2c address (0x18) but I’m not using the rpi’s default i2c bus. The relay works fine if it’s connected to bus 1. Bus 1 was getting overloaded so I added another bus via /boot/config.txt on the rpi. The rpi also detects the hardware correctly when it’s connected to bus 5; I just don’t know how to tell SF’s qwiic_relay library to use bus 5.

Any resolution to this? I’m running into the same issue on a Libre board. It has multiple i2c buses, but I don’t see an option in the qwiic python library to specify which bus to use. It seems to always use the first bus, which, of course, is not the bus with the qwiic hardware attached. Thanks.

Unfortunately no. I think the best option is to modify the SF qwiic_relay functions to take a bus argument. I haven’t had time to work on that lately. Someday…

Thanks for the reply. I’ll post a request on the GitHub project. Another forum suggested the Adafruit CircuitPython library. Maybe there’s something to borrow from there.

As a work around I discovered that I could modify the i2c adapter number that’s hard coded in https://github.com/sparkfun/Qwiic_I2C_P … nux_i2c.py on line 63. Changing the adapter to 0 allowed me to access my Qwiic device on that adapter. On my system, that file was located on this path: /usr/local/lib/python3.9/dist-packages/qwiic_i2c/linux_i2c.py

Hope this helps others.