INA219 & Arduino Based Wattmeter

by Lithium-ion in Circuits > Arduino

1205 Views, 3 Favorites, 0 Comments

INA219 & Arduino Based Wattmeter

mini_thumb.png

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

Supplies

mini_IMG_0650.png

Components Required:

  • INA219 Current sensor
  • 12C OLED display
  • ARDUINO Nano/UNO
  • Jumper wires
  • Battery power supply
  • Custom PCB from PCBWAY

Story:

mini_IMG_0649.png

Another Wattmeter project is here, but this time everything is controllable digitally. We are using the INA219 as a digital device that allows the measurement of current, voltage, and therefore power. Simply, connect wire serial through this device and it will give all the information provided via I2C communication. It is very simple and reliable to use. It has a jumper for making changes to the I2C address so you can connect more than one INA219 to a single microcontroller.

The maximum current of the measurement is determined by the shunt resistor found on the plate which is 0.1ohm (± 3.2A). Yes, it can measure the power in both directions! If you want to measure other currents or smaller currents more precisely, check out the datasheet to change that resistor. The maximum voltage that can be measured is 26V.

INA219 Sensor:

mini_ina219 image.png

This INA219 current sensor comes with a lot of modes and configuration settings which can be adapted as per requirements. With this Digital IC we got precision over a simple shunt amplifier. It also surpasses the ADC resolution of Arduino. I have a unique Arduino board. I designed my own PCB. You can order the same from PCBWAY for just in $5 for 10 PCB.

Features:

Supply voltage to INA219 : 3V - 5V

Maximum measuring voltage: 26V

Maximum measuring current: 3.2A

12 bit ADC with 4 modes

2 Voltage modes

16 different I2C Addresses

Setting Up the I2C Address:

ice_screenshot_20230924-214136.png
ice_screenshot_20230924-190341.png

In the datasheet it is defined that we can use this sensor with 16 different addresses. The connection pads are given on the PCB which can be soldered together using those addresses. Usually only 4 addresses are used but here is the full table you can see.

INA219_I2C_ADDRESS1  0x40   A0 = 0  A1 = 0
INA219_I2C_ADDRESS2 0x41 A0 = 1 A1 = 0
INA219_I2C_ADDRESS3 0x44 A0 = 0 A1 = 1
INA219_I2C_ADDRESS4 0x45 A0 = 1 A1 = 1

To configure this INA219 Power sensor we have to define the proper working mode, Range, Gain configurations, voltage (BUS) and current(Shunt) resolution. So here in the code the settings can be edited as per configurations mentioned below in the different tables.

ina.configure(INA219_RANGE_32V, INA219_GAIN_320MV, INA219_BUS_RES_12BIT, INA219_SHUNT_RES_12BIT_1S);


Configuration Settings:

Modes:
INA219_MODE_POWER_DOWN: Power-Down, Quiescent mode
INA219_MODE_SHUNT_TRIG: Shunt Voltage, Triggered on event
INA219_MODE_BUS_TRIG: Bus Voltage, Triggered on event
INA219_MODE_SHUNT_BUS_TRIG: Shunt and Bus, Triggered on event
INA219_MODE_ADC_OFF: ADC Off, stop all conversion
INA219_MODE_SHUNT_CONT: Shunt Voltage, Continuous
INA219_MODE_BUS_CONT: Bus Voltage, Continuous
INA219_MODE_SHUNT_BUS_CONT: Shunt and Bus, Continuous


Range:
INA219_RANGE_16V: 16V
INA219_RANGE_32V: 32V


Gain:
INA219_GAIN_40MV: +/- 40mV
INA219_GAIN_80MV: +/- 80mV
INA219_GAIN_160MV: +/- 160mV
INA219_GAIN_320MV: +/- 320mV
Bus Resolution:
INA219_BUS_RES_9BIT: 9-bit
INA219_BUS_RES_10BIT: 10-bit
INA219_BUS_RES_11BIT: 11-bit
INA219_BUS_RES_12BIT: 12-bit


Shunt resolution:
INA219_SHUNT_RES_9BIT_1S: 9-bit / 1 sample
INA219_SHUNT_RES_10BIT_1S: 10-bit / 1 sample
INA219_SHUNT_RES_11BIT_1S: 11-bit / 1 sample
INA219_SHUNT_RES_12BIT_1S: 12-bit / 1 sample
INA219_SHUNT_RES_12BIT_2S: 12-bit / 2 samples
INA219_SHUNT_RES_12BIT_4S: 12-bit / 4 samples
INA219_SHUNT_RES_12BIT_8S: 12-bit / 8 samples
INA219_SHUNT_RES_12BIT_16S: 12-bit / 16 samples
INA219_SHUNT_RES_12BIT_32S: 12-bit / 32 samples
INA219_SHUNT_RES_12BIT_64S: 12-bit / 64 samples
INA219_SHUNT_RES_12BIT_128S: 12-bit / 128 samples


Circuit Diagram:

CIRCUIT.png

The circuit is quite simple, only 5v is required to power on the sensor and microcontroller. The sensor and OLED both are connected to the I2C port of Arduino Nano. Both have different default addresses so no need to change the address. You can pair almost a maximum of 127 different devices to the I2C port. Follow the schematic pictorial diagram given above for the connections.

External power supply 0-30v is required to measure the voltage and current through the INA219 sensor. External load is required and the connections for connection the external power supply and load is given above. Download all the library and code files form here.

Important Info About Internal Registers:

I almost read the whole datasheet and analyze each and every part, to measure the current and voltage different approaches can be used and here the same shunt feedback with differential input is used. There are 6 - 16 bit registers where all the conversions and computations happen.

Configuration

Voltage drop across Shunt read/write register

Voltage drop to Current conversion read register

Voltage measuring with 4mv max resolution read register

Power = current*voltage read register

Calibration read/write register

The shunt will drop some voltage across it which then is amplified with an internal differential amplifier by selecting the gain settings from configurations. By default a gain setting of +/- 320mV is there which converts the voltage in bits arranging from 0-15 in register number 2.

Now this voltage drop is converted into the current readings by multiplying the above voltage drop with a calibration register value.

Calibration register is very important to program because it is the one which operates the whole system. You can find more about programming this register in the datasheet.

Voltage reading is simple, just select the mode 32/16v and the voltage register gets the respective value by automatically adjusting the resolution to 4mv.

Power is calculated by multiplying the current and voltage register values.

My Arduino Clone:

thubn.png

I made this Arduino Nano compatible board which has USB type C and a compatible USB to TTL driver unit. You can access the full tutorial from here. I made the schematics in Altium designer and then uploaded the exported gerber files on PCBWAY for fabrication.

PCBWAY is the leading PCB manufacturer from CHINA, PCBway deals in a huge range of prototyping services including PCBA, SMT assembly, RF PCB, Precision PCB, 3d METAL, CNC and Stencil. Sign-up now using this link and get free PCB coupons (valid for new users only).

Code:

ice_screenshot_20230924-190533.png
mini_IMG_0637.png

I divided the main program into two parts, first will check the configuration settings named as configuration program and the second named as Main program will show the real time power readings on the OLED display attached to I2C port. Download all the library and code files form here.

Main Program:


// 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);
}


Working and Testing:

mini_IMG_0642.png
mini_IMG_9782.png

The program is working fine, I attached the external power supply just to confirm the power ratings. And they are almost matching with each other. This INA219 sensor gives a linear response over a range of 26 volts. This sensor is best for measuring low power electronics because of having a better ADC feature this thing can measure the lower current very accurately and precisely. Don't forget to order your amazing PCB designs from PCBWAY.