Problem with sharing ground

That's expected, because the power supply is switching on and off quickly, it's like a square wave. It's a PWM power supply and works like that, with a fix voltage PWM signal and changing the duty cycle.
https://www.electronics-tutorials.ws/blog/pulse-width-modulation.html

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.

@anon27210439 , thank you very much for the explanation, perfectly understood.

I will tune the the program to measure the average value and see if it makes any sense...

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."

https://forums.adafruit.com/viewtopic.php?t=152422

So I guess that, apart from averaging redings, I have to increase the rate of readings as high as posible.

Quite a fun task :slight_smile:

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.

@pacholo
Did I miss something?
Are you actually trying to power a motor with a constant voltage LED supply?

That I was think also. I don't know if this would be ok for small motors, if the voltage and everything is in specs.

Yes, I don't see a difference between a constant voltage LED supply and a constant voltage supply.

I also have an adjustable power supply. At least "the feeling" when using them is
indistinguishable

Anyway, today thinking and searching about the project I found this device for regulating the motor:

Probably is a better (or at least cheaper), to use this device plus a cheap power supply than a regulated power supply(LED or not)....

Even with this device, I think I will face the same issues I'm tring to solved(with your help)!!!

Not if you don't use the DIM pin. What that DIM pin does is not well documented.

That DFRobot driver is the same polished turd you get from ebay for $2
Go to the Pololu website for better drivers.
Leo..

The original issue was that you could not have the DIM input grounded.. That problem will be solved with the motor driver.
What else is there?

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.

Hi,
Final schematic and code would help to complete this thread for others looking for such a solution.

Thanks.. Tom. :grinning: :+1: :coffee: :australia:

On wendsday the motor controller will arrive, I will make some test and see witch solution makes more sense..., I'll post here my final thoughts ...

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....

Here you can see the final schematic:

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;
    }
  }

Did you measure that unloaded?
Those ancient L298 drivers can drop 4volt when the motor has to do work.
Leo..

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.

L298 datasheet states a typical volt drop of 2.55volt@1Amp.
Leo..

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..." ?¿

My mum and dad used to call me that, for as long as I can remember :slight_smile:
I think people of my age (70+) find it common courtesy to use real names.
Leo..

ok, thanks, the two dots where confusing me :slight_smile: