0

we are using 2 Arduino UNO 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 what to read the real data. Can anyone help us? Thank you.

Here was 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');
}
  • You're requesting 12 bytes from the slave? Is the slave expecting you to ask for 12 bytes? – Majenko Jun 21 at 9:56
  • Chances are you're "reading" -1 (which is 255 when squeezed into a byte)n which means "no data available to read". – Majenko Jun 21 at 9:58
  • Are you sure,you are really reaching the slave? You are reading data with Wire.read(), without checking, if there is actually data in the buffer. If there isn't, read will return -1, which then gets to be 255, when downcasted to byte. Print out the return value of Wire.requestFrom(), which shows the number of actually read bytes. Have you checked with the I2C Scanner example, if the slave is really found at that address? – chrisl Jun 21 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.