Initializing I2C Communication: Programming Question

Yesterday I spent a whole day trying to troubleshoot why I was unable to communicate with a breakout board (INA260) that uses I2C communication. I combed through the spec sheet, I combed through the library, but it was only when I started analyzing some sample code that I figured out what the issue was.

Upon recognizing the issue, I also realized that this method of initializing communication is common for other breakout boards I've used before (e.g., ADS1115):

  if (!ina260.begin()) {
    Serial.println("Couldn't find INA260 chip!");
    exit(0);
  }  

  if(!adc.init()){
    Serial.println("ADS1115 not connected!");
    exit(0);
  }

Can anyone explain what's going on here? I read the above code as "if we haven't initialized communication with the board, do something." Somehow, this if statement has the same effect as if I were to just type:

ina260.begin(); // start communication w INA260 
adc.init();  // start communication w ADS1115

Why is that? Does creating the if statement also force Arduino to initialize communication if it hasn't already? Curious to learn.

Thanks,
Luke

Your version runs whether the begin was successful or not. So later nothing will work the way you want, as there is effectively no sensor.

The real version tries, and if it fails to initiate the device wisely says so and exits, so you can fix the problem.

a7

I read it as "if the begin function returns false, tell the boss something is wrong, and throw up your hands in despair".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.