#include <Wire.h>
#include <INA226_WE.h>

// The default I2C address for most INA226 modules is 0x40
#define I2C_ADDRESS 0x40

// Create the INA226 object - you need to install library INA226_WE by Wolfgang Ewald
INA226_WE ina226 = INA226_WE(I2C_ADDRESS);

void setup() {
  // Set the serial speed
  Serial.begin(115200);
  
  // Wait for the serial port to connect
  while (!Serial) {
      delay(1);
  }

  // Initialize the I2C bus
  Wire.begin();

  // Initialize the INA226
  if (!ina226.init()) {
    Serial.println("Failed to find INA226 chip. Check your wiring!");
    while (1) { delay(10); }
  }
  
  // Note: The INA226_WE library defaults to a 0.1 Ohm shunt resistor and a 3.6A range.
  // This matches 99% of the generic INA226 breakout boards on the market.
}

void loop() {
  float busVoltage_V = 0;
  float current_mA = 0;
  float current_A = 0;

  // Read the bus voltage in Volts (V)
  busVoltage_V = ina226.getBusVoltage_V();
  
  // Read the current in milliamps (mA)
  current_mA = ina226.getCurrent_mA();
  
  // Force the current to always be positive, then convert to Amps (A)
  current_A = abs(current_mA) / 1000.0;

  // Output to serial in the exact format requested with 3 decimal places
  Serial.print("Voltage:");
  Serial.println(busVoltage_V, 3);
  
  Serial.print("Current:");
  Serial.println(current_A, 3);

  delay(100); 
}
