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?
-
1I am sure it does. But I doubt you will get a more useful answer than try it and see for any particular configuration. The best source of GPIO information may be any compute module specs you can find.– joanAug 18, 2020 at 21:47
-
Ah, let me see. (1) In my Rpi3B+ days with flat rate of I2C 100kHz, I once put 8 mcp23017s one bus, together with a couple of other I2C guys,but I found them not stable, with too frequent #121 I/O Errors, (2) Now with Rpi4B's 5 I2C buses, and you can now (not for 3B+) lower I2C speed to much lower than 100Kz, so each MCP23017's chip and associated wiring capacitance is lowered, resulting total impedance not that easily exceed the I2C hard limit of 400pF (3) of course you can use I2C mux, extenders, buffers, CAT 5 wiring etc, to push your limit. / To continue, ...– tlfong01Aug 19, 2020 at 3:45
-
I have blogged 150+ posts on MCP23017 a year ago. Perhaps you can skim them and see if any of my learning experiences useful: Refs - (1) Search found 178 matches: mcp23017 tlfong01 Searched query: mcp23017 tlfong01 - tlfong01, Rpi.org.forums, 2020aug1901 raspberrypi.org/forums/search.php?keywords=mcp23017+tlfong01 (2) MCP23017 Learning Notes - tlfong01, rpi.org.forums, 2019jan23 raspberrypi.org/forums/viewtopic.php?f=37&t=231984 (3) I2C I/O Port Expander Q&A - tlfong01, rpi.org.forums, 2019mar02 raspberrypi.org/forums/viewtopic.php?f=45&t=234869.– tlfong01Aug 19, 2020 at 3:46
-
If you can use Rpi's 5 SPI buses, you can also try MCP23S08, MCP23S17. Actually I once use MCP23017 GPIO pins to fake SPI CS pins, and found it OK. This way, you have (almost) unlimited number of SPI buses. You might like to see how I mux the SPI buses with the following examples: Refs: "Muxing SPI with MCP23S08/17, MCP3008/3201/3208 Examples": (1) electronics.stackexchange.com/questions/512899/…, (2) electronics.stackexchange.com/questions/515225/….– tlfong01Aug 19, 2020 at 3:56
-
1@tlfong01 This site is all about getting answers. It's not a discussion forum. … Use comments to ask for more information or clarify a question or answer.– MilliwaysAug 20, 2020 at 7:24
2 Answers
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 /boot/config.txt
(as sudo
):
dtoverlay=i2c-mux,pca9548,addr=0x70
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.
Some advantages:
- 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
/dev/i2c-x
bus. - 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.
Disadvantage:
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.