2

Consider a scenario where two (or more) master devices (the NodeMCU's, in this case) share an I2C bus with one (or more) slave device(s), like thus:

enter image description here

Would this be a feasible setup? In other words, could both masters request (not simultaneously) from the slave?

2
  • 4
    Only if you can implement MultiMaster in the ESP8266's (software-based) I2C library.
    – Majenko
    Jul 26, 2019 at 14:16
  • care to elaborate an answer? :)
    – tony gil
    Jul 26, 2019 at 14:17

2 Answers 2

6
+50

The situation you describe is called MultiMaster. It is normally done in hardware using the I2C peripheral in the MCU. However the ESP8266 doesn't have one (or if it does, it's not used), and uses bit-banging to implement I2C in software. If you can add MultiMaster functionality to the ESP8266's Wire library then possibly yes, you could do it.

Another option is to have some other communication channel between the two masters (it could be as simple as a wire with a pullup resistor - see below) so that only one master communicates at a time.

Electrically there is no problem with your setup - I2C is a fully open-drain system (or should be implemented as such - hence the pullup resistors needed on the SCL and SDA pins), so multiple devices can drive the lines at the same time. It's just that if they do you will get data corruption and communication won't happen.


Example: Semaphore Wire Implementation

  1. Connect any two GPIO pins together with a wire. Add a pullup resistor to the wire (10kΩ should suffice).
  2. Both GPIO pins are set to INPUT.

When a master wants to send it first reads the GPIO pin:

  • If it is HIGH then it is "clear to send". So it sets the GPIO to OUTPUT and pulls the line LOW.
  • If it is LOW then it waits for a random amount of time and tries again.

Once communication is finished the master sets the GPIO pin to INPUT again.

13
  • would strict scheduling provide a workaround?
    – tony gil
    Jul 26, 2019 at 14:36
  • 1
    @tonygil Only if you have zero clock drift... Simpler to have a semaphore signal. I have added more detail to my answer.
    – Majenko
    Jul 26, 2019 at 14:37
  • 1
    Outstanding answer. (Voted). What (if any) Arduino boards have the hardware needed to support MultiMaster? And what library/libraries do you need to us in order to take advantage of it?
    – Duncan C
    Jul 26, 2019 at 15:29
  • 1
    @DuncanC The clock signal is only there when the master is communicating. Whichever master is communicating is the one that controls both the IO lines at any moment in time.
    – Majenko
    Jul 26, 2019 at 15:48
  • 1
    No, that would be a pull-down. You want a pull-up. Connect it to +3.3V.
    – Majenko
    Jul 29, 2019 at 18:11
1

Changed diagram after comments and accepted answer:

enter image description here

2
  • 1
    Yes, except that the upper and lower ground rails on the breadboard aren't connected except via the internals of the Arduino.
    – Majenko
    Jul 30, 2019 at 9:09
  • adapted to reflect your comment above
    – tony gil
    Jul 30, 2019 at 23:29

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .