Wire.endTranssmission returns error 5

hello, i m using I2C bus between EVB pico wiznet w5500 as master and arduino nano rp 2040 Connect as slave, this is the code on the master side :

while (retries < MAX_RETRIES && !success) {
    Wire0.beginTransmission(8);
    Wire0.write("R"); // Command to request msg
    error = Wire0.endTransmission();

    if (error == 0) {
      success = true;
      Serial.println("Transmission successful");
    } else {
      Serial.print("Transmission error: ");
      Serial.println(error);
      retries++;
      delay(100); // Wait a bit before retrying
    }
  }

and this is the code on slave side :

void receiveEvent(int howMany) {
  
    command = Wire.read();
Serial.println(command);
  
}

well it works most of the time, but sometimes i m getting error 5 from Wire.endTranssmission , can anyone tell me what does it mean and why does it happen ? and of course when that happens , i m not receiving the R on the slave side.

Then you have a RP2040 for both the Master and the Slave.
I recently learned that the Slave Mode for the Arduino layer on top of Mbed on a RP2040 is actually very good and causes no troubles.

The I2C bus is not supposed to work with retries. It is not a fault tolerant bus. If you need a retry, then your I2C bus is so bad that you need to fix the I2C bus (the hardware).

Did you connect the GNDs ? Are there pullup resistors ?

I could not track down what error 5 is for the RP2040.
The documentation says that it is a timeout: https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
But other numbers are used on other boards.

When two Arduino Uno boards are used, then it will go wrong if the Master tries to communicate over I2C continuously. But those run at 16Mhz.
The RP2040 runs at 133MHz, but there are a number of software layers under the Arduino layer. Perhaps you should wait a few milliseconds before trying to send something again.

Thank you! The issue occurs roughly once every ten attempts and only at the start, meaning it happens when powering up the modules and sending data for the first time. The error persists until I disconnect the boards and try again. I'm not sure why this is happening.

for the retries i mainly added them this morning, i thought that if i waited a little and send it over and over again it will send it and work, but it did nt happen.

Error 5 means timeout. Here is why:

Serial is much slower in transmission of the formatted byte value. The I2C transmission will fail as soon as the Serial buffer is full.

Either remove the transmission log from the callback function or add a long enough delay between repeated sendings.

1 Like

thanks! will try it out .