We are using two Arduino UNOs as a master/slave for this I2C protocol. We used the first Arduino (slave) as a sensor that will input dummy data (in this case, temperature data). The Arduino (master) should be able to output what the slave has input, but every time we run it, the output is always 255 instead of the temperature data.
We don't know what is wrong with the code, but we want to read the real data. Can anyone help us?
Here is the code we use for the Master:
// Include Arduino Wire library for I2C
#include <Wire.h>
// Define Temp Slave I2C Address
#define TEMP_ADDRESS 9
void setup() {
Wire.begin(); // Initialize I2C communications as Master
Serial.begin(9600); // Setup serial monitor
Serial.println(">>>MASTER IS READY<<<");
Serial.println('\n');
}
void loop() { //Temperature
delay(50);
Wire.requestFrom(TEMP_ADDRESS, 12);
Serial.print("TEMP ADDRESS: ");
Serial.println(TEMP_ADDRESS);
Serial.println("Receive data from TEMP Slave");
byte MasterReceivedTemp = Wire.read();
Serial.print("Temperature = ");
Serial.println(MasterReceivedTemp);
Serial.println('\n');
}
Best Answer
Instead of discarding the return value from
Wire.requestFrom(TEMP_ADDRESS, 12);
, test the return value to determine whether or not the Wire.requestFrom worked correctly:Documentation is here:
https://www.arduino.cc/en/Reference/WireRequestFrom
See also this Q&A about using Arduino as an I2C slave:
https://arduino.stackexchange.com/questions/43007/why-is-a-delay1-necessary-before-wire-requestfrom