chrisb2 / pi_ina219

This Python library supports the INA219 voltage, current and power monitor from Texas Instruments with a Raspberry Pi using the I2C bus. The intent of the library is to make it easy to use the quite complex functionality of this sensor.
MIT License
114 stars 34 forks source link

Invalid current readings when maximum value of calibration register is exceeded #4

Closed chrisb2 closed 7 years ago

chrisb2 commented 7 years ago

As per the specification of the INA219 the maximum value of the calibration register is 0xFFFE (65534) (see P31). When the library calculates the calibration value is does not check that the chosen current LSB will cause this to be exceeded, resulting in invalid current and power readings.

With the Adafruit INA219 breakout with 0,1 Ohms shunt resistor and a max expected current of 100mA specified and INFO logging enabled the logged calibration value will be greater that 65534, see last line:

2017-02-21 19:09:11,039 - INFO - INA219 gain automatically set to 0.04V
2017-02-21 19:09:11,043 - INFO - INA219 max possible current: 0.400A
2017-02-21 19:09:11,046 - INFO - INA219 max expected current: 0.100A
2017-02-21 19:09:11,050 - INFO - INA219 min current LSB: 3.052e-06 A/bit
2017-02-21 19:09:11,053 - INFO - INA219 max current LSB: 2.441e-05 A/bit
2017-02-21 19:09:11,056 - INFO - INA219 chosen current LSB: 4.000e-06 A/bit
2017-02-21 19:09:11,059 - INFO - INA219 power LSB: 8.000e-05 W/bit
2017-02-21 19:09:11,062 - INFO - INA219 max current before overflow: 0.131A
2017-02-21 19:09:11,065 - INFO - INA219 max shunt voltage before overflow: 0.013V
2017-02-21 19:09:11,068 - INFO - INA219 calibration: 102400

Until this is fixed, this can be worked around by increasing the value of max_expected_current. In my case setting it to 200mA fixed the issue.