Solar panel current measurement using INA219

LesJones

Joined Jan 8, 2017
4,190
Hi ebp,
The INA219 has it's own ADC and voltage reference . It also senses current via a shunt resistor in the positive rail which can be up to 28 volts positive of the logic ground. It has registers for setting the scaling of input voltage. The interface with with the outside world is via I2C This is a link to the data sheet.

Les
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
So I have did another experiment :
I have built a simple circuit with an arduino - pins D6-D10 i have a 220ohm resistor and 5 leds. I run a code to turn them ON one by one with a few seconds delay. I have made this circuit to monitor my sensor response to a different load currents. The results are as I expected - the more current drawn, the bigger the error.
When i measure Vin- to supply GND i get about 0.1V difference between when I measure across Vin- to INA219 GND.
I was using 9.8V supply ( tested with DMM I get 9.78V), but the readings I get through my INA219 are not quite close ( see arduino serial monitor)
I have tryed measuring the voltage drops across my power rails and I was surprised to see that on some wires I was able to measure 0.1 up to 0.3Voltage drop! :eek:

That is bad news to me because if I further increase the load, the error will increase as well. And I plan draw up to 0.5A of current in my final design meaning that the error will be way too unaccurate.

Are there any other simple ways to measure voltage and current? I was planning to use potential divider at firt, but I am not 100% sure what is maximum voltage that can be generated by my solar panel when I connect it to the final design, so I could not use this approach cause I need to map maximum solar panel voltage to 5V analog arduino pin.
 

Attachments

Thread Starter

zazas321

Joined Nov 29, 2015
936
Adding a copy of the code and circuit drawing

#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
#include <LiquidCrystal.h>

int ledPins[] = {
6, 7, 8, 9, 10};
int pinCount = 5;

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup(void)
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
Serial.begin(9600);
while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
}

uint32_t currentFrequency;

// Initialize the INA219.
// By default the initialization will use the largest range (32V, 2A). However
// you can call a setCalibration function to change this range (see comments).
ina219.begin();
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
//ina219.setCalibration_16V_400mA();

Serial.println("Measuring voltage and current with INA219 ...");
}



void loop() {
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);
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
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");
digitalWrite(ledPins[thisPin], HIGH);
lcd.begin(16, 2);
lcd.print(busvoltage,3);
lcd.setCursor(0, 1);
lcd.print(current_mA);
delay(3000);
lcd.clear();
}



}
 

Attachments

ericgibbs

Joined Jan 29, 2010
18,862
hi v,
I see that you have very long 'thin' wires from the power circuits to the INA shunt resistor.
At which points are you measuring the increasing voltage drops.?

I suspect a layout/wiring cause.

E
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Yes I uwe like 30cm wire from my supply to my circuit. Which voltage drops do you mean? I measured from supply GND to other points of ground and got 0.1-0.3V. Bus voltage measurements is between Vin- and GND if thats what you are asking
 

ericgibbs

Joined Jan 29, 2010
18,862
hi,
Consider that you have 30cm length of light weight wire.
When you draw current in your load, the power supply voltmeter will read higher than the INA measured voltage due a voltage in the long connect wire, as more current is drawn by the load the greater the voltage drop at the INA/project

I would suggest you both shorten the leads and increase the wire gauge from the PSU to the project and then re-run your tests.

E
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
When I did the measurements with the 9V battery I was not using long wires but I was still the error. I only have access to one size of wires in the lab. I will try and find different sizes. But it still very strange that with 100mA of current the voltage error is 0.5V. Even with long wire its still sounds like too much
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Maybe you know any other possible ways to measure my supply voltage and display it on LCD without an error? As I have mentioned above, potential divider is not gonna be great for me as I do not know my solar panel maximum voltage with my final circuit
 

ebp

Joined Feb 8, 2018
2,332
When you are comparing the ADC readings with those of a meter, be sure you are using your meter to measure at exactly the same points that the ADC is "seeing" - preferably right at the relevant terminals of the converter.

It is a simple matter with a meter to detect voltage drops in wiring. Simply work back from the "destination" to the "source" on both the high (bus) and low (ground or common) sides. Half a volt does seem like a very large error due to wire, but it is silly to be guessing at this when it is very easy to measure. If there are drops due to wiring, you will get nowhere until you identify and fix them. Plug in breadboards are not precision friendly. Jumper wires with stranded conductors and little solid pins at the ends are prone to degradation as stands break.

0.3 VDC of difference in "ground" at different points is a magnitude I would regard as completely intolerable in a low-current low-voltage circuit. With a 9 V battery, that is a 3.3% error, which renders any analog to digital conversion resolution beyond about 5 bits completely worthless.
 

ebp

Joined Feb 8, 2018
2,332
If you have errors due to current flowing in imperfect ground conductors, there are ways of reducing the errors, largely based on the concept of "star point" grounding - everything connected to ground connects at a single "point" in the system. Star point ground doesn't always work well - it's a great concept with steady currents and sometimes far less so with currents that vary.

