Sharing I2C ports from 2 different PYs #18143
Replies: 3 comments
-
You would usually create only one I2C object instance and use it for both devices. The I2C object itself does not care for which device it is used. The devices are selected by the device address. |
Beta Was this translation helpful? Give feedback.
-
Thank you. I will put my I2C initialization function in main.py and pass
its handle to both the Xpndr.py and the Multiplx.py
and give that a try.
Regards Denis Lebel
…On Fri, Sep 26, 2025 at 10:17 AM Robert Hammelrath ***@***.***> wrote:
You would usually create only one I2C object instance and use it for both
devices. The I2C object itself does not care for which device it is used.
The devices are selected by the device address.
For that you have to take the instantiation of the I2C object out of the
device driver, create it separately and just supply the I2C object to the
driver.
—
Reply to this email directly, view it on GitHub
<#18143 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEFK5ELS7A4D7SEWZ4CKHNL3UVDHDAVCNFSM6AAAAACHS5RUKCVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTINJSGI4DMMY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
First, I misunderstood the question. Fun fact:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have two I2C devices connected on the same 2 pins (port) and so far I can only run each independently with similar code for creating their own I2C object/handle. One device is a PCF8574 expander, the other is a PCA9548 multiplexer.
The code for each I2C creating is very similar.
The expander i2c init code:
from machine import I2C,Pin
Xpn_sda = Pin(10) #Pin(12) on older homstat
Xpn_scl = Pin(11) #Pin(13) on older homstat
Xpn_i2c = I2C(1,sda=Xpn_sda,scl=Xpn_scl,freq=100000)
The multiplexer i2c init code:
from machine import I2C,Pin
Mux_sda = Pin(10) # mux on i2c port0 data
Mux_scl = Pin(11) # mux on i2c port0 clock
Mux_i2c = I2C(0,sda=Mux_sda,scl=Mux_scl,freq=100000)
Both examples work independently but I want to create a single I2C driver/py that would allow them
to share the same I2C port. I'm a newbie and I don't use classes (yet) but I might have to make the jump
to get this to work. I tried simply importing the I2C object/handle from a third I2C_driver.py but that
did not work out.
My question is how do I format/create a common I2C object that can service both devices
without creating 2 objects accessing the same port. I don't believe it matters but I'm using an
RP2040 and MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico with RP2040
Beta Was this translation helpful? Give feedback.
All reactions