Lithium ION
Published © GPL3+

INA219 & Arduino based Wattmeter

Small digital voltage and current detection sensor with ultimate 12 bit precision and features. Made your Wattmeter with flexibility.

BeginnerFull instructions provided1.2 hours328
INA219 & Arduino based Wattmeter

Things used in this project

Software apps and online services

PCBWAY

Story

Read more

Custom parts and enclosures

Library

Schematics

Datasheet

Code

Configuration code

Arduino
// Modified by Sagar saini

#include "INA219-SOLDERED.h"  // Download the library : https://github.com/SolderedElectronics/Soldered-INA219-Current-Sensor-Arduino-Library

INA219 ina;

// Function to check sensor setup

void checkConfig()
{
    Serial.print("Mode:                 ");//Print on display in which mode is sensor
    switch (ina.getMode())
    {
    case INA219_MODE_POWER_DOWN:
        Serial.println("Power-Down");
        break;
    case INA219_MODE_SHUNT_TRIG:
        Serial.println("Shunt Voltage, Triggered");
        break;
    case INA219_MODE_BUS_TRIG:
        Serial.println("Bus Voltage, Triggered");
        break;
    case INA219_MODE_SHUNT_BUS_TRIG:
        Serial.println("Shunt and Bus, Triggered");
        break;
    case INA219_MODE_ADC_OFF:
        Serial.println("ADC Off");
        break;
    case INA219_MODE_SHUNT_CONT:
        Serial.println("Shunt Voltage, Continuous");
        break;
    case INA219_MODE_BUS_CONT:
        Serial.println("Bus Voltage, Continuous");
        break;
    case INA219_MODE_SHUNT_BUS_CONT:
        Serial.println("Shunt and Bus, Continuous");
        break;
    default:
        Serial.println("unknown");
    }

    Serial.print("Range:                ");//Print measuring range
    switch (ina.getRange())
    {
    case INA219_RANGE_16V:
        Serial.println("16V");
        break;
    case INA219_RANGE_32V:
        Serial.println("32V");
        break;
    default:
        Serial.println("unknown");
    }

    Serial.print("Gain:                 ");//Print gain of measuring
    switch (ina.getGain())
    {
    case INA219_GAIN_40MV:
        Serial.println("+/- 40mV");
        break;
    case INA219_GAIN_80MV:
        Serial.println("+/- 80mV");
        break;
    case INA219_GAIN_160MV:
        Serial.println("+/- 160mV");
        break;
    case INA219_GAIN_320MV:
        Serial.println("+/- 320mV");
        break;
    default:
        Serial.println("unknown");
    }

    Serial.print("Bus resolution:       ");//Print bus resolution
    switch (ina.getBusRes())
    {
    case INA219_BUS_RES_9BIT:
        Serial.println("9-bit");
        break;
    case INA219_BUS_RES_10BIT:
        Serial.println("10-bit");
        break;
    case INA219_BUS_RES_11BIT:
        Serial.println("11-bit");
        break;
    case INA219_BUS_RES_12BIT:
        Serial.println("12-bit");
        break;
    default:
        Serial.println("unknown");
    }

    Serial.print("Shunt resolution:     ");//Print shunt resolution
    switch (ina.getShuntRes())
    {
    case INA219_SHUNT_RES_9BIT_1S:
        Serial.println("9-bit / 1 sample");
        break;
    case INA219_SHUNT_RES_10BIT_1S:
        Serial.println("10-bit / 1 sample");
        break;
    case INA219_SHUNT_RES_11BIT_1S:
        Serial.println("11-bit / 1 sample");
        break;
    case INA219_SHUNT_RES_12BIT_1S:
        Serial.println("12-bit / 1 sample");
        break;
    case INA219_SHUNT_RES_12BIT_2S:
        Serial.println("12-bit / 2 samples");
        break;
    case INA219_SHUNT_RES_12BIT_4S:
        Serial.println("12-bit / 4 samples");
        break;
    case INA219_SHUNT_RES_12BIT_8S:
        Serial.println("12-bit / 8 samples");
        break;
    case INA219_SHUNT_RES_12BIT_16S:
        Serial.println("12-bit / 16 samples");
        break;
    case INA219_SHUNT_RES_12BIT_32S:
        Serial.println("12-bit / 32 samples");
        break;
    case INA219_SHUNT_RES_12BIT_64S:
        Serial.println("12-bit / 64 samples");
        break;
    case INA219_SHUNT_RES_12BIT_128S:
        Serial.println("12-bit / 128 samples");
        break;
    default:
        Serial.println("unknown");
    }

    Serial.print("Max possible current: ");
    Serial.print(ina.getMaxPossibleCurrent());//Print maximum current
    Serial.println(" A");

    Serial.print("Max current:          ");
    Serial.print(ina.getMaxCurrent());//Print maximum measurable current
    Serial.println(" A");

    Serial.print("Max shunt voltage:    ");
    Serial.print(ina.getMaxShuntVoltage());//Print max voltage on shunt that can be read
    Serial.println(" V");

    Serial.print("Max power:            ");
    Serial.print(ina.getMaxPower());//Print max power that can be read
    Serial.println(" W");
}

