maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

I2C problems when adding more than 3 devices

Tue Nov 08, 2016 12:58 pm

Hi,
I am building a chessboard ( http://chess.fortherapy.co.uk/home/a-wo ... -computer/ ) using I2c and when I add more than 3 devices the I2c behaviour becomes erratic.
I have 4 MCP23017's each controling 16 reed switches , adresses 0x21,0x22,0x23,0x24
1 HT16K33 controlling an 8x8 LED matrix. 0x70

I have tested each device individually and they work well. However when I try and connect them all the behaviour becomes erratic.If I use sudo i2cdetect -y 1 repeatedly I see anything from 5 to no devices attached.
I have tried this at 5v & 3v same issue?

(I have built a smaller noughts and crosses board with 3 components, no problems)

I have also noted that if I just add extra wire to the bus (no devices) that also causes the same problems.
I am using 1/0.6mm solid core wire for connections. The wire distance between the RPI and the devices is a max of about 2 feet.

I can connect any three devices and its all fine, once I add a forth it all falls over.
I have nothing else connected.(the diagram on my web page showes resistors but I have not included them as I understand that the I2C pins are protected) I am using a PI3 udated to latest SW

Is the length and type of wire an issue? If I use a low resistance wire, will that help? If so what type?

Regards

Max
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 1:07 pm

i suspect you left the reset pins floating
post schematics or joan would say "a photo"

User avatar
joan
Posts: 14930
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 1:29 pm

Massi wrote:i suspect you left the reset pins floating
post schematics or joan would say "a photo"
I suggest a photo as it shows what was actually connected. A schematic shows the intended connections which are not always carried out.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 1:35 pm

i had to use "and" in place of "or" :)

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 3:17 pm

Hi,
Pin 18, Reset and Pin 9 VDD connected to “+”
Also all devices show correct address when queried individually or in group of 2 or 3. Its just when a 4th is added it goes crazy.
Photo of basic connections attached.
It seems like some sort of noise or interference
Max
Attachments
I2c-cons.jpg
basic I2c Connections
I2c-cons.jpg (44.01 KiB) Viewed 4390 times
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

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

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 6:35 pm

maxchess42 wrote:Hi,
Pin 18, Reset and Pin 9 VDD connected to “+”
...
It seems like some sort of noise or interference
Max
I normally connect pin 18 (Reset) to Vdd via an 8.2k ohm resistor, as shown here:
http://www.cpmspectrepi.uk/raspberry_pi ... #Schematic
Also you have no power supply decoupling capacitors on your breadboard (C2 0.1uF and C3 10uF in my circuit schematic linked to above).
Trev.
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

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 9:29 pm

I will try the capacitors. You diagram shows a non polarized 0.1 uf and a polarized 100 uf. I assume the first one is a ceramic style one? Is that correct?
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Tue Nov 08, 2016 9:41 pm

Also don't have 8.2k resistor to hand, is 10k alright?
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

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

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 6:43 am

maxchess42 wrote:I will try the capacitors. You diagram shows a non polarized 0.1 uf and a polarized 100 uf. I assume the first one is a ceramic style one? Is that correct?
Yes - the 0.1uF acts as a "local spike filter" and the 10uF deals with "current surges" ie. the two main sources of "coupled noise" - standard practice from the days of TTL.
maxchess42 wrote:Also don't have 8.2k resistor to hand, is 10k alright?
Yes (or a 6K8, the value's not too critical, it's just there to ensure that the voltage on the RESET pin rises more slowly than the main power and the device can generate it's internal reset pulse)
Trev.
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

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 11:58 am

Ok, so I have added the capacitors, but not yet the resistors and can still see a maximum of only three devices out of 5.
I then tried it wired up to an Arduino Nano and I can see all devices and they all seem to work. Absolutely identical I just moved the SCL, SDA, GND & 5v from one to the other.

I then tried it on another Pi (a V2 with fresh install of Pixel) and get the same Pi result, ie max of three devices connected.

Later I am going to rewire all the existing 5 devices and add the resistors and try again with both Pi and Arduino and see what happens

Maybe the PI is just to noisy to support lots of devices on i2c?
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

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

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 12:56 pm

maxchess42 wrote:Ok, so I have added the capacitors, but not yet the resistors and can still see a maximum of only three devices out of 5.
I then tried it wired up to an Arduino Nano and I can see all devices and they all seem to work. Absolutely identical I just moved the SCL, SDA, GND & 5v from one to the other.
I then tried it on another Pi (a V2 with fresh install of Pixel) and get the same Pi result, ie max of three devices connected.
Later I am going to rewire all the existing 5 devices and add the resistors and try again with both Pi and Arduino and see what happens
Maybe the PI is just to noisy to support lots of devices on i2c?
Do any of the I2C devices have their own (on-board) pull-up resistors? If so, they need to be removed**. Also not all I2C devices with 5V power will work with the Pi's 3.3V "high level". How long are your connecting wires? Are they "twisted pairs"?
Trev.
**So that >3.3V cannot be seen by the Pi's GPIO's.
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

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 1:23 pm

The 5 devices I am trying to connect are: 4 x MCP23017 1 x Adafruit HT16K33 16×8 LED Matrix Driver.
The MCP23017. The MCP23017 is a packaged IC, it does have internal pull ups that can be set, but that's done later by program. I don't think its possible to remove the resistors.
I have tried the I2C bus with both 3.3 & 5v power, same result.
The wire distance between the RPI and the furthest devices is about 2 feet. I am using 1/0.6mm solid core wire.
I have successfully built systems with two MCP23017 and 1 HT16K33 with no problems.

I appreciate all your help, I am just wondering if I should use an Arduino for all the physical computing and then send results serially to the PI. I did that on my first chess board, but hoped I could get a simpler Pi only design with I2c. I will need to connect a total of 7 devices in the final project.

Max
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8876
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 2:35 pm

Potentially the I2C pullups aren't strong enough.
But I'd first ensure you're within the voltage tolerance of the input pins on these devices.
For the HT16K33 the datasheetappears to say 0.7VDD, or 3.5V if on 5V. Fail.
For the 23017 the datasheet says VIH is 0.8VDD, or4V if you're on a 5V supply rail. Fail.

(a) Run the chips off 3.3V or you're really pushing your luck.
(b) Measure what the high voltage is on the I2C bus with your multiple devices connected. Is it being dragged down below acceptable levels?
The I2C pullups are 1k8 on the PI, which is typically good enough for a fair few I2C devices, but it may be that you need to add another 3k3 or 2k7 resistor in parallel to make an effective pull up of around the 1k0 mark.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 3:55 pm

are you able to get all the four MCPs working?
i know them and do not know the other chip, but i only have 2 together :)
Btw, for sure it's not something related to the number of devices (i have a tenth of i2c devices connected with no problem), but you can try adding a i2c multiplexer (ie a PCA9548) to split capacitance of i2c bus on different branches..

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Wed Nov 09, 2016 4:41 pm

