Strange i2c bus issues with Photon

I received a Photon in the most recent shipment and upgraded it to 4.3rc2. I am seeing bus corruption/crashing happening after a random period of time. The failure exhibits in returning 0xFF for all reads no matter which sensor I am using on the bus. It will sporadically return a read here and there but for the most part it just gets into this mode where no reads work. I reset the unit and then it works for a bit until the issue appears again. I currently poll the sensors 2 seconds apart every 10 seconds. I have increased the length between loops to 60 seconds and the issue still happens.

The code I am using is very basic wire code:

bool getTemperatureAndHumidity()
{
  // Wake AM2315 up
  Wire.beginTransmission(AM2315_ADDRESS);
  delay(2);
  Wire.endTransmission();

  // Request Temperature & Humdity
  Wire.beginTransmission(AM2315_ADDRESS);
  Wire.write(AM2315_READREG);
  Wire.write(0x00);  // start at address 0x0
  Wire.write(4);  // request 4 bytes data
  Wire.endTransmission();
  delay(10);

  // Read Temperature & humidity
  uint8_t reply[10];
  Wire.requestFrom(AM2315_ADDRESS, 8);
    for (uint8_t i=0; i<8; i++) {
      reply[i] = Wire.read();
    }

    if (reply[0] != AM2315_READREG) return false;
    if (reply[1] != 4) return false;

    humidity = reply[2];
    humidity *= 256;
    humidity += reply[3];
    humidity /= 10;

    temperature = reply[4] & 0x7F;
    temperature *= 256;
    temperature += reply[5];
    temperature /= 10;

    // change sign
    if (reply[4] >> 7) temperature = -temperature;

    return true;
}

I see the behavior with the AM2315, TSL2561 (Adafruit Library) and the Atlas Scientific EZO pH Circuit. The sensors work perfectly fine on a Raspberry Pi2. Any ideas?

Try checking the return value of Wire.endTransmission(), if it’s non-zero there was an error on the bus, and the I2C on the photon or on the sensor may be in a strange state.
Also, try checking the return value of requestFrom if it’s not 8, the read was not successful.

If an error occurs you should try to re-init the state of the I2C by calling Wire.begin() again. Try that and

ie:

if( Wire.endTransmission() != 0 ){ 
    Wire.begin() ;     /* re-init the i2c */
    return false ;     /* let the caller know the read failed */
 }

this problem is discussed here too:

I added some checks and on endTransmission() I am getting a 4 back. I added logic to do a Wire.Begin(); when I detected that and only have had moderate success. For one sensor it started getting past endTransmission() but on reads only returns 0xFF. For the other sensor nothing I seem to do anymore and stop it from returning code 4 on endTransmission().

I had problems with the I2C on Photon as well with a variety of sensors and the SparkFun Weather shield. The problem is in the firmware and I understand it is actively being worked. Hope to see a fix soon.
Update: 0.4.4rc3 resolved the I2C issues with the SparkFun Photon Weather Shield.

Please update to 0.4.4.rc.3 where the I2C issues have been fixed! :smile:

1 Like