Using two current sensors (ina219), how to write the code?

Hello, i have trouble making my ina219 current sensors work. One of them works, the other one just stays permanently at -0,05 no matter what i do here are pictures to showcase it:
image

The way i connect them to arduino UNO is this:
ina219 #1: scl -> A5
ina219 #1: sda -> A4
ina219 #2: scl -> A3
ina219 #2: sda -> A2

The code is here:

#include <ZMPT101B.h>
#include <Adafruit_INA219.h>

// definování adresy senzoru
#define ADDR 0x40
// inicializace senzoru s nastavenou adresou z knihovny
Adafruit_INA219 ina219_5(0x40);
Adafruit_INA219 ina219_12(0x41);

int state = 1;
int lastState = -1; // Uložíme si poslední stav

void setup() {
  Serial.begin(19200);
  ina219_5.begin();
  ina219_12.begin();

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);

  ina219_5.setCalibration_16V_400mA();
  ina219_12.setCalibration_16V_400mA();
}

void loop() {
  if (Serial.available() > 0) {
    int receivedValue = Serial.parseInt();
    if (receivedValue >= 1 && receivedValue <= 7) {
      state = receivedValue;
      Serial.print("Received state: ");
      Serial.println(state); // Výpis přijatého stavu
    }
  }

  if (state != lastState) { // Kontrola, zda se stav změnil
    Serial.print("Changing state to: ");
    Serial.println(state); // Výpis změny stavu
    switch (state) {
      case 1: set200z(); break;
      case 2: set400z(); break;
      case 3: set600z(); break;
      case 4: set0r(); break;
      case 5: set1r(); break;
      case 6: set2r(); break;
      case 7: set3r(); break;
    }
    lastState = state; // Aktualizace posledního stavu
  }
  
  readVoltage();
  delay(5); // Zpoždění 5 ms pro rychlé měření
}

void set200z() {
  Serial.println("Setting 200z"); // Debug výpis
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
}

void set400z() {
  Serial.println("Setting 400z"); // Debug výpis
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
}

void set600z() {
  Serial.println("Setting 600z"); // Debug výpis
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
}

void set0r() {
  Serial.println("Setting 200zr"); // Debug výpis
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
}

void set1r() {
  Serial.println("Setting 200zr"); // Debug výpis
  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
}

void set2r() {
  Serial.println("Setting 400zr"); // Debug výpis
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH);
}

void set3r() {
  Serial.println("Setting 600zr"); // Debug výpis
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
}

void readVoltage() {
  static unsigned long lastChrono;
  unsigned long currentChrono = millis();
  if (currentChrono - lastChrono >= 1) { // ~100Hz
    Serial.print("Uin:");
    Serial.print((230 * (analogRead(A0) - 512)) / 512);
    Serial.print(",");
    Serial.print("Uout:");
    Serial.print((230 * (analogRead(A1) - 512)) / 512);
    Serial.print(",");
    Serial.print("proud1:");
    Serial.print(ina219_5.getCurrent_mA());
    Serial.print(",");
    Serial.print("proud2:");
    Serial.print(ina219_12.getCurrent_mA());
    Serial.println("");
    lastChrono = currentChrono;
  }
}

The INA219 is indended for DC not AC.
You need the ACS712 sensor.

So there is no way of doing this without this sensor right? Why does it work fine for the first one though?

Does it actually work fine? Is the frequency, phase and amplitude correct?

Even if they could measure AC you have them wired wrong and your code is totally wrong.
Best to start over with the correct sensor and do it right.
What are the range of voltages and currents you want to measure?

Plenty of good information here:

I've heard from multiple sources that they can be used for AC as well. Can you tell me what is wrong about the wiring?

Regarding the code: I know that some parts have redundant and janky code, it's a 3 years old project i have to continue on now, so i'm building on something i was able to do back then, which wasn't very good, i'm intending to improving the code but if you got any tips or see something alarmingly wrong, please tell.

However the main concern is making the second sensor work.

Voltages and currents: the sine source is an output from transformator 12V and 2A

If you would, please explain how hooking the INA219's SCL and SDA I2C signals up to analog inputs rather than the Uno's SCL and SDA pins is supposed to work.

Please post a complete schematic, the diagram in your OP helps a bit, but it's not a schematic and doesn't show how everything is wired. I could guess what you have, but that's a waste of my time and useless to you.

The idea that you can select 2 different frequencies from different tappings on a transformer is, to put it politely, interesting.

1 Like

I've made a mistake, i'm connecting them like this:

ina219 #1: scl -> A5
ina219 #1: sda -> A4
ina219 #2: scl -> A3
ina219 #2: sda -> A2

A5 and A4 should be fine, however the second one connected to A3 and A2 is as i see now wrong. So i should hook them up somewhere else right?

I look forward to seeing a complete schematic.

1 Like

If an Uno, both should be connected to A4 and A5.

Did you set the address on one ina219 to a different i2c address? Won't work unless you do.

Edit: After changing the i2c address, runt he i2c_scanner sketch to ensure you know what addresses they have.

1 Like

yes i'm doing this:

Adafruit_INA219 ina219_5(0x40);
Adafruit_INA219 ina219_12(0x41);

which should create instances of ina219 with different addresses right? However the output from i2c_scanner from Arduino Playground - I2cScanner

outputs only this:

I2C device found at address 0x40  !
done

I have tried to connect both ina sensors to A5 and A4 now but it has the same result

That is in your code. You must physically change the i2c address on the ina219 board. There should be 2 sets of pads labeled A0 and A1.

1 Like

Did you change the address jumpers on the second breakout board as indicated in Adafruit's tutorial?

1 Like

ohh i have no idea i could do that so i think that's most likely the problem.

I haven't soldered anything so that means they share the same i2c address right now right?

Okay i haven't tested it out yet, but rn when i test the sensors seperately they work, when i test them at once only one of them is displayed on plotter, so i guess my error is this. Thank you so much!

That is correct. That is why I suggest checking the new i2c addresses with the i2c_scanner when you are done changing, just to ensure they ended up where you thought.

I'll be able to test this out tommorrow but i think this might work cause they both unsoldered. Thank you so much!

Well then I suggest you ask those multiple sources how to connect them and write the code.
However, If you read the datasheet you will soon realize that they cannot measure AC.
They were never intended to measure AC and no one uses them to measure AC

Hey i didn't mean it to sound like other people know better than you. I'm not trying to argue with you, certainly not with people that are trying to help in their free time so sorry if it came out that way, i was just stating - my professor told me it should be fine to use this so i did and frankly i don't have any other option now, so i guess i just gotta roll with it now.

It's just odd to me because if you would use diode bridge, suddenly it should be completely fine to use. But at the same time ina219 is able to read negative and positive at the same time so it's making me confused.

If the prof said it was OK and you just need to plot some point on a graph then it's OK. However, if you need to make accurate measurements of peak, rms, average values or phase and frequency measurements you may find the code difficult or impossible to write.

1 Like