Go Down

Topic: I2C Distance (Read 23390 times) previous topic - next topic

kaor

Tested connection via CAT5 2 pairs over 200 meters.
No external parts but wires (used internal pullups).
Works almost nice, just set SCL speed to 500hz.
I'm going to add just capacitors to remove SCL spikes from SDA signal but during test I ran into one problem.
In some circumstances, I2C get stuck. I'm using standard Wire library.
Code hangs in twi_readFrom() function waiting for TWI_MRX changed value.
I tried another custom I2C library (by Wayne Truchsess), hangs anyway.
I met the same problem year ago and as I see there is no library guaranteeing 100% stable work.
I don't mean 100%  clear data via I2C, I mean not hanging code even when there is error in I2C transmission.
?????? ?????? ?????, ??? ??? - ??????????!

Headroom

Just came across another very good source of information in respect of what cables to use etc.

http://ics.nxp.com/support/documents/interface/pdf/an444.pdf

Very recommended reading for anyone dealing with I2C over longer distances
http://trippylighting.com

http://ledshield.wordpress.com/

Headroom


Tested connection via CAT5 2 pairs over 200 meters.
No external parts but wires (used internal pullups).
Works almost nice, just set SCL speed to 500hz.
I'm going to add just capacitors to remove SCL spikes from SDA signal but during test I ran into one problem.
In some circumstances, I2C get stuck. I'm using standard Wire library.
Code hangs in twi_readFrom() function waiting for TWI_MRX changed value.
I tried another custom I2C library (by Wayne Truchsess), hangs anyway.
I met the same problem year ago and as I see there is no library guaranteeing 100% stable work.
I don't mean 100%  clear data via I2C, I mean not hanging code even when there is error in I2C transmission.



Before you start arbitrarily adding components like capacitors I'd suggest you start reading some background info on I2C. As you've already downloaded Wayne's I2C library, perhaps you found the info on the same website referring to the dimensioning of Pullup resistors ?

http://dsscircuits.com/articles/effects-of-varying-i2c-pull-up-resistors.html

From your description it appears that the I2C bus simply locks up. I've had the same problem in one of my projects (10 devices over about 5 meters). Wayne's library allows to set a timeout value after which the I2C bus is restarted/reset, which is something the standard Arduino TwoWire library does not offer. If the data you are transferring is not critical that may help your situation. However, that limits the effects but does nothing to deal with the root cause of the problem, which is likely system capacitance and noise. The Application Note I linked to in my post above contains some valuable information that does not only apply when using the I2C bus extenders ;-)
http://trippylighting.com

http://ledshield.wordpress.com/

kaor

I'm talking about software library and reply is about hardware and same thing again and again.
I'm explained my goal in another topics and do not want to offtopic this one.
If I have some free time, I'll try to find out better solution for stabilizing I2C library.
BTW Wayne's timeout doesn't helps, I going to figure figure out why.
?????? ?????? ?????, ??? ??? - ??????????!

Headroom

Of course the hardware is where the problem is, because one those length of cable your signal degrades considerably!
The internal pull-up resistors are less than ideally dimensioned for I2C bus operation. Right the first measurement in this article explains that:
http://dsscircuits.com/articles/effects-of-varying-i2c-pull-up-resistors.html

Then you are using CAT5 cable with a nominal capacitance of 50pf/m. The I2C bus capacitance is supposed to not exceed 400pf and that does not only refer to the cable but the entire system. Past 5m of cable length your system capacitance will degrade your signal to a point where the I2C bus simply will not function reliably. Lowering the operation frequency of the I2C bus will only remedy that to a degree.

In the app note I linked to above (here it is again : http://ics.nxp.com/support/documents/interface/pdf/an444.pdf) they tested several different cables that have a significantly lower nominal capacitance than CAT5 cable. They specifically did not use twisted pair as the I2C signal is bidirectional/unbalanced.

If you really need to go in the range of 200m with CAT5 cable (or even "just" 50) you should use e.g a PCAA9600, which is specifically designed to separate the bidirectional I2C-bus signals into unidirectional TX and RX signals and enables the SDA and SCL signals to be transmitted via balanced transmission lines.

Perhaps you start using an oscilloscope so you have positive verification that your design works ;-)
http://trippylighting.com

