5

I would like to connect multiple I2C devices to my Raspberry Pi. Can I do that? What do i need to take care? Also is there any library which will output I2C on other GPIOs?

  • 1
    You can connect multiple devices to the I2C bus using the same pins. That is why I2C devices have addresses so that communications can share a bus between multiple devices. You can read more about how I2C operates on wikipedia and sparkfun – Steve Robillard Dec 22 '16 at 16:30
11

The whole point of I2C is it is a bus. You can therefore connect multiple I2C devices to the same GPIO provided they have different I2C device addresses.

If the devices share an address which can't be changed you can use an I2C multiplexor chip to typically connect 8 devices to the same I2C bus. You send a command to the multiplexor to specify the device to be addressed.

Recent Pi kernels directly support the following devices with device tree.

Name:   i2c-mux
Info:   Adds support for a number of I2C bus multiplexers on i2c_arm
Load:   dtoverlay=i2c-mux,<param>=<val>
Params: pca9542                 Select the NXP PCA9542 device

        pca9545                 Select the NXP PCA9545 device

        pca9548                 Select the NXP PCA9548 device

        addr                    Change I2C address of the device (default 0x70)

See /boot/overlays/README for details.

Recent Pi kernels directly support software bit banging of I2C on any spare GPIO with device tree.

Name:   i2c-gpio
Info:   Adds support for software i2c controller on gpio pins
Load:   dtoverlay=i2c-gpio,<param>=<val>
Params: i2c_gpio_sda            GPIO used for I2C data (default "23")

        i2c_gpio_scl            GPIO used for I2C clock (default "24")

        i2c_gpio_delay_us       Clock delay in microseconds
                                (default "2" = ~100kHz)

See /boot/overlays/README for details.

In addition there are plenty of software bit banging examples for Raspberry Pi I2C available by doing an internet search.

Note. If you don't use the standard I2C bus (pin 3/5) you will need to add external pull-ups to 3V3 on the GPIO you choose to use. As a guide pins 3/5 have 1k8 pull-ups to 3V3.

  • 1
    Joan's own pgpio library is of course one fine example that permits addition pins to be used for bit-banging I2C. In some usage scenarios it can be useful to have separate buses, I have s setup that I am planning that will use the "built-in" bus for high-speed interfacing with a couple of local devices (an LCD display with an I2C backpack) and a 8-pin I/O device and a separate lower-speed for a "long distance" (it has to be slower) Home Automation bus via some I2C bi-directional drivers to interface with some more 8-bit I/O devices for burglar alarm door/window switches & other things. – SlySven Dec 22 '16 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.