I have a RPi4 and I have been using the I2C bus with 4 MCP23017 gpio expanders and this works fine. My intention is to use 8 MCPMCP23017 devices, as they have only 3 bit addressing, this is the maximum on 1 I2C bus. My question is does the RPi's I2C bus have a maximum device loading/fan out?
You do not understand I²C - this is an open drain bus, so the concept of fanout does not apply. Loading is limited more by capacitance - which in turn is related to bus length and layout in addition to the number of devices. You can maximise the number of devices by utilising low value pullups or buffering.
See Buffering and multiplexing in https://en.wikipedia.org/wiki/I²C
"See When there are many I2C devices in a system, there can be a need to include bus buffers or multiplexers to split large bus segments into smaller ones. This can be necessary to keep the capacitance of a bus segment below the allowable value or to allow multiple devices with the same address to be separated by a multiplexer."
With any sensible layout you should not have loading problems with 8 MCPMCP23017 devices.
If you're worried about too high bus capacitance, too high fan-out (if at all relevant in this case...) or if you want to connect more than 8 MCP23017 IO expanders to the RPi, then there's a very simple solution: connect an I2C switch like the PCA9548 (8 I2C buses) - or similar - to the original I2C pins of the RPi.
Since the Linux kernel has implicit support for this since a couple of years, the only thing you have to do is to add the following line to the file
This way, you will have an extra of 8 I2C buses where you can connect your extra devices to (use
i2cdetect -l to see the extra buses and their names). The only thing you have to do in your code is to tell your software to which
/dev/i2c-x device your I2C devices are connected.
- Spread of bus capacitance
- You don't have to care about the switching commands of the I2C MUX, the Linux kernel module is doing this for you based on the
- You can add a total of 64 MCP23017 devices to the RPi (indirectly, that is), meaning a (crazy) total of 64 x 16 = 1024 IO's (what more do you want...)
- Important: you can work with a mix of power supplies "at the other side" of the I2C MUX. This can be 3V3, but also 1V8 or 5V. Extremely convenient! Note that the front end of the I2C MUX must obey the power supply of the RPi (that is, 3V3) to avoid damage of the in/out pins.
Yes, there's one: you have to add extra hardware (although limited) to your project but you get back a lot for this small extra effort.