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

Detect charging status by measuring current flow with configured INA219 battery sensor #5271

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

nebman
Copy link

@nebman nebman commented Nov 7, 2024

Detect charging status by measuring current flow with configured INA219 battery sensor

As already described in issue #5226 here is the pull-request

Intro

Nodes that use an e.g. INA219 for monitoring the battery voltage (explicitly set address in the power settings) can detect the charging status by measuring the current flow across the shunt. The current implementation in AnalogBatteryLevel doesn't make much use of the isCharging() function.

If the current is negative, that indicates the battery is being charged.

This PR adds the functionality to detect the charging status by measuring current flow when the INA address is set as battery monitor.

Changes to code

  • new abstract class CurrentSensor with function getCurrentMa() to not have to request full metrics every time
  • minimalistic implementation for the INA219 (I can expand that to the INA3221 and INA260 classes although I can't test those)
  • on by default when the power settings have an INA address set
  • can be disabled by define DISABLE_INA_CHARGING_DETECTION
  • current flow detection direction can be inverted with INA_CHARGING_DETECTION_INVERT when VIN+ is not wired to the high side (question: any known variants out there with this IC onboard where we could pre-check this?)

Tests

I tested the branch on two of my custom build NRF52 nodes integrating an INA219. Also tested both extra defines. Works well so far and the correct charging status is displayed on the screen

Every input is appreciated, especially because I'm no C++ expert ;) Unfortunately the trunk extension keeps crashing in my VS Code

minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging
added comments and 2 extra defines to disable and swap detection direction
fix disabled case
minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging
added comments and 2 extra defines to disable and swap detection direction
fix disabled case
@fifieldt fifieldt force-pushed the use-ina-currentflow-for-charging-detection branch from 90062a1 to e54d259 Compare November 7, 2024 11:40
@fifieldt
Copy link
Contributor

fifieldt commented Nov 7, 2024

Thank you very much for this, very useful first contribution to Meshtastic :)

I think I have a INA3221 lying around here to help test.

What's the best way to wire these things up?

@nebman
Copy link
Author

nebman commented Nov 7, 2024

Thank you and also thanks for offering your help to test!
To wire this up, you need the classic I2C pins (VCC, GND, SCL, SDA), and splice the INA3221 Channel 1 (if I read that correctly from code) into your plus side power supply line, i.e. VIN1+ to battery positive and VIN1- towards the load/board.
Be aware there are boards out there with a kinda faulty design, where all channels refer on one side to GND. They can't be used.

As it just happened, the mailman delivered me an INA3221 board some minutes ago :) If you give me some time I might be able to test it myself (also more coding is needed).

@nebman
Copy link
Author

nebman commented Nov 7, 2024

code for INA3221 is up, using channel 1 for battery voltage and current monitoring, the charging detection works fine

@fifieldt
Copy link
Contributor

Hi, apologies. It looks like the devices I have lying around are an INA231 and an INA226. I will try and buy an INA3221

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.

4 participants