I2C/Wire speed and cable length

I've searching a bit but have not found any answer.

Do the I2C/Wire speed affect the maximum cable length? Can I lower the speed to obtain longer cable length?

I'm working with a project where I need to connect several boxes to an Mega2560 I2C bus and I'm worried about the total length.

One box will contain the controller while three other boxes contain devices that need the I2C bus. Lowering the is no problem since I do not need any fast connection between the boxes.

Do the I2C/Wire speed affect the maximum cable length? Can I lower the speed to obtain longer cable length?

Yes and yes.

I'm working with a project where I need to connect several boxes to an Mega2560 I2C bus and I'm worried about the total length.

Usually, if you have to think about wire length the choice of I2C as the communication method is wrong. I2C was designed to be used in-PCB, so the total bus length shouldn't exceed about 50cm using standard speeds. If you lower the speed considerably you can make the bus longer but you shouldn't go over about 1-2m even at low speeds.

One box will contain the controller while three other boxes contain devices that need the I2C bus. Lowering the is no problem since I do not need any fast connection between the boxes.

I don't know what "box" means in that context but I strongly suggest to use another communication standard to connect the boxes (p.e. RS-485, Ethernet, etc.)

I know that I2C is suppose to work on a PCB only, but my experience is that it work OK with cables unless they get too long (when strange things might happen).

RS232 or any other way to communicate is not possible. It's a PCF8574 chip inside the other boxes that the controller need to communicate with.

I will be using non-twisted shield cables and each cable will be between 20-40 cm.

How low can I the speed? Can I choose any number I like or is the speed value given in steps?

RS232 or any other way to communicate is not possible. It's a PCF8574 chip inside the other boxes that the controller need to communicate with.

Nevertheless this is possible, you just have to spend each box a MCU.

Can I choose any number I like or is the speed value given in steps?

You can choose any number, the library will choose the closest step available.

How low can I the speed?

That depends on the devices on the bus. Most will work down to a few hundred bits per second.

But keep in mind that an I2C bus over cables will have problems in a (electrically) noisy environment. You can try that setup but be prepared that it will be quite unreliable.

thehardwareman:
I know that I2C is suppose to work on a PCB only

Thank you.

The signal of a I2C bus is weak, since the high level is with a pullup resistor.

The function Wire.setClock() can set the speed of the Master. The Slave just follows the clock of the Master.
You can enter 50000 up to 400000 as parameter. I think the function fails when trying to go lower.

However that is not the problem. The main problem is the crosstalk between SDA and SCL.
The receiving PCF8574 has fast hardware for the I2C bus. There is nothing you can do in the Master to fix that. So the PCF8574 will accept every noise and glitch and crosstalk spike there is, just like a cooky monster eating cookies.

You could add a LC or RC filter at the Master and all Slaves and write your own code to get a lower clock.
How many wires are in the cable and how are they organized ? Perhaps you can keep SDA and SCL away from each other and both between GND wires. The maximum sink current for I2C is 3mA, so when you select pullup resistors to get close to 3mA, there is less noise. Maybe it is possible. The Arduino IDE 1.8.13 has low-level timeouts for the I2C bus. Use those to recover from a bus error.

If you go that route, then you can also hire a few persons and use a Chappe optical telegraph: Telegraphy - Wikipedia.

This MegaPoints YouTube Video shows I2C working over 100m of cable. The MegaPoints boards use Atmega microprocessors.

...R My Arduino to Arduino I2C Tutorial

Robin2: MegaPoints YouTube Video shows I2C working over 100m of cable.

I think I have seen the schematics. The hardware has nothing special if I remember it correctly. Is there a microcontroller on both sides ? That has the possibility to fix problems. I have never seen a sketch and I don't know how the signals are organized in that cable.

By the way, outside in a garden (and hopefully without power lines in the ground) is a very clean environment.

Dear mister MegaPoints, tell us your secret.

The cable I will use has four wires - SDA, SCL, INT (from the PCFs to the MCU) and GND. It will be a non-twisted shielded cable.

I also saw that video. However it does not show what's inside those boxes and which speed he used. I know there exist chips that can be used to extend the length of a I2C bus.

That is not good. You can not put enough GND between the other wires. You can try, it is only a short distance. If you look at the wires in the cable, then put SDA and SCL on opposite wires.

Let the MultiSpeedI2CScanner run for a few hours. If it is stable at 400 kHz, then 100 kHz should be safe. https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner

So I should only have SDA, SCL and INT?

I'll try the multispeed scanner when things are mounted together. Thanks for that info.

Koepel: Dear mister MegaPoints, tell us your secret.

I don't suspect a secret.

Most of the applications for MegaPoints modules will involve distances of less then 10 metres.

