I2C Slave Connection Issue

Hi there, I have developed a robot using multiple boards connected via I2C. The problem I'm facing is the communication between the slaves. Let me clarify, I have 1 master and 2 slaves working together.

  • Master checks few things and pass an integer to slave 1
  • slave 1 does few things and need to pass an integer to slave 2

My problem is on step 2 when I try to send something from slave 1 to slave 2. Doesn't I2C allow this? I can pass messages from master to both slaves but slave to slave communication is not working.

Did anyone else experience such problem and what possible solutions are available out there.

Thank you.

Doesn't I2C allow this?

No. A master makes requests. A slave responds. A slave can do nothing on it's own.

and what possible solutions are available out there.

Serial communications don't have such limitations. Serial can be used for master->slave, slave->master, and slave->slave. If you have only one Serial port, SoftwareSerial can be used. Only one instance of SoftwareSerial can be listening at a time, though.

If your slave 1 needs to talk to slave 2, you could have your slave send some byte to the master to tell that the data needs to be forwarded to the slave.

epic_roboworks: If your slave 1 needs to talk to slave 2, you could have your slave send some byte to the master to tell that the data needs to be forwarded to the slave.

But, slaves can not initiate communication.

correct. I had a similar setup. A slave and two masters would always send packets of two bytes

On the master side, I had something like this

unsigned long whenLastPolledSlave1=millis(); //global var
#define FORWARDREQUEST 0x77 //slave will respond with this to the master if the slave wants to send something to another slave
....
void loop(){
 if( millis() - whenLastPolledSlave1 > 2000) { //ensures that there is at least 2s between polling 
 twoBytes=pollSlave1();
  if( twoBytes == FORWARDREQUEST){
  twoBytes=pollSlave1(); //poll the slave again, the next bytes we expect the data which will be forwarded
  sendDataToSlave2(twoBytes);
  }
  else{
  //do something useful with the bytes intended for the mater
  }
 }
}