Ok I will try some of these ideas.
Wnen you say : "add another 3k3 or 2k7 resistor in parallel to make an effective pull up of around the 1k0 mark."
where do these resistors go. Is there one each from the power line and SCL/SDA ?

Also I wonder whether this level shifter might help:
https://www.adafruit.com/products/757

Regards
Max
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8876
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 10:18 am

I suggest you read up a little bit about how I2C works at an electrical level.
The basics are that SDA and SCL are pulled high by resistors (one per line) and devices can only pull the line low. If the devices don't see a high enough voltage via the resistors to detect the line as high, then you won't get anything.
Pull up resistors are going from SDA and SCL to 3.3V. Do NOT connect them to 5V unless you want to buy a new Pi.

Get a voltmeter on the lines when idle but connected to all your devices and see what voltage it reads. If it is below 3.0V then you have a problem.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 1:11 pm

OK thanks, I will do as you suggest and see what happens, however the impression I am getting is that trying to run multiple 5v devices from the PI is unlikely to work and that running 4 or more I2c devices at 3.3v requires careful monitoring and planning. I will try to get this working as a learning exercise keeping a careful eye on the voltage.

However on a more practical note, two of my 5 or 6 devices need to run at 5v volts as they are driving, first an LED matrix and second an Character LCD display.

Therefore to be careful I intend to connect the Pi I2C at 3v to a level shifter and then to connect that to a TCA9548A I2C Multiplexer which I will run at 5v. This then gives me up to 8 seperate I2c channels.
My understanding is that the TCA9548A will place a load of one device so protecting the Pi.
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 1:21 pm

maxchess42 wrote:Therefore to be careful I intend to connect the Pi I2C at 3v to a level shifter and then to connect that to a TCA9548A I2C Multiplexer which I will run at 5v. This then gives me up to 8 seperate I2c channels.
You do not need to "use" the multiplexer (so use the virtual 8 channels).
You can keep all channels active together.
The advantage is that no channel has pullups, so you can have some channels pulled up to 5V and other pulled up to 3.3V. That's good.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8876
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 1:31 pm

You don't need the level shifter. The TCA9548A supports differing pull up voltages between all the busses - see page 18 of the datasheet

Do note from https://learn.adafruit.com/adafruit-tca ... ut/pinouts
These pins do not have any pullups installed, so if you are using a chip or breakout without i2c pullups be sure to add them! Nicely, you can have Vin be 3.3V and have these pins pulled up to 5V (that is, they are 5V compliant)
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.

Running any 5V devices from the Pi relies TOTALLY on the electrical spec of those devices. VIH (Voltage Input High) is the critical value and needs to be <=3.3V, otherwise you're relying on the individual devices and how they perform when operated outside of the specification.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

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

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 2:28 pm

