Can i set a MASTER Arduino as sender and receiver?

Hi, I have two Arduino UNO and I want to connect them using i2c, I will set one as MASTER and the other as Slave, but I want the two board can send and receive, example the MASTER send to SLAVE some byte to do something (ex: move servo motors to positions) , so the SLAVE read this byte and set it in the target position for the servo motor and when they reach the position the SLAVE send to MASTER that the job is done and it can continue the other code step or something like that . I want to know if can i set a MASTER Arduino as sender and receiver and also the SLAVE?

Yes, it is explained here, with an example.

You can implement a form of polling. Master sends data and next queries the slave; slave will reply with e.g. false (0) indicating that the move is not complete yet or with true (1) if the move is complete.

A simpler way might be a wire between the two Arduinos on two digital pins; input on the master, output on the slave. Set pin high when move is completed, low if it's not completed yet; master can check the status of the pin.

sterretje: You can implement a form of polling. Master sends data and next queries the slave; slave will reply with e.g. false (0) indicating that the move is not complete yet or with true (1) if the move is complete.

Sending 1 or 0 about position was just an example , some times i will send a string or list of numbers

the SLAVE can answer more than one byte, so yes - a SLAVE can respond an integer or a string also.

But a I2C SLAVE can not initiate communication, so your MASTER has to pull data from the SLAVE.

You could setup an I2C MULTIMASTER environment but in your usecase I assume it's far easier for you if you let your MASTER ask the SLAVE in a loop/while as long as the SLAVE answers with an OK.

But this brings me to another point: if a servo is connected to your SLAVE with some well known servo library - the slave can not determine if the servo has reached the final position. So how will you get the information that your servo is in end position?

noiasca: But this brings me to another point: if a servo is connected to your SLAVE with some well known servo library - the slave can not determine if the servo has reached the final position. So how will you get the information that your servo is in end position?

Like i says sir the servo motor was just an example , maybe i will use some leds and buttons connected to the master and others to slave , when a button master is clicked ,it will send the number of the pin and it will blink a led in the slave with the same pin and vice versa , and also this was just an example sir :)

The most important is how to set the MASTER as sender and receiver , and the SLAVE also as sender and receiver

I get the impression you still miss the point:

MASTER - SLAVE in I2C can be a two way communication:

MASTER->sends to ->SLAVE SLAVE->responds to ->MASTER

meaning: a) There is two way communication between MASTER and SLAVE. b) The communication is always started by the MASTER

if you need both Arduinos to be able to initiate the communicaton you need a

MULTIMASTER

implementation where both Arduinos act as MULTIMASTER and SLAVE simultaneously.

noiasca: if you need both Arduinos to be able to initiate the communicaton you need a

MULTIMASTER

implementation where both Arduinos act as MULTIMASTER and SLAVE simultaneously.

Ok i will try it , do you have any tutorial to coding this ?

aymannox:
I want to know if can i set a MASTER Arduino as sender and receiver and also the SLAVE?

1. Let us build the following setup between two Arduino UNOs. In this setup, UNO-1 will be able to work as Master when UNO-2 will act as Slave; UNO-2 will work as Master and UNO-1 will act as Slave. This basic idea of data communication could be extended to exchange any kind of complex data transfer between two UNOs in Master/Slave combinations.
i2c-1yx.png

2. Upload the following sketch into UNO-1.

#include <Wire.h>
#define slaveUNO2Address 0x09
#define slaveUNO1Address 0x08
char msg1[] = "HelloUNO2";
char msg2[30] = "";
void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveUNO1Address);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(4) == LOW)
  {
    Wire.beginTransmission(slaveUNO2Address);
    Wire.write(msg1, sizeof(msg1));
    Wire.endTransmission();
  }

  if (digitalRead(5) == LOW)
  {
    Wire.requestFrom(slaveUNO2Address, 1);
    byte x = Wire.read();
    Serial.println(x, HEX);  //shows 0x19
  }
}

void receiveEvent(int howmany)
{
  for (int i = 0; i < howmany; i++)
  {
    msg2[i] = Wire.read();
  }
  Serial.println(msg2);
}

void sendEvent(int howmany)
{
  Wire.write(0x39);
}

3. Upload the following sketch into UNO-2

#include <Wire.h>
#define slaveUNO2Address 0x09
#define slaveUNO1Address 0x08
char msg1[] = "HelloUNO1";
char msg2[30] = "";
void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveUNO2Address);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  Wire.beginTransmission(slaveUNO1Address);
  Wire.write(msg1, sizeof(msg1));
  Wire.endTransmission();
}

void loop()
{
  if (digitalRead(4) == LOW)
  {
    Wire.beginTransmission(slaveUNO2Address);
    Wire.write(msg1, sizeof(msg1));
    Wire.endTransmission();
  }
  if (digitalRead(5) == LOW)
  {
    Wire.requestFrom(slaveUNO1Address, 1);
    byte x = Wire.read();
    Serial.println(x, HEX);  //shows: 0x39
  }
}
void receiveEvent(int howmany)
{
  for (int i = 0; i < howmany; i++)
  {
    msg2[i] = Wire.read();
  }
  Serial.println(msg2);
}

void sendEvent(int howmany)
{
  Wire.write(0x19);
}

4. Press RESET buttons of both UNO-1 and UNO-2.

5. Bring in SM1 and SM2.

6. Gently, press K1. Observe that HelloUNO2 has appeared on SM2.

7. Gently, press K2. Observe that 19 has appeared on SM1.

8. Gently, press K3. Observe that HelloUN1 has appeared on SM1.

9. Gently, press K4. Observe that 39 has appeared on SM2.

i2c-1yx.png

aymannox:
Ok i will try it , do you have any tutorial to coding this ?

no, but google is your friend:

https://www.google.com/search?q=i2c+multimaster+arduino&ie=utf-8&oe=utf-8

GolamMostafa: 4. Press RESET buttons of both UNO-1 and UNO-2.

5. Bring in SM1 and SM2.

6. Gently, press K1. Observe that HelloUNO2 has appeared on SM2.

7. Gently, press K2. Observe that 19 has appeared on SM1.

8. Gently, press K3. Observe that HelloUN1 has appeared on SM1.

9. Gently, press K4. Observe that 39 has appeared on SM2.

What will happen if i press K1 and K3 same ( the two boards send message in the same time )

aymannox: What will happen if i press K1 and K3 same ( the two boards send message in the same time )

This is a tutorial task. In reality, K1 and K2 would be two different asynchronous signals; they will, probably, never happen at the same time. However, we can make parallel connection of K1 and K3 and see what happens when K1/K3 is pressed down.