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