6by9 wrote: ...
Running any 5V devices from the Pi relies TOTALLY on the electrical spec of those devices. VIH (Voltage Input High) is the critical value and needs to be <=3.3V, otherwise you're relying on the individual devices and how they perform when operated outside of the specification.
+1
For example an MCP23017 (as used in "ye olde" "Slice of Pi" board**) is O.K, as is its single port equivalent, MCP23008 (but may be not multiple devices - I tend to use only "one at a time") but the similar, simpler, PCF8574 devices (often used as LCD "backpacks"****) are known to be "finicky" - one work around for them (suggested by @mahjongg and others) is to reduce their supply voltage with a diode in series with the 5V supply to ~4.4V. This is something I'm trying out at the moment - I'll be posting more details on my website eventually.
Trev.
** http://www.cpmspectrepi.uk/raspberry_pi ... OfPio.html
**** http://www.cpmspectrepi.uk/raspberry_pi ... D_Backpack
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

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 8:22 pm

I just want to make sure I understand what to do, at the risk of exposing my ignorance.

So I connect the Mux and run either 3.3v or 5v to my devices as appropriate
and then I
Add "dtoverlay=i2c-mux,pca9548" to /boot/config.txt

and you say that
the 8 ports are presented as 8 independent I2C buses by the kernel. Saves you writing code for controlling the mux

Which sounds like magic, now for the stupid question. How does the Mux know which of the 8 ports are mapped to which device address without some form of mapping statement?


OK, Just done a bit more reading in other posts, but leaving the above reply as it might be helpful to others searching.

In the Device Tree thread:
viewtopic.php?f=44&t=162256&p=1050052&h ... 8#p1050052

it says

The 8 ports show up as /dev/i2c-3 to /dev/i2c-10, as they are the first 8 undefined bus numbers.

and that you can see the assignments by typing:
dmesg|grep pca

Is that right?



Yours in awe
Max (really from Guildford unlike Ford Prefect)
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

User avatar
joan
Posts: 14930
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 10:33 pm

maxchess42 wrote: ...
The 8 ports show up as /dev/i2c-3 to /dev/i2c-10, as they are the first 8 undefined bus numbers.
...
You are correct.

I remain a dissenting voice. I wouldn't bother using device tree, I'd just talk directly to the multiplexor, especially if all you are using it for is a level shifter and the devices you are using have different I2C addresses.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: I2C problems when adding more than 3 devices

Thu Nov 10, 2016 11:20 pm

joan wrote:
maxchess42 wrote: ...
The 8 ports show up as /dev/i2c-3 to /dev/i2c-10, as they are the first 8 undefined bus numbers.
...
You are correct.

I remain a dissenting voice. I wouldn't bother using device tree, I'd just talk directly to the multiplexor, especially if all you are using it for is a level shifter and the devices you are using have different I2C addresses.
as said above, the device tree approach helps (a lot) if you use it as a multiplexer, so if you have devices with same id that you want to use together.
Otherwise, it just slows down your bus.
Consider that for every device call, you'll have to add another i2c call to select the channel.
If you simply connect it, you just need to write a 0b11111111 to the multiplexer to have all 8 channels enabled and ready for your devices (3v or 5v, it depends on where you pull the lines)

the device tree approach does not allow you to use the PCA9548 with multiple channels active at once (while you can obtain this result by hand)

maxchess42
Posts: 14
Joined: Thu May 23, 2013 1:11 pm
Location: Guildford, UK
Contact: Website

Re: I2C problems when adding more than 3 devices

Fri Dec 02, 2016 7:23 pm

massi,
you say you just need to write a 0b11111111 to the multiplexer to have all 8 channels enabled

Under the device tree method, I would get 8 new buses and just use the right one per device.
with a statement like: bus = smbus.SMBus(3)

In your method with all channels open how do I direct a call to the correct channel.
I assume the Bus is always the Mux bus, which is 1

My existing statements are of the form:
bus.write_byte_data(DEVICE_address,register,0xFF)
and
bus.read_byte_data(DEVICE_address,register)
and go through the default channel.

How do I modify this to choose one of the 8 channels now open.
Pi A's, Bs, 2s, 3s & zeros 's in Birdboxes, Chess Sets, games, RuneAudio servers Arduinos everywhere else

See: http://www.chess.fortherapy.co.uk\

User avatar
joan
Posts: 14930
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problems when adding more than 3 devices

Fri Dec 02, 2016 7:55 pm

maxchess42 wrote:massi,
you say you just need to write a 0b11111111 to the multiplexer to have all 8 channels enabled

Under the device tree method, I would get 8 new buses and just use the right one per device.
with a statement like: bus = smbus.SMBus(3)

In your method with all channels open how do I direct a call to the correct channel.
I assume the Bus is always the Mux bus, which is 1

My existing statements are of the form:
bus.write_byte_data(DEVICE_address,register,0xFF)
and
bus.read_byte_data(DEVICE_address,register)
and go through the default channel.

How do I modify this to choose one of the 8 channels now open.
If all the devices on the multiplexor have different I2C addresses (e.g. 0x20, 0x21, etc.) you just need to talk to the address (e.g. 0x20, 0x21, etc.).

If some of the addresses on the multiplexor have the same address (e.g. 0x20 on channel 0 for device A, 0x20 also on channel 1 for device B) you need to first write a byte to the multiplexor to select the proper device. E.g. write 0b00000001 to select channel 0 (device A), 0b00000010 to select channel 1 (device B) etc.

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