PCA9548A I2C mux device overlay for Pi gpio-I2C

Add 8 I2C buses for GPIO software I2C on Raspberry Pi with device tree overlay so programs like i2cdetect work like the hardware I2C.

Public Chat
Similar projects worth following
The 9548A is a popular device to use for more flexibility with I2C connections. Many devices have a fixed I2C address that limits use to only one device per I2C bus. To add more devices, you need to have more I2C ports to connect with. The PCA9548 solves this problem, as well as the problem of buffering sensors that use voltages other than 3.3v. You can mix 3.3v and 5v sensor connections, the limitation being that all devices on a bus operate at the same voltage.

If you simply connect this device - and do not install what is called a "device tree overlay" you have to write code that switches the multiplexer to the desired I2C bus connection. If you add the device tree overlay, the linux kernel will find the mux device on the GPIO software bus and create device-tree entries that allow you to simply specify the "/dev/i2c-X" to access the bus. Other I2C utilities like "i2cdetect" work just as they do with the hardware /dev/i2c-1.

The device can be "PCA9548A" or "TCA9548A" when supplied by Texas Instruments.   The specifications are somewhat different - from the Texas Instruments product page for PCA9548A:

Recommended alternative parts

  • TCA9548A  - The device has the SAME FUNCTIONALITY and PINOUT as the compared device but is NOT an exact equivalent.   This P2P device has a wider voltage range (down to 1.65V) and performance enhancements (Reset and Power-On Reset)

I originally wrote this device tree overlay for Mycodo - since we needed a way to have the multiplexer work on a GPIO software I2C bus that is more compatible with SMbus devices like the AM2315 temp/humidity sensor.   The sensor has a fixed address ( one per bus ) and really misbehaves when the clock is stretched by the Pi hardware implementation.   This also allowed use of the i2cdetect program to scan for sensors added hot.  Yes, the mux supports hot insertion of devices, so you can add, scan and use without a reboot.   The one thing to note is that the RESET function exposed by the chip is not used and needs to be tied to the supply voltage (+3.3v on the Pi) through a pull-up resistor.   It would be possible to use the reset line with a spare GPIO if sensor devices causing hung bus conditions is a problem. I have not used the feature.

  • Updated for Pi4

    CarbonCycle09/18/2020 at 03:07 0 comments

    I have updated this overlay to work with the Pi4 and more recent Raspian releases.

  • The virtues of software I2C on the Raspberry Pi

    CarbonCycle10/09/2018 at 22:41 0 comments

    Tested most recently with Raspbian Stretch. This is most useful for devices that are SMbus compliant ( <=100khz clock ) or have fixed addresses that prevent using more than one on the same bus. This is also a good work-around on the Raspberry Pi for the clock-stretching bug that some devices (AM2315 for instance) misbehave with the clock artifacts from the hardware ARM I2c bus. Control the clock speed by modifying the delay ( i2c_gpio_delay_us ). 

    This software bus provides some additional SMbus support.

    Details on SMbus functions

    ### I2C-ARM
            pi@raspberry:~ $ sudo i2cdetect -F 1
            Functionalities implemented by /dev/i2c-1:
            I2C                                                yes
            SMBus Quick Command              yes
            SMBus Send Byte                         yes
            SMBus Receive Byte                    yes
            SMBus Write Byte                         yes
            SMBus Read Byte                         yes
            SMBus Write Word                        yes
            SMBus Read Word                        yes
            SMBus Process Call                      yes
            SMBus Block Write                        yes
            SMBus Block Read                        no
            SMBus Block Process Call            no
            SMBus PEC                                    yes
            I2C Block Write                              yes
            I2C Block Read                              yes

    ### Software-I2C

             pi@raspberry:~ $ sudo i2cdetect -F 3
             Functionalities implemented by /dev/i2c-3:
             I2C                            ...

    Read more »

View all 2 project logs

  • 1
    How to install the device tree overlay completely including adding a software I2C port
    To Install with the defaults. 
    This is the best method if you don't want to manually edit files.
    Step 1 - Change to /home/pi and Download the install script
    pi@raspberry:~ cd ~
    pi@raspberry:~ wget
  • 2
    Step 2 - Run the install script
    pi@raspberry:~ /bin/bash
  • 3
    Step 3 - Reboot
    After reboot, you should see something like this in dmesg output:
    [    2.168710] systemd-udevd[137]: starting version 215
    [    2.527337] i2c-gpio i2c@0: using pins 23 (SDA) and 24 (SCL)
    [    2.796954] i2c i2c-3: Added multiplexed i2c bus 4
    [    2.799319] i2c i2c-3: Added multiplexed i2c bus 5
    [    2.801690] i2c i2c-3: Added multiplexed i2c bus 6
    [    2.803925] i2c i2c-3: Added multiplexed i2c bus 7
    [    2.808755] i2c i2c-3: Added multiplexed i2c bus 8
    [    2.809316] i2c i2c-3: Added multiplexed i2c bus 9
    [    2.811035] i2c i2c-3: Added multiplexed i2c bus 10
    [    2.812586] i2c i2c-3: Added multiplexed i2c bus 11
    [    2.812604] pca954x 3-0070: registered 8 multiplexed busses for I2C switch pca9548
    This is with the default i2c address of 0x70.  You can pass another address in the declaration:

View all 4 instructions

Enjoy this project?



lenz_kappov wrote 08/27/2021 at 10:02 point

Does anyone know how/if this can be coerced onto a Pico?

  Are you sure? yes | no

jon_eversett wrote 03/18/2021 at 22:11 point

I've recently purchased and TCA8548A mux hat for Rpi 4. When I run it I get 

OSError: [Errno 16] Device or resource busy Any idea how I can fix this please?

  Are you sure? yes | no

CarbonCycle wrote 02/08/2019 at 18:38 point

Faster?  In what way? 

  Are you sure? yes | no

jaco.slabbert wrote 02/06/2019 at 13:21 point

Rather generate additional i2c busses. It is much faster then the muxer

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates