0
\$\begingroup\$

I have a strange situation with an I2C INA219 current/volt meter module (datasheet).

In front of me, I have a rather scary 16.8V lipo - its positive output is on the VIN+ of the INA219. The VIN- leads off to a big chunky switch, which leads off to an XL4015 DC-DC, set to 5V output. Its output reads 5.02V, with no load. Power for the UNO is obviously coming from the USB port of my laptop. The battery ground of is not connected to the UNO ground, but the GND of the sensor module is, as prescribed.

Below are the XL4015, the INA219, and finally, the diagram of precisely what I see in front of me:

enter image description here

enter image description here

enter image description here

Switching the XL4015 on or off does not affect the battery output leading to the INA219 input, which reads as 16.03V. The INA219 sensor is happily connected, (SCL-SDA-GND-VCC heading off to an Arduino UNO) and is giving readings to the UNO.

Its just that they are wrong. I'm obviously very interested in getting the 16.8V battery voltage, but the reading is something weird, like 1.08V. A multimeter says otherwise, as I said. Serial output:

Bus Voltage: 1.08 V Shunt Voltage: 1.25 mV Load Voltage: 1.09 V Current: 12.60 mA Power: 14.00 mW

Below is the code:

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;


void setup(void) 
{
  Serial.begin(115200);
  }
  uint32_t currentFrequency;
  ina219.begin();
}

void loop(void) 
{
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");
  delay(2000);
}

The connections should be super dumb obvious, but I am dumb, apparently, or I've got a dud sensor. Yet that 12mA looks about right, so the current sensing bit seems to be doing its thing. Voltage reading though....

Any ideas?

\$\endgroup\$
7
  • 4
    \$\begingroup\$ Is this sensor thing galvanically isolated or not? Otherwise you need to tie the grounds together. Also please post real schematics, not some block scheme. \$\endgroup\$
    – Lundin
    Nov 23, 2021 at 15:04
  • \$\begingroup\$ Ok so it would seem this is the TI INA219-SOT23, correct? You might want to edit in the link to that datasheet in the question. \$\endgroup\$
    – Lundin
    Nov 23, 2021 at 15:09
  • \$\begingroup\$ @Lundin galvanic isolation does not matter, does it? You cannot measure battery voltage this way. It is most likely the Adafruit breakout, which has messed up connection schematics. \$\endgroup\$
    – Maple
    Nov 23, 2021 at 15:14
  • \$\begingroup\$ @Maple It's not at all clear to me how everything is connected, by most of these Arduino modules seem to like placing optocouplers all over the place. \$\endgroup\$
    – Lundin
    Nov 23, 2021 at 15:17
  • 1
    \$\begingroup\$ You say battery ground is connected to sensor ground, but your diagram disagrees. You must also label the pins on your diagram, otherwise it is unclear what each wire connects to. \$\endgroup\$
    – DamienD
    Nov 26, 2021 at 9:04

1 Answer 1

3
\$\begingroup\$

I suspect you are using Adafruit breakout board or some knockoff. The documentation on Aadafruit site is messed up.

You have two wiring diagrams for current measurement, using screw terminals to connect on-board shunt inline with load.

And you have third diagram on the bottom for voltage AND low-current measurement, using pin header holes to connect VIN+, VIN- and GND to the load.

Note the difference! The GND connection is required to measure voltage.

If you want to measure high current, you need to use screw terminals to connect thick wires. But you can still use pin header to connect GND. I suggest consulting TI datasheet for wiring schematics and explanations how it works. It is much more informative than geared towards kids Adafruit site.

UPDATE

Important: the following is based upon assumption that XL4015 does have 50 mOhm shunt resistor in the negative rail.

The primary reason you have a problem is that INA219 is designed to measure bus voltage, not supply voltage. In the picture below (from TI datasheet) you can see that in bus voltage position of the input selector it measures between Vin- and GND. Some sensors, like INA260, are more flexible in this regard.

enter image description here

But not all is lost! Note that breakout board uses 100 mOhm shunt. What you can do is remove it and use 50 mOhm shunt on XL4015 board instead. By reducing PGA gain in half you will get exactly the same range with minimal changes in software.

You can also measure battery voltage between battery + terminal and common ground. The voltage drop on the shunt is negligible, but you can even account for it, since you measure it anyway, to get current reading.

So, begin with de-soldering the shunt from INA219 board. Then make the connections as follows:

enter image description here

The INA219 and Arduino grounds are now at same potential, connected to the output of DC-DC converter, so no problems here.

If you compare this schematics to the one from the datasheet, you should notice that in shunt voltage mode the polarity of inputs Vin+ and Vin- is reversed. However according to datasheet shunt differential can be negative:

enter image description here

So, you just have to account for this when you read conversion results. From the datasheet (8.6.3.1 Shunt Voltage Register): "Negative numbers are represented in 2's complement format". Convert this to positive number and you should have the same result as it would have been with original connections.

UPDATE 2

I just realized that XL4015 is battery charging board, which you are using as DC-DC converter. This is extremely weird choice. I am not even sure it would work properly with load attached on battery side.

There are hundreds if not thousands of cheaper DC-DC step-down boards that don't have shunts in them. That would simplify sensor connection a lot, as you can place it before the converter and use conventional wiring with common GND throughout.

I strongly recommend choosing different DC-DC for this application first, then deal with sensor board.

UPDATE 3

I found XL4015 module schematic. While it is not just a buck converter, it is not an appropriate lithium battery charger either. Although it can be used (with great care) as such in a pinch. An appropriate charger should include at least end-of-charge shut-off, ideally support cell balancing and have pre-charge stage in charging profile.

Nevertheless, the schematic confirms 50 mOhm series resistor in a ground rail, so you can definitely use a connection suggestion from first update.

\$\endgroup\$
6
  • \$\begingroup\$ Hmm, that's a possibility. I think I'll cautiously hook on an OLED and code the Arduino to show me what's going with the INA219 readings, then try power the UNO up via the XL4015 5V output (as originally intended) with no USB to the computer. This will let me connect a common ground and see what happens. The XL4015 has a 50R resistor between its input and output ground, and the output ground will be the one to head off to the Arduino - could there be issues from this I wonder, with or without a USB connection? \$\endgroup\$
    – Hoets
    Nov 23, 2021 at 15:35
  • \$\begingroup\$ Are you sure about 50R resistor? First, it is way too large for current shunt. Second, ground is weird place for it, as it creates potential between supply and load grounds. This makes it problematic to measure battery voltage correctly, as you need GND to battery "-" for measurement, and you need GND to Arduino GND for communication. This is where galvanic isolation comes in handy, but it is not easy to use in this case. Finding DC-DC with direct GND pass-through is most cost efficient solution for this, IMHO \$\endgroup\$
    – Maple
    Nov 23, 2021 at 15:50
  • \$\begingroup\$ @Maple I have a similar board with current sensing in the ground (odd choice, right?) but the resistor value is 50 mOhm, so this part is probably a mistake. \$\endgroup\$
    – devnull
    Nov 23, 2021 at 16:12
  • \$\begingroup\$ @Hoets Can you, please, confirm what devnull is saying? 50 mOhm makes much more sense. And in that case I may have a solution for you that does not involve additional investment \$\endgroup\$
    – Maple
    Nov 23, 2021 at 18:42
  • 1
    \$\begingroup\$ @Hoets If you are sure it's just a DC-DC then indeed it would be bad choice for LiPo charging. I was going from product description and that there are more components on the PCB than necessary for simple conversion. If you are confident in it and you have confirmed devnull's correction above then you can use it as described in first update. \$\endgroup\$
    – Maple
    Nov 24, 2021 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.