Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library crashes when using other I2C devices #2

Open
d4nd4nd4nd4n opened this issue Oct 18, 2021 · 3 comments
Open

Library crashes when using other I2C devices #2

d4nd4nd4nd4n opened this issue Oct 18, 2021 · 3 comments

Comments

@d4nd4nd4nd4n
Copy link

d4nd4nd4nd4n commented Oct 18, 2021

This INA220 library is awesome and it is working perfectly. Or perfectly until I add another I2C device to the bus. The other device works but INA220 dies.

For example I have the INA220 (addres 0x40) and an ADS1115 AD board (address 0x48) connected to the same I2C line on my ESP32. When INA220 is alone it works perfectly with this library. But when both devices are initialized, INA220 dies. It gives the same strange values with each read request.

But if I skip INA library and read manually the INA registers with the Wire library, I get the correct reading while also ADS1115 is initialized.

I can have both devices physically connected to the I2C line at the same time. Problems start when I initialize the libraries for the devices (ads.begin(); and ina220.begin(...);). If I only initialize INA library (ina220.begin(...)) the device works properly.

What could cause this?

@nathancheek
Copy link
Owner

Hello!

What library are you using for the ADS1115? My first thought is that the other library could be clobbering something set up by the INA220 library.

Do you have an oscilloscope or logic analyzer? It could be useful to capture the INA220 I2C signals with and without the ADS1115 library active, to see what changes between the two configurations (speed, voltage, data, etc).

What incorrect value do you see returned by the INA, and which register is it? Is the value the same no matter which register you read?

If you are able to upload your code, I'd also like to take a look at that.

@d4nd4nd4nd4n
Copy link
Author

I fixed it!?!?

I have the last days been making my own version of your library. This has been good as now I really understand how the INA device works. But when making an example to you, suddenly I get both INA and ADS to work on the same I2C line with your library. Very strange.

I assume that there is something wrong with my original code. I will leave it and write a new one where I have all the functions working.

But thanks again for a great library! Also the source files are very understandable and usable. Professional work!

Below is your example code with the ADS added. This code is working and giving results from both INA and ADS.

`#include <INA220.h>
#include <Adafruit_ADS1015.h> //Lämpötilamittaus

/******************

  • Begin Configure
    /
    const uint8_t NUM_INA = 1; // 1 INA devices
    const uint8_t MAX_CUR = 10; // 10 Amps
    const uint16_t SHUNT_R = 20000; // 20 mOhm
    uint8_t ina_addresses[NUM_INA] = {0x40}; // INA I2C addresses
    /
  • End Configure
    ******************/

INA220 ina220;
Adafruit_ADS1115 ads;

void setup() {
Serial.begin(115200);

pinMode(14, OUTPUT);
digitalWrite(14, LOW);

uint8_t availableDevices = ina220.begin(MAX_CUR, SHUNT_R, INA_ADC_MODE_128AVG, INA_ADC_MODE_128AVG, INA_MODE_CONTINUOUS_BOTH, ina_addresses, NUM_INA);
Serial.print("Configured "); Serial.print(availableDevices); Serial.print(" of "); Serial.print(NUM_INA); Serial.println(" INA220 current sensors");
delay(100);
ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV/bit
ads.begin();
delay(100);
}

void loop() {
for (int i = 0; i < NUM_INA; i++) {
float vol = ina220.getBusMilliVolts(i) / 1000.0;
vol *= 4.567; //voltage divider factor
float cur = ina220.getBusMicroAmps(i) / 1000.0;
float power = ina220.getBusMicroWatts(i) / 1000.0;
power *= 4.567;
Serial.print("INA at 0x"); Serial.print(ina_addresses[i], HEX); Serial.print(" measures "); Serial.print(vol); Serial.print(" V, ");
Serial.print(cur); Serial.print(" mA, and "); Serial.print(power); Serial.println(" mW");

digitalWrite(14, HIGH);
int lampotila = ads.readADC_SingleEnded(0);
digitalWrite(14, LOW);
Serial.println("Lampotila: " + (String)lampotila);

}
Serial.println();
delay(1000);
}`

@nathancheek
Copy link
Owner

That's great! I'm glad to hear you got it working with the example code.

Writing your own library (or modifying someone else's) is definitely a great way to learn the chip.

Please let me know if you have any more issues with the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants