Raspberry PI Multiple I2C Devices

About: Hobbies: Raspberry PI, Electronics, Python, Drones

Frustrated because you can not use multiples of the same I2C devices in your project. No need to use slow multiplexers. The latest raspbian kernel support the creation of multiple I2C busses using GPIO pins. This solution is super fast.

Step 1: Some Shell Comands

Connect one of your i2c devices while your raspberry pi is of, start your raspberry pi and run

sudo i2cdetect -y 1

You will see a table like in the attach figure. I have attach a BMP280 temp and barometric pressure sensor. The i2c address is 0x76 according to the table. Note this address.

Do this for all your i2c devices.

Step 2: Case One: I2c Devices Have the Same Address

This was always the problematic case. An i2c bus can handle multiple devices, but they should have different i2c addresses. Some i2c devices have jumpers to set other i2c addresses, but many don't. In this case you may use a i2c multiplexer (hardware) to rotate the i2c SDA (Data) and SCL (Clock) or you can create an additional i2c bus or more.

I will create two aditional busses, nl bus 3 and 4

Open the cli and run

cd /boot

sudo nano config.txt

Add the following line of code, preferable in the section where spi and i2c is enabled.

dtoverlay=i2c-gpio,bus=3,i2c_gpio_delay_us=1

This line will create an aditional i2c bus (bus 3) on GPIO 23 as SDA and GPIO 24 as SCL (GPIO 23 and 24 is defaults)

Also add the following line to create i2c bus 4

dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=17,i2c_gpio_scl=27

GPIO 17 will be the SDA and GPIO 27 will be the SCL for i2c bus 4.

Tipe control X to exit.

Shut down your PI, switch it of. Connect your i2c devices to bus 3 (SDA to GPIO 23 and SCL to GPIO 24) and the other to i2c bus 4 (SDA to GPIO 17 and SCL to GPIO 27).

Switch on the pi.

Run:

sudo i2cdetect -l (Lower Case L)

You will now see that i2c bus 3 and 4 is also listed. Also run:

sudo i2cdetect -y 3

sudo i2cdetect -y 4

Now you can use your sensor in your programming language. Remember to specify the correct i2c busses.

Attach is an example for the popular BMP280 Temperature and Pressure sensor. No multiplexer can read 2 BMP280s this fast.

An example of 2 Sensirion SDP 810 sensors is also attach. Again working much faster than the multiplexer I used in the past

I may also add other sensors in future to https://github.com/JJSlabbert/Raspberry_PI_i2C_conficts

Step 3: Case 2: Different I2c Addresses.

Simple. i2c is a bus. A bus purpouse is to communicate with multiple devices. Connect the devices parallel to the same i2c bus. You can use bus one.

Run:

sudo i2cdetect -y 1

You will see the devices listed.

Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • First Time Author

      First Time Author
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    3 Discussions

    0
    None
    enrique023

    Question 5 months ago

    Hello, excellent tutorial, very easy to follow.

    I just have problems to find my other devices, when I write the command:

    i2cdetect -l

    in effect it shows me:

    i2c-3 i2c i2c@3 I2C adapter

    i2c-1 i2c bcm2835 I2C adapter I2C adapter

    i2c-4 i2c i2c@4 I2C adapter

    but when I write the commands:

    sudo i2cdetect -y 3 or

    sudo i2cdetect -y 4

    they run very slowly and don't detect the devices (pressure sensors with same address).

    I tried with BCM GPIO and WiringPi GPIO (23 and 24, 17 and 27) considering the possibility that I'm connecting on the wrong pins but It didn't work.

    Any comment will be helpful,

    1 more answer
    0
    None
    grompotenrique023

    Answer 5 months ago

    Tnx for feedback,

    What pressure sensors are you using?

    If you connect them to bus 1, do they show up when running i2cdetect -y 1.? If they do, try the following, read on.

    The internal i2c bus (bus 1) has a 1.8 kohms pull up resister between 3volt pin and SDA, and also between 3volt and scl. Add those resisters between your 3volt and gpio pins used as SDA and SCL and test again.

    Please let me know if this worked.

    Also read https://www.raspberrypi.org/forums/viewtopic.php?t=64610

    I beliefe that the bmp280 that i used had the pull ups on the sensor itself. My SDP810 (flow sensors) needed extra 10 kohms sensors, even on bus one.

    7AD6477D-922A-4BA5-9591-A7B24155D33D.jpeg
    0
    None
    mjrovai

    6 months ago

    Great! I never tried multiple I2C buses! Thanks