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

**URL:** <https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312>\
**Category:** Programming\
**Created:** [July 1, 2024, 4:58pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312 "2024-07-01T16:58:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 4:58pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/1 "2024-07-01T16:58:13Z")

</div>

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](https://europe1.discourse-cdn.com/arduino/original/4X/d/b/f/dbf3ff27d92d72ff460db82fdc8885ea918aea1d.png)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e08eee2cda308b05bb5c1ac9c6242cac913628f.png)

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:

```arduino
#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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [July 1, 2024, 5:19pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/2 "2024-07-01T17:19:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 5:40pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/3 "2024-07-01T17:40:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [July 1, 2024, 5:46pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/4 "2024-07-01T17:46:19Z")

</div>

> [@bergerino](#):
>
> Why does it work fine for the first one though?

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

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [July 1, 2024, 5:50pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/5 "2024-07-01T17:50:50Z")

</div>

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:

> **[GitHub - RobTillaart/ACS712: Arduino library for ACS Current Sensor - 5A,...](https://github.com/RobTillaart/ACS712)**
>
> Arduino library for ACS Current Sensor - 5A, 20A, 30A - RobTillaart/ACS712

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 6:10pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/6 "2024-07-01T18:10:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [July 1, 2024, 6:19pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/7 "2024-07-01T18:19:02Z")

</div>

> [@bergerino](#):
>
> The way i connect them to arduino UNO is this:  
> ina219 #1: scl -\> A0  
> ina219 #1: sda -\> A1  
> ina219 #2: scl -\> A2  
> ina219 #2: sda -\> A3

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.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [July 1, 2024, 6:25pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/8 "2024-07-01T18:25:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 6:29pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/9 "2024-07-01T18:29:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [July 1, 2024, 6:30pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/10 "2024-07-01T18:30:46Z")

</div>

I look forward to seeing a complete schematic.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [July 1, 2024, 6:32pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/11 "2024-07-01T18:32:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 6:44pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/12 "2024-07-01T18:44:22Z")

</div>

yes i'm doing this:

```arduino
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](https://playground.arduino.cc/Main/I2cScanner/)

outputs only this:

```arduino
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

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [July 1, 2024, 6:46pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/13 "2024-07-01T18:46:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [July 1, 2024, 6:48pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/14 "2024-07-01T18:48:50Z")

</div>

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

> **[Adafruit INA219 Current Sensor Breakout](https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/pinouts#i2c-addresses-based-on-jumpers-3153848)**
>
> The INA219B breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can use this breakout to measure both the high side voltage and DC current draw over I2C with 1% precision.

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 6:58pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/15 "2024-07-01T18:58:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [July 1, 2024, 7:01pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/16 "2024-07-01T19:01:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 7:05pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/17 "2024-07-01T19:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [July 1, 2024, 7:07pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/18 "2024-07-01T19:07:26Z")

</div>

> [@bergerino](#):
>
> I've heard from multiple sources that they can be used for AC as well.

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

---

<div class="post-metadata">

**Author:** ![bergerino](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bergerino](https://forum.arduino.cc/u/bergerino)\
**Post date:** [July 1, 2024, 7:44pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/19 "2024-07-01T19:44:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [July 1, 2024, 7:53pm UTC](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312/20 "2024-07-01T19:53:41Z")

</div>

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.

[Next page](https://forum.arduino.cc/t/using-two-current-sensors-ina219-how-to-write-the-code/1277312.md?page=2)
