53. I2C Concepts 1

Question.2

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();
  Serial.begin(9600);
  delay(1000);

  Wire.beginTransmission(0);  // 0x00 is I2C general call address used for broadcasting messages to all slaves
  Wire.write("Hello, Slaves!");
  Wire.endTransmission();

  Serial.println("Data sent to all slaves.");
}

void loop() {
}

 

Slaves Arduino Code:

Note : same code has been uploaded to both the slaves with address of slaves are

  • slave1= 27 
  • slave2 = 32
#include <Wire.h>
void setup() {
  Wire.begin(27);  // for slave2 address will be 32
  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