how can I make multi master Arduino device to work with single I2C device? As both the master device are 20 m away from Slave device,

I am using the I2C relay board to connect with 2 Arduino boards and need to connect I2C device and create the two way switching technique from this.

Any suggestions here will be a great help.

  • Why would you want to use a highly priced device with an unsuitable interface with no support when there are hundreds of relay modules which use simple logical levels? – Milliways Dec 7 '18 at 10:50
  • Yes there are TTL relay modules with almost identical form factor and you can buy them for <10% of the price of the OP's board. What is the reason you need I2C, because of the long distance between master and slave? – MichaelT Dec 7 '18 at 11:21
  • Hi MichaelT, Actually I am looking for the solution to use I2C relays for longer distance and setting the priority of master device (Arduinos) to work as per the requirement. – Nate Pulkar Dec 10 '18 at 7:50

What you want is to operate in multi-master mode. This is a supported configuration of I2C - however the Arduino API has no support for it.

It involves each master identifying if a collision has occurred and stopping transmission if it has - to then retry transmission later on.

The Arduino hardware can do it, but the software can't. You would have to bypass the software and control the I2C interface yourself (or locate a better library than the Wire library that supports multi-master).

  • Hey, What if instead of using Arduino, I 'll use ESP8266 or ESP32? Also If I 'll go with Arduino then any IC package will be useful to achieve this kind of mode? – Nate Pulkar Dec 10 '18 at 7:52

The answers by Majenko and chrisl are far too optimistic in my opinion.

A distance of 20 meters is not possible. The I2C bus can be 20 cm or 50 cm.

There is no library that supports a multi-master bus for the Arduino Uno.

You have to change your project. Make one Arduino board the Master, and let the Master retrieve data from the other board and the Master writes to the relay board. You need a bus capable of such a distance or use wireless. The RS-485 bus has no problems with 20 meters. Adafruit has a number of RFM69 modules, those should have no problem with 20 meters.

There are chips that translate the I2C bus signals into differential signals that can be used with a twisted pair cable (for example a cheap Cat5 ethernet cable). The SparkFun Differential I2C Breakout makes it easy to put a I2C device at a longer distance.
(Thanks to @PimV for this addition).

  • 1
    Sparkfun has a "Differential I2C Breakout Board" that enables distances of 100 ft (link. – PimV Dec 7 '18 at 11:43
  • @PimV yes, those are nice modules. They create a rs-485 alike signal over twisted pair wires. When a ethernet cable is used and connected properly then it should have no problem with 20 meters. – Jot Dec 7 '18 at 11:59
  • Hi, The Differential I2C Breakout board seems better Idea. But Could it be possible that instead of using Ethernet Port, I can use 2 cables at longer distances also? – Nate Pulkar Dec 10 '18 at 7:54
  • An simple Cat5 ethernet cable is cheap and is according to certain specifications for a twisted pair cable. The PCA9615 chip on the Sparkfun chips works best with a ethernet cable. Using just any two cables (of two wires each plus GND) is doubtful. When that cable is not shielded, it might pick up too much electric noise. When that cable is shielded, the capacitance to GND might be too high. A lot can happen in a cable of 20 meters. – Jot Dec 10 '18 at 13:07

The Wire library has no complete multi-master support (as I remember). You can join the bus as slave and becoming master by using the master functions for the time of these transmissions (on execution of endTransmission() the master style I2C transmission is started. When it's finished the library will change to slave again). But you will run into problems, when the two masters send at the same time.

The I2C protocol incorporates a technique named bus arbitration. When two masters write to the bus at the same time, the first one to write a 1 on the bus, must retreat from transmission and become slave again. Unfortunately it seems, that the Wire library doesn't implement this.

So you might not run into problems, until the master are sending simultaneously. Depending on what the Arduinos also have to do, you may be able to recover from such a condition by using the Watchdog Timer to reset both masters, when they are stuck there.

Or you try to find a good I2C/TwoWire library, that works for Arduino and has Bus arbitration implemented.

Sidenote: 20m is quite a long distance for I2C. It is designed for short distances, mostly on the same PCB (hence it is called Inter Integrated Circuit, short IIC or I2C). You may not run into issues, but you should keep this in mind. The bus will have a greater capacitance and you may need strong pullup resistors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.