Projects / USB current and energy meter

USB current and energy meter

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANOUSBMICROUSB

    Wire the power path

    Micro-USB breakout VBUS to VIN+, VIN- to the USB-A socket's VBUS and join the grounds.

  2. NANOUSB

    Pass the data lines

    Connect D+ and D- straight from one breakout to the other.

  3. NANOINA2191.01 ASSD1306

    Wire the I2C devices

    INA219 and OLED share A4 and A5.

  4. NANOUSBMICROUSB

    Power the Nano separately

    A different USB cable into the Nano.

  5. NANOUSBMICROUSB

    Plug in a load

    Connect a charger to the micro-USB side and a phone or a USB fan to the USB-A socket.

  6. NANOINA2191.01 A

    Read

    Voltage, current and the running totals appear.

  7. NANOUSBMICROUSB

    Check against a known meter

    Compare with a USB power meter or a multimeter in series.

In short

How do you build an USB current and energy meter?

An INA219 measures voltage, current and mAh of anything you plug into USB. You need Arduino Nano, INA219 Current Monitor, USB-A female breakout, Micro USB breakout board, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data). It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Micro-USB breakout VBUS (from the charger)INA219 VIN+Source side of the shunt
INA219 VIN-USB-A female VBUSLoad side of the shunt, where the device plugs in
Micro-USB breakout GNDUSB-A female GND and Nano GNDCommon ground
D+ and D- of both breakoutsJoined straight throughSome phones need them to charge fast
INA219 VCC and GNDNano 5V and GNDSensor supply
INA219 SDA and SCLNano A4 and A5I2C, address 0x40
OLED SDA and SCLNano A4 and A5Shared bus, address 0x3C
Nano powerSeparate USB cableDo not take its power from the measured line

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "Adafruit INA219", "Adafruit SSD1306" and "Adafruit GFX Library". With the default 0.1 Ω shunt it reads up to about 3.2 A at best resolution of about 1 mA. Totals reset when the Nano restarts. Treat readings as a guide, since the shunt drops about 0.1 V per amp.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_INA219 ina;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float mAh = 0, mWh = 0;
unsigned long last = 0;

void setup() {
  ina.begin();                                  // default range: 32 V, 2 A, 0.1 ohm shunt
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
  last = millis();
}

void loop() {
  float busV = ina.getBusVoltage_V();           // voltage on the load side
  float mA = ina.getCurrent_mA();
  unsigned long now = millis();
  float hours = (now - last) / 3600000.0;
  last = now;
  if (mA > 0) { mAh += mA * hours; mWh += busV * mA * hours; }

  oled.clearDisplay();
  oled.setTextSize(2); oled.setCursor(0, 0);  oled.print(busV, 2); oled.print(" V");
  oled.setCursor(0, 20); oled.print(mA, 0); oled.print(" mA");
  oled.setTextSize(1);
  oled.setCursor(0, 44); oled.print(mAh, 1); oled.print(" mAh");
  oled.setCursor(0, 54); oled.print(mWh, 1); oled.print(" mWh");
  oled.display();
  delay(250);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Nothing plugged inAbout 5 V and 0 mA
USB fanA few hundred mA
Phone chargingUp to 1 to 2 A depending on the charger
Compare with a multimeterWithin about 3 percent

If it does not work

Shows 0 mA

The load is on the wrong side, or VIN+ and VIN- are swapped. Current flows from VIN+ to VIN-.

Voltage low under load

The shunt and thin wires drop voltage. Use short, thick wires.

Phone does not fast-charge

Data lines are not joined or the charger needs a negotiation chip.

Display blank

Check the I2C addresses 0x40 and 0x3C and wiring.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the USB current and energy meter?

Arduino Nano, INA219 Current Monitor, USB-A female breakout, Micro USB breakout board, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data).

How long does the USB current and energy meter take to build?

About 60 minutes for a beginner maker, in 7 steps.

How do I know the USB current and energy meter works?

Nothing plugged in: About 5 V and 0 mA. USB fan: A few hundred mA. Phone charging: Up to 1 to 2 A depending on the charger.

Shows 0 mA: what should I check?

The load is on the wrong side, or VIN+ and VIN- are swapped. Current flows from VIN+ to VIN-.

Voltage low under load: what should I check?

The shunt and thin wires drop voltage. Use short, thick wires.

Phone does not fast-charge: what should I check?

Data lines are not joined or the charger needs a negotiation chip.

Display blank: what should I check?

Check the I2C addresses 0x40 and 0x3C and wiring.