Usage fault with i2c bus wire and wire1 endTransmission

I am having problems using a humidity sensor SHT31-D with wire and wire1. I stripped out code from the Adafruit_SHT31 library and to find the issue and it works great with arduino. Whenever endTransmission is called, it puts the Electron into a usage fault (5 flashes). Problem on both wire and wire1 pins.

I want to use wire1 (pins C4 & c5) so I set SLC pin to INPUT as discussed.

I checked for DIV/0 by printing all the wite() commands.

I have the pull-up resistors as discussed.

Code -

void writeCommand(uint16_t cmd) {
  Serial.println("writeCommand input cmd = ");
  Serial.println(cmd);
  delay(100);
  Wire1.beginTransmission(0x44);
  Serial.println("writeCommand cmd >> 8");
  Serial.println(cmd >> 8);
  delay(100);
  Wire1.write(cmd >> 8);
  Serial.println("writeCommand cmd & 0xFF");
  Serial.println(cmd & 0xFF);
  delay(100);
  Wire1.write(cmd & 0xFF);
  Serial.println("writeCommand end Transmission");
  delay(100);
  Wire1.endTransmission(); 
  Serial.println("writeCommand Done");
  delay(100);  
}
 
void setup(){
  Serial.begin(9600); // connect serial
  
  
  pinMode(C5, INPUT); 
  
  delay(10000); //so I have time to start serial monitor
  
  writeCommand(0x30A2); ///SHT31_SOFTRESET);
  delay(10);
}

bool CheckHumidity(float &hum){
  Serial.println("Here 1");
  delay(100);
  
  writeCommand(0x2400); ///SHT31_MEAS_HIGHREP
  
  uint8_t readbuffer[6];
  Serial.println("Here 2");
  delay(100);
  
  Wire1.requestFrom(0x44, (uint8_t)6); //(_i2caddr, 
  if (Wire1.available() != 6) 
    return false;
  for (uint8_t i=0; i<6; i++) {
    readbuffer[i] = Wire1.read();
  }
  uint16_t SRH;
  SRH = readbuffer[3];
  SRH <<= 8;
  SRH |= readbuffer[4];
  
  Serial.println("Here 3");
  delay(100);
  
  
  uint8_t *data = readbuffer+3;
  int len = 2;
  const uint8_t POLYNOMIAL(0x31);
  uint8_t crc(0xFF);  
  for ( int j = len; j; --j ) {
      crc ^= *data++;

  for ( int i = 8; i; --i ) {
    crc = ( crc & 0x80 )
      ? (crc << 1) ^ POLYNOMIAL
      : (crc << 1);
        }
  }
  
  Serial.println("Here 4");
  delay(100);

  if (readbuffer[5] != crc) return false; 
  
  double shum = SRH;
  shum *= 100;
  shum /= 0xFFFF;
  hum = shum;
  
  Serial.print("humidity = ");
  Serial.println(hum);
    
  return true;
}

void loop(){  
  float curHum = 0.0;
  CheckHumidity(curHum);    
  Serial.print("humidity = ");
  Serial.println(curHum);
  delay(1000);
}

I might be blind, but where are your Wire.begin() or `Wire1.begin() calls?

Also what system version are you targeting?

Here is a working library for the SHT31D sensor.

1 Like

Why are you putting these huge delays between the .write()'s? .write() just caches bytes until the endTransmission() call, which is what sends the message.

Thanks, that was it I was missing the lost the Wire.begin when I stripped the code. I was missing the pinMode(C5, INPUT); before I stripped it all.

1 Like

The delays are because on one of them aruino or particle, I have had the last serial.print not show up if it something goes wrong really fast after it.