54. I2C Concepts 2

Question.3

What will be printed on the Serial Monitor of Slave 1 and Slave 2 ? (after executing the following code)

Master Arduino Code:

#include <Wire.h>
void setup() {
  Wire.begin();
  delay(1000);


  Wire.beginTransmission(0); // general call address
  Wire.write("hi");
  Wire.endTransmission();
}


void loop() {
}

 

Slave 1 Arduino Code:

Note : Slave 2 Arduino code is the same, only the address of slave 2 is 32.

#include <Wire.h>
void setup() {
  Wire.begin(27); // Address of slave 2 = 32
  TWAR |= 1; // enable general call
  Serial.begin(9600);
  Wire.onReceive(receiveData);
}


void loop() {
}


void receiveData(int byteCount) {
  while (Wire.available()) {
    char c = Wire.read();
    Serial.print(c);
  }
  Serial.println();
}

 

Select Answer