meshtastic / firmware

Meshtastic device firmware
https://meshtastic.org
GNU General Public License v3.0
3.62k stars 906 forks source link

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

Open nebman opened 2 weeks ago

nebman commented 2 weeks ago

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

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

fifieldt commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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

fifieldt commented 1 week ago

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