USB Power Meter
A bench power monitor built on the INA219 high-side sensor: every second the sketch reads the bus voltage, load current and power over I2C, integrates power into a running energy counter (mWh), tracks the peak current, and redraws an OLED dashboard with volts and milliamps in large digits. When the load pulls more than 800 mA an OVERLOAD banner appears and the red LED on GPIO 4 lights — and because the emulated supply has realistic source resistance you can watch the bus voltage sag as the current climbs. The button on GPIO 14 resets the energy and peak counters. In the Lab, click the INA219 module to reveal a 0–2000 mA load slider and try tripping the overload alarm yourself.
// USB Power Meter - live volts, milliamps, milliwatts and energy from an INA219
// high-side monitor on I2C. The red LED warns when the load draws more than
// 800 mA, and the button resets the energy (mWh) and peak-current counters.
// In the Lab, click the INA219 module to reveal a load-current slider.
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_INA219 ina219;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const int LED_PIN = 4;
const int BTN_PIN = 14;
const float ALARM_MA = 800.0;
float energy_mWh = 0.0;
float peak_mA = 0.0;
unsigned long lastMs = 0;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
if (!ina219.begin()) {
Serial.println("INA219 not found");
while (true) delay(100);
}
ina219.setCalibration_32V_2A();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
Serial.println("USB Power Meter ready");
lastMs = millis();
}
void loop() {
float busV = ina219.getBusVoltage_V();
float mA = ina219.getCurrent_mA();
float mW = ina219.getPower_mW();
// integrate energy: mW x hours = mWh
unsigned long nowMs = millis();
float hours = (nowMs - lastMs) / 3600000.0;
energy_mWh += mW * hours;
lastMs = nowMs;
if (mA > peak_mA) peak_mA = mA;
if (mA > ALARM_MA) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
if (digitalRead(BTN_PIN) == LOW) {
energy_mWh = 0.0;
peak_mA = 0.0;
Serial.println("Counters reset");
}
Serial.print("V=");
Serial.print(busV, 2);
Serial.print(" I=");
Serial.print(mA, 0);
Serial.print("mA P=");
Serial.print(mW, 0);
Serial.print("mW peak=");
Serial.print(peak_mA, 0);
Serial.print(" E=");
Serial.print(energy_mWh, 2);
Serial.println("mWh");
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("USB POWER METER");
display.setTextSize(2);
display.setCursor(0, 14);
display.print(busV, 2);
display.print("V");
display.setCursor(0, 32);
display.print(mA, 0);
display.print("mA");
display.setTextSize(1);
display.setCursor(0, 52);
display.print("P ");
display.print(mW, 0);
display.print("mW E ");
display.print(energy_mWh, 1);
display.print("mWh");
if (mA > ALARM_MA) {
display.setCursor(80, 0);
display.print("OVERLOAD");
}
display.display();
delay(1000);
}
Built with FluxBench.AI — the AI workbench for embedded projects.