darryletter
Posts: 76
Joined: Thu Jan 01, 2015 8:52 pm
Location: KC,MO

multiple I2C devices

Thu Jan 01, 2015 9:03 pm

I am new to the raspberry pi and i have a "B +".

So my question is it possible to hook up more then one device at a time. Can the I2C bus communicate to more then one device. what i was hoping to do was to breakout the I2C lines and hook more then one device and the same time
Life After 60 sucks

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5720
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: multiple I2C devices

Thu Jan 01, 2015 11:39 pm

darryletter wrote:I am new to the raspberry pi and i have a "B +".
So my question is it possible to hook up more then one device at a time. Can the I2C bus communicate to more then one device. what i was hoping to do was to breakout the I2C lines and hook more then one device and the same time
Quick answer is "Yes" - so long as each device has been setup to use a unique I2C address**.
Trev.
** FWIW, a fairly simple example for two different I2C "LCD display backpacks" can be seen here:
http://www.cpmspectrepi.webspace.virgin ... _Interface
and an example using two different I2C port-expander chips is here (revised circuit):
http://www.cpmspectrepi.webspace.virgin ... oFull.html
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
sav25
Authorised Reseller
Authorised Reseller
Posts: 386
Joined: Thu Aug 30, 2012 7:18 pm
Location: Southend-on-Sea, Essex, UK
Contact: Website Twitter

Re: multiple I2C devices

Thu Jan 01, 2015 11:43 pm

Yeah something like the popular MCP23017 for example, has 3 address pins that need to be connected to 3.3v or GND:

I've been making some notes for my notebook and drew this the other day actually (well, on the PC):

Image

So here, pins 15,16 and 17 can be wired different ways to give different addresses, allowing you to address different chips and I/O in your code.

I think there's a maximum though, I think something like 8 or 9 using this method, but I also heard you can add hundreds if you port expanders to control port expanders - but that's going to be very pointless for 99.9% of people!

Rich

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: multiple I2C devices

Thu Jan 01, 2015 11:46 pm

sav25 wrote:I think there's a maximum though, I think something like 8 or 9 using this method, ...
Come on Rich think in BINARY. How many things can you count with THREE bits?

000
001
010
011
100
101
110
& 111.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
sav25
Authorised Reseller
Authorised Reseller
Posts: 386
Joined: Thu Aug 30, 2012 7:18 pm
Location: Southend-on-Sea, Essex, UK
Contact: Website Twitter

Re: multiple I2C devices

Thu Jan 01, 2015 11:48 pm

Yep - 8 it is then. I was just testing you :lol:

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: multiple I2C devices

Thu Jan 01, 2015 11:49 pm

sav25 wrote:Yep - 8 it is then. I was just testing you :lol:
I make it zero to seven, because I'm a systems programmer.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: multiple I2C devices

Fri Jan 02, 2015 9:50 am

There are some I2c devices that have tri-state address pins. That means that with two address pins, 9 addresses are available

L=low H=high O=open
OO
OL
OH
LO
LL
LH
HO
HL
HH

I remember seeing quad state address pins somewhere. Low, High, Open, and Connected to SCL.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

darryletter
Posts: 76
Joined: Thu Jan 01, 2015 8:52 pm
Location: KC,MO

Re: multiple I2C devices

Fri Jan 02, 2015 5:02 pm

ok there is a way to do this thanks everyone but just a few more questions and then on to another post. How well does this work does the comm speed slow down? is there any failure issues? can the rpi b+ do input and output from 2 or more devices at the same time with the other I2C devices or is it one device at a time?
Life After 60 sucks

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: multiple I2C devices

Fri Jan 02, 2015 5:32 pm

With a single processor your machine can only ever do one thing at a time. The appearance that your Linux system is multitasking / multi-threading is just a smoke & mirrors illusion generated by a clever dispatcher in the kernel. The CPU has one core so at any instant it is running one instruction (assuming we're not farming any of the work out to the GPU).

So the answer to 'Can you read and write an I2C bus "in parallel"?' is 'Yes, you can.' because any read operation is going to complete before the next write operation starts (unless it's interruptible which is unlikely to be allowed by the kernel driver for any time critical I/O operation).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: multiple I2C devices

Mon Jan 05, 2015 3:57 pm

I think that a few google searches might help.

"Unix time sharing system"
"Unix Context switch"
"Unix Internals"
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

darryletter
Posts: 76
Joined: Thu Jan 01, 2015 8:52 pm
Location: KC,MO

Re: multiple I2C devices

Mon Jan 05, 2015 6:25 pm

Richard-TX wrote:I think that a few google searches might help.

"Unix time sharing system"
"Unix Context switch"
"Unix Internals"
thank you will start the search shorty and see what i can find out.
Life After 60 sucks

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26828
Joined: Sat Jul 30, 2011 7:41 pm

Re: multiple I2C devices

Mon Jan 05, 2015 10:34 pm

darryletter wrote:ok there is a way to do this thanks everyone but just a few more questions and then on to another post. How well does this work does the comm speed slow down? is there any failure issues? can the rpi b+ do input and output from 2 or more devices at the same time with the other I2C devices or is it one device at a time?
I2C is pretty slow, the Pi should have no problems handling lots of devices at the 'same' time.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: multiple I2C devices

Tue Jan 06, 2015 6:12 am

I typically use 4 MCP23017 chips on a Rpi for a total of 64 GPIOs. I have wired 16 of them to a single PI as the Rpi has two I2C buses. 16*16=256 GPIOs.

To really reduce latency, raising the I2C bus speed helps. The MCP23017 is capable of 1.7 Mhz I2C bus speed. I have run the I2c Bus speed up to 1.2 Mhz on the Rpi.

If a higher speed is needed then the SPI version (MCP23S17) is capable of 10 Mhz. You can still use up to 16 of them on a Rpi.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

darryletter
Posts: 76
Joined: Thu Jan 01, 2015 8:52 pm
Location: KC,MO

Re: multiple I2C devices

Wed Jan 07, 2015 8:27 pm

ok great news got a mcp23017 in a couple of days ago and have it working great right now i love this chip.

I do have just one concern that might not really be a problem though.

i my python script i set all the i/o pins to output and then pin 3 to input. this works fine for the test leds i have on pin 0 & 1. I just set the output to 0 and they go off and set the output to 1 and they come on. But when i put my 8 relays on pins 8 thru 15 i have to set them to output 1 to turn off and output 0 to turn on.

Have i got something setup or wired wrong or is this even something i should be worried about.

thanks for all the great help here
Life After 60 sucks

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: multiple I2C devices

Wed Jan 07, 2015 9:28 pm

That's a common problem with some relays. You can either live with it or invert the signal with a couple of transistors wired as a NOT gate.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

darryletter
Posts: 76
Joined: Thu Jan 01, 2015 8:52 pm
Location: KC,MO

Re: multiple I2C devices

Wed Jan 07, 2015 11:57 pm

if it won't hurt anything i can live with it
Life After 60 sucks

Return to “Interfacing (DSI, CSI, I2C, etc.)”