Example:
With the individual LEDs - instead of connecting each LED to the ground strip on the breadboard, connect them together using one of the normal five-hole strips, then use a wire to connect from that strip to a point as close as you can get to the point the INA uses for "ground." Make sure the power source (your PV panel and any other power supplies in the system) also connect very close to that point.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
As I have mentioned above, I did try and measure my voltage drops from the source to different +5V points, and I was able to Identify 0.1-0.3 Voltage drop on multiple points, but these did not have any long wires and conneciton seemed fine so I am not should how would I fix that. You have mentioned star point grounding. Lets say if I have 5 grounds, should I just use a header or something to connect all grounds together?
 

ebp

Joined Feb 8, 2018
2,332
You might use a header, but you might just stick with the plug-in board and use adjacent holes as a reasonable approximation of a star point. The idea is to get rid of as much unwanted resistance as possible between things that should be at the same potential.

I'd suggest you return your existing configuration and dig a little deeper to identify where the voltage drops are occurring. Measure drops from one end to the other of wires. If you can get your meter probes right on the ends of the wires, do that, then "move out" to adjacent holes in the breadboard. Think about each individual thing that can contribute resistance and try to identify them. You may find that the total voltage drop is just the sum of a bunch of very small drops or you may identify one or two particular parts (wires) or connections that contribute most of the drop. Fine, sharp-pointed probes are helpful for this sort of thing. If you have probes with clip tips, you can use something like sewing needles or pins.

It can be a bit time consuming to do this, but I think it's better than banging your head against a wall trying to guess where the problem lies. Troubleshooting requires a methodical process and thinking about possibilities. When you have a system with complex things like ADCs and microcontrollers with programs that might be dubious, there are lots of places for things to be wrong. Starting by eliminating suspicion were you can do it reasonably easily is usually wise. I've seen lots of people who are reasonably good at building things and writing programs, but had very little ability to diagnose and troubleshoot. Picking up toubleshooting skills is very valuable.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
So you suggesting to connect ground wires as close to each other as possible and use as short wires as I can? Instead of splitting ground wires randomly across breadboard ground rail, I can use one collumn of the breadboard and connect grounds there?
 

ebp

Joined Feb 8, 2018
2,332
Yes, that could be helpful. You can still use the ground rails on the breadboard, but with caution. Think about the current paths and the resistances in those paths and how you can keep the resistances from working against you.

An illustration:
Suppose you have three connection points in a row, called A, B and C. There is a resistance of 1 ohm between A and B and another 1 ohm between B and C. You can't do anything to reduce the resistance, you just have to accept it. You have a circuit board with your microcontroller and ADC on it and a "ground" wire from it. Let's say the board requires 30 mA to run, so it puts 50 mA through its ground wire. Its ground wire is the "zero volt" reference point for the ADC (we'll assume no voltage drop across that wire). Also on your board you have 5 outputs, each of which supplies 10 mA to an LED (current flows from the outputs, into the anodes the LEDs and has to get from the cathodes back to the solar panel negative by some external path. You have a solar panel whose voltage you want to measure.

If you connect the solar panel negative to A, the circuit board to B and the LED cathodes to C, you'll have 50 mA flowing through the BC resistor due to the LEDs and 80 mA through the AB resistor - the 30 mA for the board plus the 50 mA for the LEDs, so there will be an 80 mV drop between A and B and your reading of the panel voltage will have considerable error. If you rearrange the way you use the points and put the LEDs on A, the panel on B and the board on C, now only the 30 mA of the board contributes to the voltage drop between the panel and the board. The 50 mA for the LEDs comes in from the "other side" and doesn't flow through the BC resistor. Your error has been reduced from 80 mV to 30 mV just by a small rearrangement of the current paths.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hello guys im back here again.
I have built a simple circuit to test my INA219 chip again. All I did is conenct INA219 to my arduino and supply and 4 LEDS turning ON with 5 seconds delay to give me enough time to take measurements . I am trying to read my supply voltage.

I have used 8V supply (7.98V measured with DMM)

Still, I was not able to identify where the voltage error is coming. When 90mA of current is flowing, the reading I get is 7.92V. The voltage drop across shunt is only 9mV.

As suggested above, I have measured voltage drops across the supply GND and other ground connections and I was only getting about 2-3mV on all points. So where did I loose the rest of the voltage from 7.98 to 7.92 ? :eek:
 

Attachments

Thread Starter

zazas321

Joined Nov 29, 2015
936
I have added my LCD to the circuit above. The value is still 7.92V at 98mA so its good news, not increasing
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
I have added my voltage regulators, the voltage is still 7.92V. Now I have exactly same circuit as I had before, but the error is much smaller!
 
Top