void setup()
{
    Serial.begin(115200);

    Serial.println("Initialize INA219");

    // Default INA219 address is 0x40
    ina.begin();

    // Configure INA219
    ina.configure(INA219_RANGE_32V, INA219_GAIN_320MV, INA219_BUS_RES_12BIT, INA219_SHUNT_RES_12BIT_1S);

    // Calibrate INA219. Rshunt = 0.1 ohm, Max excepted current = 2A
    ina.calibrate(0.1, 2);

}


void loop()
{
  Serial.println("Configuration Settings: ");
  Serial.println("-----------------------------------------------");
  checkConfig();
  Serial.println("-----------------------------------------------");
  delay(1000);
}

MAIN CODE

Arduino
// Modified by Sagar saini

#include "INA219-SOLDERED.h"  // Library : https://github.com/SolderedElectronics/Soldered-INA219-Current-Sensor-Arduino-Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

INA219 ina;
#define OLED_RESET 1
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);


//Configure the sensor first
void setup()
{
    Serial.begin(9600);  //Initialize the serial port
    ina.begin(); // Default INA219 address is 0x40
    ina.configure(INA219_RANGE_32V, INA219_GAIN_320MV, INA219_BUS_RES_12BIT, INA219_SHUNT_RES_12BIT_1S); //Configure settings check configuration program
    ina.calibrate(0.1, 2); // Calibrate INA219. Rshunt = 0.1 ohm, Max excepted current = 2A
    Wire.begin();
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();
}

// Read values in loop
void loop()
{
    Serial.print("Bus voltage:   ");
    Serial.print(ina.readBusVoltage(), 5);  //Read bus voltage ,5 means upto 5 decimal places
    Serial.println(" V");

    Serial.print("Bus power:     ");
    Serial.print(ina.readBusPower(), 5);  //Read bus power
    Serial.println(" W");


    Serial.print("Shunt voltage: ");
    Serial.print(ina.readShuntVoltage(), 5);  //Read shunt voltage
    Serial.println(" V");

    Serial.print("Shunt current: ");
    Serial.print(ina.readShuntCurrent(), 5);  //Read shunt current
    Serial.println(" A");
    Serial.println("");
 
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("WattMeter:");
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,25);
  display.print("Voltage:");
  display.setCursor(50,25);
  display.print(ina.readBusVoltage(),5);
  display.display();
    
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,35);
  display.print("Current:");
  display.setCursor(50,35);
  display.print(ina.readShuntCurrent(),5);
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,45);
  display.print("Power:");
  display.setCursor(50,45);
  display.print(ina.readBusPower(),5);
  display.display();
    
    delay(1000);
}

Credits

Lithium ION
53 projects • 35 followers
A passionate electronics DIY boy. Currently improving in Embedded systems, soldering and programming.

Comments