I2C communication from slave to master?

Hi All,

I'm working on a project which involves several Atmeg328's. The master needs to send commands out to the slaves, and the slaves in turn need to send data back to the master.

I figured I2C will be the best protocol as the bus distance is short and only 2 wires are needed. I have no problems with the master sending out commands to the slaves - this works fine. But I just cannot work out how to get the slaves to send data back to the master. I've searched the forums, the interwebs, and everything else I can think of.

Can anyone point me in the right direction?

Thanks.

Basically you give the "master" an address as well. Then either end can initiate communication. I got the idea from somewhere else, but documented it in my own way here:

Both ends do this:

 Wire.onReceive (receiveEvent);

Then either one can respond when the other one starts a communication. You should do error checking on the Wire.endTransmission call in case it fails (eg. due to arbitration).

eg.

byte err = Wire.endTransmission ();

if (err != 0
  {
     // try again or something
  }

Excellent! Thanks very much. Works perfectly. The I2C documentation on the Arduino website wasn't clear on this.

Thanks again.