I2C doesn't seem to work correctly

RandallR:
But Nick,
If you have multiple masters and one master wishes to communicate with another master. Isn't the second master going into 'slave' mode?

I have an example of multiple masters here:

See the Atmega328 datasheet page 218:

21.4 Multi-master Bus Systems, Arbitration and Synchronization

The TWI protocol allows bus systems with several masters. Special concerns have been taken in order to ensure that transmissions will proceed as normal, even if two or more masters initiate a transmission at the same time.

(TWI being the same as I2C more or less).


If multiple devices register a slave address then they can be addressed by any other device. So you might have "slaves" 2, 3 and 4. Now device 3 can "call" 2 or device 2 can "call" 3.

At the moment when you do the Wire.endTransmission() call, the device doing that attempts to initiate a transfer (ie. become the master) and if possible, send the data. When complete, it goes back into "slave" status. So effectively every one can be a master and a slave.

This is made possible electrically because each device has the two wires as high impedance (input) until such time as they attempt to become the master.