Skip to content

Conversation

@ilario
Copy link
Contributor

@ilario ilario commented Oct 16, 2024

Trying to measure small currents with a 1 Ohm (for currents up to 80 mA) or a 10 Ohm (for currents up to 4 mA) shunt resistor I created new calibration functions, following the instructions in the code comments and in the INA219 datasheet but they were all wrong.
For example, following the instructions I ended up using a calibration factor of 41, that resulted in very poor resolution.

So, I would consider that the instructions are confusing, and I rewrote them.

As you can see, the logic I propose is very different:

  1. for gain/8, use always 4096 as calibration factor (unless you are calibrating versus a reliably measured current). For other gains, 4096 is also a good choice (but also 8192 and other values would be equally as good, I just used 4096 for simplicity).
  2. choose the gain that better suits your needs
  3. consider replacing the shunt resistor when no gain is a good fit

Please check if the code works well also in other cases (I checked only the last two functions with shunt resistors of 1 and 10 Ohm and for currents far from the saturation ones).

@ilario
Copy link
Contributor Author

ilario commented Oct 16, 2024

Pylint was complaining about too many instance attributes because I used

self.calibration = 4096

instead of the previously present

self._cal_value = 4096
self._raw_calibration = self._cal_value

As I don't think I made the situation worse, I silenced the "too many instance attributes" error.
If you consider I should address this, please advise how.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged main and resolved conflicts created by newer commits.

This looks good to me. The updated equations appear to match the information in the the data sheet to me. I don't have hardware to test.

We can always revert or update as needed if people notice issues with the new version.

@FoamyGuy FoamyGuy merged commit d2e1484 into adafruit:main Jan 13, 2026
1 check passed
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 14, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_INA219 to 3.5.0 from 3.4.31:
  > Merge pull request adafruit/Adafruit_CircuitPython_INA219#37 from ilario/calibration

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel to 6.3.20 from 6.3.18:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#172 from FoamyGuy/code_format
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#170 from jposada202020/bouncing_ball_example
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

Successfully merging this pull request may close these issues.

2 participants