Built-in project · Arduino Uno R3

Two rails on an Arduino Uno

An INA226 on a 12 V load and an INA3221 on a 5 V one: each rail’s voltage and current, and the power, on one I2C bus. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Measuring current with a shunt resistor
  • Two I2C devices at different addresses
  • Voltage, current and power on a rail
  • How a lamp's current changes with its model
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 9

How it works

Two power monitors share one I2C bus. An INA226 at address 0x40 watches a 12 V rail feeding a filament lamp. An INA3221 at address 0x41 watches a 5 V rail with its own lamp on channel 1.

Each module has a 0.1 ohm shunt in series with its load and reads the tiny voltage across it, which gives the current. The INA226 also reads the rail voltage on VBUS and works out power.

Every half second the Uno prints one line with the 12 V rail's volts, milliamps and milliwatts, and the 5 V rail's volts and milliamps.

Parts list

9 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Power monitor, INA226 pin SCLArduino Uno R3 pin SCL
Power monitor, INA226 pin SDAArduino Uno R3 pin SDA
Power monitor, INA226 pin GNDArduino Uno R3 pin GND
Three-channel current monitor, INA3221 pin VCCArduino Uno R3 pin 5V
Three-channel current monitor, INA3221 pin GNDArduino Uno R3 pin GND
Three-channel current monitor, INA3221 pin SDAArduino Uno R3 pin A4
Three-channel current monitor, INA3221 pin SCLArduino Uno R3 pin A5
Power monitor, INA226 pin VCC5 V
Power monitor, INA226 pin VBUS12 V
Power monitor, INA226 pin IN+12 V
Filament lamp (1) pin 1Power monitor, INA226 pin IN-
Filament lamp (1) pin 2Ground
Three-channel current monitor, INA3221 pin IN1+5 V
Filament lamp (2) pin 1Three-channel current monitor, INA3221 pin IN1-
Filament lamp (2) pin 2Ground

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Two rails: an INA226 watches a 12 V rail and an INA3221 a 5 V one, each
// with its own load, both on the I2C bus, the way a bench supply or a robot's
// power board keeps an eye on what everything draws.
//
//   INA226 (0x40)    SDA -> SDA   SCL -> SCL   on the 12 V rail's load
//   INA3221 (0x41)   SDA -> A4    SCL -> A5    channel 1 on the 5 V load
//
// Each module carries a 0.1 ohm shunt in series with its load, and reads the
// small voltage across it: the current. The INA226 reads the rail's own
// voltage on VBUS too, and works out the power.
//
// Things to try. Swap a lamp for another model and watch its current follow.

#include <Wire.h>
#include "INA226.h"
#include "Adafruit_INA3221.h"

INA226 rail12(0x40);
Adafruit_INA3221 rail5;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rail12.begin();
  rail12.setMaxCurrentShunt(0.8, 0.1);
  rail5.begin(0x41, &Wire);
  rail5.setShuntResistance(0, 0.1);
  Serial.println("Two rails");
}

void loop() {
  delay(50);
  Serial.print("12V rail ");
  Serial.print(rail12.getBusVoltage(), 2);
  Serial.print(" V ");
  Serial.print(rail12.getCurrent_mA(), 0);
  Serial.print(" mA ");
  Serial.print(rail12.getPower_mW(), 0);
  Serial.print(" mW   5V rail ");
  Serial.print(rail5.getBusVoltage(0), 2);
  Serial.print(" V ");
  Serial.print(rail5.getCurrentAmps(0) * 1000, 0);
  Serial.println(" mA");
  delay(450);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial and I2C. It begins the INA226 and tells it its range with setMaxCurrentShunt(0.8, 0.1), a maximum of 0.8 A across a 0.1 ohm shunt. It begins the INA3221 at 0x41 and sets channel 0's shunt to 0.1 ohm.

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rail12.begin();
  rail12.setMaxCurrentShunt(0.8, 0.1);
  rail5.begin(0x41, &Wire);
  rail5.setShuntResistance(0, 0.1);
  Serial.println("Two rails");
}

loop sketch.ino

Prints one line per pass. For the 12 V rail it reads bus voltage, current in mA and power in mW from rail12. For the 5 V rail it reads voltage and current from rail5 and multiplies amps by 1000 for mA. Delays of 50 ms and 450 ms space the readings.

void loop() {
  delay(50);
  Serial.print("12V rail ");
  Serial.print(rail12.getBusVoltage(), 2);
  Serial.print(" V ");
  Serial.print(rail12.getCurrent_mA(), 0);
  Serial.print(" mA ");
  Serial.print(rail12.getPower_mW(), 0);
  Serial.print(" mW   5V rail ");
  Serial.print(rail5.getBusVoltage(0), 2);
  Serial.print(" V ");
  Serial.print(rail5.getCurrentAmps(0) * 1000, 0);
  Serial.println(" mA");
  delay(450);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • A rail reads zero current This project
    The lamp must be in series with the module: IN+ on the supply, IN- to the lamp, and the lamp's other pin to ground.
  • One module never answers This project
    Check its SDA, SCL and power. The INA226 is at 0x40 and the INA3221 at 0x41, as set in the sketch.
  • The current reads zero or wrong. Power monitor
    Put the load in series with the shunt, and the IN+ and IN- pins across it. Set the calibration register to match the shunt value of 0.1 ohm.
  • The sensor is not found. Power monitor
    The address is set by the A0 and A1 pins, base 0x40. Check SDA, SCL, power and ground. The bus voltage input must stay within the chip's allowed range.
  • The lamp is dim or does not light when driven from a board pin. Filament lamp
    A lamp needs far more current than a pin gives (a pin is limited to about 20 mA). Switch it with a transistor or MOSFET from its own supply that matches the lamp's voltage rating.
  • The lamp's brightness lags or it burns out the switch when it first turns on. Filament lamp
    A cold filament draws several times its running current at first. Size the switch for that surge, not just the steady current.
  • Current reads zero or is wrong. Three-channel current monitor
    The shunt must sit in series with the load. Connect IN+ on the supply side and IN- on the load side. Reading is signed, so swapping them flips the sign.
  • The chip does not answer, or two boards clash. Three-channel current monitor
    Set the address with the A0 pin: GND is 0x40, VS is 0x41, SDA is 0x42, SCL is 0x43. Max bus voltage is 26 V. Use a shared ground.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.