The multimeter shows you the average voltage, because it switches quickly. But the INA219 takes the samples fast, each in a different instant.
You could try to take several samples and average them, 10 or 20.
Read this in Adafruit forum(info coming from developers):
"Depending on the resolution of the output, an INA219 needs between 100us and 600us to take a single reading. It also has a built-in averaging function that collects several readings and calculates the average as a single result. Depending on the number of samples in each set, it can take between 1ms and 70ms to calculate a reading.
The INA219 uses buffered output so you can read it at any time, but you'll get the previous reading if the current one isn't ready yet."
Well, I think that the rate is not the problem. The power supply switches at 1.47KHz, it means that each cycle has a duration of 680uS. If the INA219 needs up to 600uS to take a sample, and it seems that it averages the result, each sample will be different, because it will be not synchronized with the PWM signal.
So you have to average several samples. Maybe it would work. I'm not sure, you have to try.
The rate is not critical, as far as the PWM output is stable during several milliseconds.
Anyway this a not very orthodox way of using the INA219. Otherwise you could sample the PWM signal yourself, high or low, and average it knowing the amplitude.
Or infer the output PWM duty cycle out of your PWM input signal injected to the opto-coupler.
Original issue was solved with an optocoupler, and it was because INA219 and DFR0971 can't not share GND.
When solved, another issue appear, and it was that INA219 fails to measure correct voltage when measuring a circuit with PWM variations.
Yesterday I modify the programing, so to measure average voltage values(avv) insted of "instant voltage values". The results where valid for my case. I leave here the results in case anyone is interest, in every case, I varied the PWM from 0 to 100%:
Measuring 10 times per second( 10 Hz) gives you, in general, enough values so that nearly never you get avv of 0V when more than 0V is applied.
Increasing the rate to 20 Hz gives more consistence results..
When increasing the rate to 50Hz, arduino fails...
In general the avv you get is less than expected (I understand this is normal, as it is PWM, and sometimes you get readings of 0V..), as you increase the control signal, avv gets closer and closer to the "real mean voltage".
In my case, 10 Hz is ok, I don't need more accuracy.
So for me, apart from the point that when the motor driver arrives I expect to get the same results but with less expense the issue is solved.
Thank you very much for the suggestions of everyone and the time taken to help me.
Driver arrived today, I just tested it and it works fine. As I thought, advantages are that you can sustituted the optocoupler and the DFR for the motor controller, plus you don't need a special power supply (don't need to be regulated).
INA219 still needs mean calculations to give more or less acceptable results. I supose you can use a PWM to analog converter to get a more accurate data, but it doesn't seem easy to find a 12 Vdc PWM to 0..5 Vdc analog converter....
The only down side I have found is that max output voltage goes down to 11 V, but tipical power supplies allow to to tune a little bit the output voltage, so not a big deal....
Now you can see a simple code for testing (sorry for the "spanglish"), if you are going to tested it, please visit the wiki about the motor controller, you have to be extra carefull with the " Supply Switching Jumper", you can burn the module or even the PLC if you don't do it the right way.... if measuse voltage and arduino voltage are different always disconnect the jumper.
Thanks again for the help and the suggestions to all of you.
#include <Adafruit_INA219.h>
struct str_medida_ina219{
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float VoltajeMedido = 0;
float power_mW = 0;
float energia;
};
str_medida_ina219 medida_ina219;
Adafruit_INA219 ina219;
unsigned long tiempo_anterior2=millis();
unsigned long tiempo_actual=millis();
const int pin_voltaje_salida_fuente=6;
int measures_per_second=20;
void setup() {
// output to control motor driver
pinMode(pin_voltaje_salida_fuente, OUTPUT);
//Start upo serial
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//Start up INA 219
if (! ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1) { delay(10); }
}
Serial.println("INA219 iniciada.");
}
void loop() {
tiempo_actual=millis();
if (tiempo_actual-tiempo_anterior2 > (1000/measures_per_second)){//Then a tenth of a second has pass since las time, do somethingh
static float tenth_of_a_second=0;
medida_ina219.shuntvoltage = medida_ina219.shuntvoltage + ina219.getShuntVoltage_mV();
medida_ina219.busvoltage = medida_ina219.busvoltage + ina219.getBusVoltage_V();
medida_ina219.current_mA = medida_ina219.current_mA + ina219.getCurrent_mA();
medida_ina219.power_mW = medida_ina219.power_mW + ina219.getPower_mW();
medida_ina219.VoltajeMedido = medida_ina219.busvoltage + (medida_ina219.shuntvoltage / 1000);
tenth_of_a_second= tenth_of_a_second+(10.0/measures_per_second);
if (tenth_of_a_second>=10){ //Then a second has pass, do something (increases motor output by 10 (min=0, max=255))
static float i=0;
i=i+10;
analogWrite(pin_voltaje_salida_fuente, i); //PWM Speed Control
Serial.print("Set to : ");
Serial.print(i/255*100);
Serial.println(" %");
//calculate mean values
medida_ina219.shuntvoltage = medida_ina219.shuntvoltage/measures_per_second;
medida_ina219.busvoltage = medida_ina219.busvoltage/measures_per_second ;
medida_ina219.current_mA = medida_ina219.current_mA/measures_per_second;
medida_ina219.power_mW = medida_ina219.power_mW /measures_per_second;
medida_ina219.VoltajeMedido = medida_ina219.VoltajeMedido/measures_per_second;
//print mean values
Serial.print(" Tensión Medido: ");Serial.print(medida_ina219.VoltajeMedido,1);Serial.print(" V.");
Serial.print(" Tensión Shunt: ");Serial.print(medida_ina219.shuntvoltage,1);Serial.print(" V.");
Serial.print(" Tensión de BUS: ");Serial.print(medida_ina219.busvoltage,1);Serial.print(" V.");
Serial.print(" Intensidad: ");Serial.print(medida_ina219.current_mA,0);Serial.print(" mA.");
Serial.print(" Potencia: ");Serial.print(medida_ina219.power_mW,0);Serial.print(" mW.");
//Reset values
medida_ina219.shuntvoltage = 0;
medida_ina219.busvoltage = 0;
medida_ina219.current_mA = 0;
medida_ina219.power_mW = 0;
medida_ina219.VoltajeMedido =0;
tenth_of_a_second=0;
if (i>255){i=150;}
}
tiempo_anterior2=tiempo_actual;
}
}
No, it's with load, but not under real use.
Max amp for my application is 1 amp. They are supose to be capable of handle 2 amps, so I expect not to have issues with that.
Will see how it behaves on the real application... in a couple of weeks I hope.
You are right Wawa, quite a voltage drop..., anyway I plan to run the system between 12 and 15 Vdc max voltage supply. I have bought a cheap 15Vdc / 2 A/30W power supply, that should be enough. It also admits to tune up the voltage to 18Volts, so I think I should have no issue even when running at 15 volts.
What do you mean signing always your message with "Leo..." ?¿