Wire Library [repeated start]

About our forum:
We like to the see the full sketch and we really like to see the picture (not a link).
If you go to the top of your first post, you can change the Category. Change it to: "Networking, Protocols, and Devices category".
Your picture uploaded to this forum:

About the Wire library:
You don't have to test for error number 2, since the numbers have different meanings on different boards. Just check if it is not zero to see if "something" is wrong.
If something is wrong, you can not continue with a Wire.write() and Wire.endTransmission(), you have to do the whole thing over again.
See also my alternative explanation of the functions of the Wire library.

Totally nuts:
A repeated start to claim the I2C bus while testing the NACK/ACK from a Slave is totally nuts. I have never seen that before and it makes no sense. The repeated start should be used while communicating with a Slave, not to start a communication.
Not sending a STOP, until the Slave is ready and then send the data and a STOP after that is not possible with the Wire library. It hurts my brain to even think about it, because it makes no sense. I'm pretty sure that it breaks the I2C standard.
The I2C hardware follows the I2C standard, so it might be impossible to do that.
With a software I2C library you can change the source code and do whatever you want. I hope that is not needed.
Can your try first without that repeated start ?

To delay or not, that is the question:
When the I2C is done in software, a Master in hardware is sometimes too fast. When the Master delays too much, the Slave might stop listening (as DrDiettrich already wrote). I suggest some delay, for example 10 to 100 µs.

Can you try this:

// Testing the ACK to its I2C address does not need a databyte
// Try 50 times with 5ms delay in between.
int error = 0;
for( int i=0; i<50 and error==0; i++)
{
  delay(5);
  Wire.beginTransmission(0x4B);
  error = Wire.endTransmission();  // use 'false' to claim the bus, try without first.
}

if( error == 0)             // was there really a succesfull ACK from the Slave ?
{
  delayMicroseconds(50);         // some delay, since the Slave uses software

  // The Slave is responding, send the real data. 
  Wire.beginTransmission( 0x4B);
  Wire.write( 0x01);
  error = Wire.endTransmission();   // no parameter, the I2C bus is releases after this.
  if( error == 0)
  {
    Serial.println( "Success, data delivered to Slave");
  }
  else
  {
    Serial.println( "Error, no success after all");
  }
}
else
{
  Serial.println( "The Slave was never ready during testing");
}
1 Like