http://ledshield.wordpress.com/

kaor

I'm setting SCL low frequency, yesterday I was playing with SCL prescaler = 64 and TWBR from 15 to 17.
TWBR 17 (rate ~ 7khz)  gives very stable results, when 15 (~ 8khz) fails very often.
I've modified little bit I2C library and all timeouts are handled without hanging.

Now about capacitors, atmega supports spikes shorter than 50 ns but due to long line higher capacitance this feature goes useless, therefore you need some external spike suppressor.
Everyone suggest to use pullup resistor but! You should consider line resistance, in my case 200 meter wire has about 200 ohm resistance, if you low resistance pullup, you lower data signal too, no amplifier can help.
But there is very easy way out, use pullups with capacitors, considering low SCL rate. Low SCL rate allows to use capacitor that "blurs" signal and distinguishes spikes. You should use capacitors according wire resistance+pullup resistance and SCL rate considering t=rc formula.
I think this way you can extend I2C distance very easy.
?????? ?????? ?????, ??? ??? - ??????????!

Headroom

Granted, thar may work but obviously at a very substantial loss of data rate. A PCA9600d costs $2.08 at mouser and is hardly an investment and provides you with a rock solid solution.

You experimental way, however, has something defiant. I like it ;-)
http://trippylighting.com

http://ledshield.wordpress.com/

kaor

#22
Sep 18, 2012, 01:38 pm Last Edit: Sep 18, 2012, 01:47 pm by kaor Reason: 1
As I mentioned, in some circumstances no amplifier can help.
PCA9600d is just amplifier (RX/TX splitting doesn't help in this case).
Amplifiers allow you to use low resistance pullups (that couldn't be driven by atmega ports), for example 400 ohm pullups, but if your connection wire has high resistance, something like 200 ohm or higher, than you cant use low resistance pullups, this will cause data loss.
You can be shure, no PCA9600d will help you in this case!
you need to lower SCL rate and use capacitors to smooth signal and distinguish spikes on SDA line caused by SCL line (that is main problem of long distance I2C).
I'll put scopes later.

?????? ?????? ?????, ??? ??? - ??????????!

kaor

Some update.
I simulated twisted pair and took some parameters from http://alignment.hep.brandeis.edu/Lab/XLine/XLine.html.
Twisted pair seems like this scheme


If we start simulation without capacitors (switch is off), than we have

Wee see wide spike on SDA and this spike won't be distinguished via atmega spike filter.

Now we turn on the switch and we see another scope

In this case spike amplitude is shorter and does'n go to logical 0.
?????? ?????? ?????, ??? ??? - ??????????!

Headroom

I don't disagree with your assessment. Very educational!

The technical documentation I have provided links to provides very detailed instructions on how to overcome problems such as yours.

The Arduiuno drives the I2C bus with 5V. Given the resistance and capacitance of 200m of CAT5 Ethernet cable you can obviously approach the problem in different ways. Reducing the bus frequency is one, however, at a substantial loss of data rate.
If you want to maintain a higher data rate, the other approach is to drive the bus at a higher voltage, which in case of an Arduino requires additional hardware. For example a PCA9600 that is able to deal with the higher overall capacitance - as it is specifically designed to worh with twisted pair cable - and the higher line resistance by being able to operate up to 15V.

http://trippylighting.com

http://ledshield.wordpress.com/

kaor

#25
Sep 23, 2012, 08:23 pm Last Edit: Sep 24, 2012, 08:26 am by kaor Reason: 1
Headroom, yes, in my project I can sacrifice data rate to get more stability and less hardware.
?????? ?????? ?????, ??? ??? - ??????????!

Headroom

Cool. Great discussion BTW. I believe that there are other projects that could benefit from your work. Thanks for posting it!
http://trippylighting.com

http://ledshield.wordpress.com/

Go Up