...R

@Robin2, it is possible to make a bad I2C with only 2 meters of flat ribbon cable where SDA is next to SCL and almost no pullup and no GND nearby to dampen it. It is really easy to do it wrong.

When the signals are next to GND, then a good pullup with a low clock speed is needed. I'm not sure but I think the hardware of the ATmega328P supports very low clock speeds, but the Wire.setClock() does not support very low clock speeds. Maybe the MegaPoints uses 10 kHz clock speed. The MegaPoints modules that I see don't send a lot of data over the I2C bus. The ATmega328P has already a slew-rate-limiter and a filter for the I2C bus in hardware, so a lot is possible as long as the noise and crossover spikes can be kept low.

@thehardwareman, I want more GND in that cable. The extra capacitance to GND is something that can be solved with a lower clock speed. If you have three signal wires, then I would like three GND wires as well. But it depends on how the wires are organized in the cable. Even if it is not twisted pair, a machine made it and they are at a certain position in the cable.

Koepel: @Robin2, it is possible to make a bad I2C with only 2 meters of flat ribbon cable where SDA is next to SCL and almost no pullup and no GND nearby to dampen it. It is really easy to do it wrong.

We would not be having this discussion if the communication was using Serial. At the level of moving bits along a wire I just can't see what is different between an Arduino's TTL Serial and its I2C.

...R

Koepel: @thehardwareman, I want more GND in that cable. The extra capacitance to GND is something that can be solved with a lower clock speed. If you have three signal wires, then I would like three GND wires as well. But it depends on how the wires are organized in the cable. Even if it is not twisted pair, a machine made it and they are at a certain position in the cable.

More GND? Then I have misunderstood the previous message. I can find a cable with more wires so that I can add more GND.

I'm not sure what the "type of cable" is called, but it is just a shield covered with plastic and the individual wires are non-twisted.

For a project of my own I have just created an I2C cable that is about 2m long. I used two twisted pairs (taken out of a piece of ethernet cable). The examples from my Arduino to Arduino I2C Tutorial work fine with it using a Mega and an Uno.

...R

This MegaPoints YouTube Video shows I2C working over 100m of cable. The MegaPoints boards use Atmega microprocessors.

According to the website these controllers use a CAN bus and not I2C. So the 100m make more sense. I doubt that you get I2C running over 100m even at 10kHz in that clean environment.

pylon: According to the website these controllers use a CAN bus and not I2C.

I don't know where that comes from. They have a page on their website that shows how to use an Arduino with I2C to interface with their controllers and I tried that and it worked. The Megapoints controllers are part of a model railway club project so I don't have access to them now.

...R

If they use I2C, which one is the master? What clock speed do you have to set if the Arduino is the master?

That is too much confusion, let's investigate this :P

The Megapoints Controllers use the I2C bus as a "network". There is a CAN board, but the MegaPoints Controllers use I2C. In the video he uses 4k7 pullup resistors.

In his document he mentions that with a driver a cable length of "a kilometre" is possible. He writes that on page 6 of his Network Guide. He also writes there that those bus extenders are "fit for our purpose". That indicates that he definitely uses them.

For the 1km claim, he refers to the NXP Design note. In that document on page 5, slide 10, it says that 2 meters is the maximum length for a normal 400kHz I2C bus. On page 25, slide 68 is the 1km mentioned with 31kHz and I2C bus extenders such as the P82B715 and the P82B96.

I can not see a I2C bus extenders on the photo of the controllers :o So I assume that the SDA and SCL of the ATmega328P chip are used. The long cable is achieved with:

  • 4k7 pullup resistors. I don't know if the 10k pullup on the Controllers is enabled with jumpers.
  • I think he has GND between the SDA and SCL, that is very good.
  • Perhaps a lower I2C clock.
  • The garden is a very clean environment.
  • Using only ATmega328P with their ridiculous high sink current up to 30 or 40mA. Some sensors can only sink 3mA, so he has no bare sensors in the network.

With or without those I2C bus extenders, I think it is possible. So Mister MegaPoints has no secret after all, but I would like to see the software for the I2C bus ;)

Koepel: That is too much confusion, let's investigate this :P

The Megapoints Controllers use the I2C bus as a "network". There is a CAN board, but the MegaPoints Controllers use I2C. In the video he uses 4k7 pullup resistors.

In his document he mentions that with a driver a cable length of "a kilometre" is possible.

This is just a distraction. The OP has no need for a 100 metre connection, never mind 1000 metres.

The only point I was trying to make is that TTL serial data is transmitted over a wire by toggling an I/O pin HIGH and LOW. And I2C works exactly the same way so the achievable distances should be similar.

...R