I2C protocol to Connect two Arduino

Hi everyone,

I need to connect two Arduino together. So I have to use the I2C Protocol.
In the Slave part we need to both send and receive data to and from Master.
I've learned for receiving the data from master I have to add the following code in the void setup section:

Wire.onRequest(requestEvent);

Also for receiving data from Master I have to use the following code in the void setup section:

Wire.onReceive(receiveEvent);

The question that can I add both of them in order to send receive data?

Thanks
Mahdiyar

Yes. What happened when you tried it?

Mahdiyar:
I need to connect two Arduino together. So I have to use the I2C Protocol.

Assuming that you want to communicate between UNO (I2C-Master) and NANO (I2C-Slave), then you have the following hardware setup.
i2c-ms.png
Figure-1:

In the Slave part we need to both send and receive data to and from Master.

Slave will send data to Master when Master sends a request command to the Slave by executing the following code which must be included in the Master sketch:

Wire.requstFrom(slaveAddress, numberOfDataBytesRequestedd);

I've learned for receiving the data from Slave I have to add the following code in the void setup section of Master:

Wire.onRequest(requestEvent);

Also for receiving data from Master I have to use the following code in the void setup section of the Slave sketch:

Wire.onReceive(receiveEvent);

And then define the declare/define the following function after loop() function of the Slave sketch:

void receiveEvent(int howMany)
{
    //put codes here as needed
}

The question that can I add both of them in order to send receive data?

The question is not clear to me.

i2c-ms.png