0

Connecting multiple I2c devices to a Raspberry Pi
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Wed Nov 09, 2016 4:52 pm

Hi,
I want my Raspberry PI to use I2C to drive 1x HT16K33; 4 x MCP23017 and an i2c LCD backpack driving a 4x20 display
my initial experiments show that after connecting 3 devices at either 5v or 3v it will not reliably recognise any more devices.
I think I need to drive this lot at 5v to get reliable results and drive the LCD
I have ordered a 4-channel I2C-safe Bi-directional Logic Level Converter - BSS138 as recommended in the guide to HT16K33

Will this fix the problem?
What is the most reliable way to connect these components or should I use a 5v Arduino instead?
Thanks for any help

Max

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Wed Nov 09, 2016 5:04 pm

If these are all boards from us, they will all have 10K pullup resistors on the i2c bus lines. You can usually put 3 or 4 on the bus before the combined pullups are too strong. With 6 sets of pulllups on the bus, your Pi will have a hard time driving it. I would recommend removing the pullups from a few of your devices.

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Wed Nov 09, 2016 5:46 pm

The HT16K33; and i2c LCD backpack are yours but the 4 x MCP23017 (like yours) are generic ICs with internal pull ups registers.
Would an Arduino be better than a PI to drive these? Some variants like the Teensy even have multiple I2C buses

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Wed Nov 09, 2016 6:13 pm

The Arduino can generally handle a bit more on the bus than the Pi. But 6 devices is pushing it. Aside from removing redundant pullups, you could use a processor with multiple buses, or you could use a multiplexer to get 8 buses on any processor:
https://www.adafruit.com/products/2717? ... DQod4MEGgQ

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Thu Nov 10, 2016 7:52 am

Ok I have ordered some of those multiplexers( I know I only need one, but I like to have spares). A couple of questions:
1)Is the electrical load also multiplexed? I.e. Does the Pi now think that it is supporting just one device electrically?
2) the libraries look good but are for Arduino, is there a Pi version?

Thanks a lot for your help
Max

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Thu Nov 10, 2016 8:21 am

Power and ground for all the buses is shared. Only the SDA and SCL lines are multiplexed. So at any time, the Pi will only see the load of the pullups on the active channel.

For an example of using it with the Pi, see this post: viewtopic.php?f=19&t=83918#p424327

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Thu Nov 10, 2016 8:31 am

Excellent! that should do it, thanks.

I have also been asking on the Raspberry PI Forums and apparently:
There is a device tree overlay for the TCA9548 so that the 8 ports are presented as 8 independent I2C buses by the kernel. Saves you writing code for controlling the mux, and keeps the code platform independent. Add "dtoverlay=i2c-mux,pca9548" to /boot/config.txt.

Amazing, I await delivery of my TCA9548 to see if its all that easy.

Max

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Wed Nov 16, 2016 7:52 am

OK, so got the TCA9548 . I have used the RPI device tree and it shows I have lots of new I2c channels so when I do: sudo i2cdetect -y 3
I see UU on 70 (reserved for the mux) and 3f which is my Adafruit 16x2 Character LCD + Keypad.

but can't work out how to modify the example program "char_lcd_plate.py" to write to the correct busnumber (3)
I assume I need to change
lcd = LCD.Adafruit_CharLCDPlate()

So I tried
lcd = LCD.Adafruit_CharLCDPlate(3) and lcd = LCD.Adafruit_CharLCDPlate(busnum=3)

I also found another post suggesting changed Adafruit_CharLCD.py
but it contains lots of classes. I tried a few changes, but It was not clear what to modify.

Any ideas?

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Wed Nov 16, 2016 8:09 am

You shouldn't need to change the library at all. You just need to select the channel on the mux before writing to the display.

Python code to select the multiplexer channel can be found here: https://github.com/kizniche/Mycodo/issu ... -156718451

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Wed Nov 16, 2016 8:29 am

OK, if I use that program I assume I do not use the device tree

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Wed Nov 16, 2016 8:44 am

Not sure of the answer to that. Let me check with my colleagues....

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Re: Connecting multiple I2c devices to a Raspberry Pi

by Maxchess on Wed Nov 16, 2016 9:14 am

OK, just tried it both ways.
You use the program instead of the device tree.
The device tree is the more elegant solution, but it means that you have to amend all your original routines to use the right bus.
This program allows you to leave the bus assignment the same (usually 1) and switches to the right channel, but its much easier to get it to work with existing code.

Thanks

Maxchess
 
Posts: 28
Joined: Tue Aug 20, 2013 12:03 pm

Re: Connecting multiple I2c devices to a Raspberry Pi

by adafruit_support_bill on Wed Nov 16, 2016 9:17 am

Good to hear you have it working. Thanks for the follow-up.

adafruit_support_bill
 
Posts: 74982
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.