Measure DC Power with an INA219 and Arduino Uno R3

Created:  
Updated:   04Aug2024   04:03:44   UTC 2024-08-04T04:03:44Z
Rating:   (0 reviewsThis article has not been rated yet)

This tutorial shows how to use the INA219 sensor module to measure current, voltage, and power with the Arduino Uno R3. The INA219 ICIntegrated Circuit, modules (including pinout), Arduino Hookup, Libraries, and code will be covered. An example of measuring current, voltage, and power on a simple resistor circuit is provided to demonstrate how to use the INA219.

INA219 IC

The INA219 ICIntegrated Circuit is a high-side current and voltage monitor chip with an I2CInter-Integrated Circuit. Also referred to as IIC or I2C. interface that operates from a single 3V to 5.5V DC supply. It measures the voltage, current, and power consumption of a circuit using an external shunt resistor. The INA219 IC has a Programmable Gain Amplifier (PGAA Programmable Gain Amplifier (PGA) is an electronic amplifier whose gain can be controlled by external digital or analog signals.) and 12-Bit ADCAnalog-to-Digital Converter (ADC, A/D, or A-to-D) for continuous or trigger operation. It also has programmable calibration, gain, filtering, and ADC resolution. The INA219 can output the following measurements on an external load circuit:

  • Bus Voltage [Volts]
  • Shunt Voltage [Volts]
  • Current [Amps]
  • Power [Watts]

The load supply voltage can also be computed by summing the shunt and bus voltages: VLOAD = VSHUNT + VBUS. The INA219 measures bus voltage on the load side instead of load power supply according to the INA219 Datasheet (PDF). These voltages are shown in the figure below.

INA219 High-Side Current/Voltage Sensing

Inside the INA219 IC the PGA and ADC measures the differential voltage VIN = VIN+ - VIN- across an external shunt resistor. Using internal switching this device monitors both the shunt voltage drop and the bus voltage VBUS = VIN- - GND on an external load. In order to calculate the current and power values, the user must program the resolution of the Current Register and value of the shunt resistor.

INA219 Functional Diagram (Source: Datasheet)

Specs


INA219 Specs
Feature Description
IC Info INA219 High-Side Current/Power Monitor by Texas Instruments (TI)
Voltage Supply (VS) 3.0V to 5.5V single supply
Interface I2C 2-Wire (SCL, SDA)
  • Fast Mode (1kHz to 400kHz)
  • High-Speed Mode (1kHz to 2.56MHz)
Analog Inputs
Differential
(VIN+ - VIN-)
±26V
Analog Inputs
Common-Mode
(VIN+ + VIN-)/2
-0.3V to 26V
ADC Resolution 12-Bit (15-Bit Averaging)
Offset Voltage
  • PGA = 1: 10μV typical, 100μV max
  • PGA = 1/2: 20μV typical, 125μV max
  • PGA = 1/4: 30μV typical, 150μV max
  • PGA = 1/8: 40μV typical, 200μV max
Offset Voltage Drift 0.1μV/°C
Max Shunt Voltage
  • PGA = 1: 40mV
  • PGA = 1/2: 80mV
  • PGA = 1/4: 180mV
  • PGA = 1/8: 320mV
Operating Temperature -40°C to 125°C

PGA Gain & Range

The PGA configuration sets the gain and Full-Scale Range (FSR) of voltage across the shunt resistor. The PGA gain settings in the table below can be found in Table 4 of the INA219 Datasheet.

INA219 PGA Settings
PGA Gain FSR
1 ±40mV
1/2 ±80mV
1/4 ±160mV
1/8 ±320mV

Resolution & Sampling

The ADC resolution settings adjust the number of ADC bits or number of samples used when averaging results for the Bus Voltage Register and Shunt Voltage Register. Higher ADC resolution or sampling requires longer conversion times. The ADC resolution settings in the table below can be found in Table 5 of the INA219 Datasheet.

INA219 ADC Settings
Bit/Samples Conversion Time
9-Bit 84µs
10-Bit 148µs
11-Bit 276µs
12-Bit 532µs
2 Samples 1.06ms
4 Samples 2.13ms
8 Samples 4.26ms
16 Samples 8.51ms
32 Samples 17.02ms
64 Samples 34.05ms
128 Samples 68.10ms

Chip Versions

The INA219 IC comes in a SOIC-8 (3.91mm x 4.90mm) or SOT-23 (1.63mm x 2.90mm) package. There are two versions of the INA219 chip: A and B. The A version has a ±1% total max current measurement error over the entire operating temperature range and the B version has a ±0.5% total max current measurement error over the entire operating temperature range, but the overall functionality is otherwise the same. The markings on the chip indicate whether its the A or B version.

INA219 Modules

Breakout board modules are available for INA219 that have pin through holes with 0.1" (2.54mm) spacing so you can easily attach it to a breadboard and 3.5mm terminal plug mount holes so you can easily connect your load. Most INA219 modules have a 0.1Ω 1% shunt resistor that can measure up to 3.2A with ±0.8mA resolution. The module power supply can be in the range of 3V to 5.5V and the load supply voltage can be in the range of 0V to 26V DC.

There are two common INA219 modules: the Adafruit (PID 904) module and a generic module. Both modules have a 0.1Ω 1% shunt resistor that can measure up to 3.2A with ±0.8mA resolution. The Adafruit board has a small size of 25.6mm x 20.4mm (1.0in x 0.8in) and the generic boards usually have a size about 25.5mm x 22.3mm (1.0in x 0.9in). The main difference between these modules is the Adafruit module has STEMMA QTSTEMMA QT ('cutie') connectors are Adafruit's name for 4-pin JST SH connectors with 1.0mm pitch. These connectors were implemented on Adafruit boards to make it easy to plug-n-play various sensors and devices without soldering and wiring. The STEMMA QT (JST SH) are a smaller connector than the STEMMA (JST PH) that do not fit on smaller boards. STEMMA QT is cross-compatible with SparkFun Qwiic connectors. connectors for I2C and an onboard power LEDLight Emitting Diode.

Adafruit INA219 Module

The Adafruit board comes with a 6-pin header and a 3.5mm terminal plug for the load. On each side of the board are STEMMA QTSTEMMA QT ('cutie') connectors are Adafruit's name for 4-pin JST SH connectors with 1.0mm pitch. These connectors were implemented on Adafruit boards to make it easy to plug-n-play various sensors and devices without soldering and wiring. The STEMMA QT (JST SH) are a smaller connector than the STEMMA (JST PH) that do not fit on smaller boards. STEMMA QT is cross-compatible with SparkFun Qwiic connectors. connectors for supplying power VCC to the INA219 chip and the two I2C lines (SCLSerial Clock (SCL) is the output clock signal line from the master device. Also referred to as SCK, SCLK, or CLK. and SDASerial Data Line ). Adafruit provides an Overview of the board and an Arduino library on GitHub for communicating with the device over I2C.

Adafruit INA219 Module
Adafruit INA219 Board Layout (Top View)
Adafruit INA219 Board Layout (Bottom View)

On the back of the board are A0 and A1 solder jumpers that can be bridged with solder to change the I2C address to the list below.

  • Default = 0x40
  • A0 soldered = 0x41
  • A1 soldered = 0x44
  • A0 and A1 soldered = 0x45

There is also an LED jumper that can be cut to disable the board's power LED if you want to reduce power consumption.

Generic INA219 Module

The Generic INA219 modules are similar to the Adafruit module, with the same functionality, except they do not include the STEMMA QT connectors for I2C and the onboard power LED. A couple other minor differences are the power pin is labeled VCC instead of VIN and the I2C solder jumpers are on the front of the board instead of the back of the board.

Generic INA219 Module
Generic INA219 Board Layout (Top View)
Generic INA219 Board Layout (Bottom View)

Arduino Hookup

To power the INA219 module, connect the Arduino 5V pin to the INA219 VCC and the Arduino GND pin to the INA219 GND. The two I2C communication lines (SCL and SDA) also need to be connected between the INA219 and Arduino.

The external load in this example is a 100Ω resistor that is powered by the Arduino's 5V pin. This will provide a current through the load resistor of 5V/(100Ω + 0.1Ω) ≈ 50mA. You can use a lower value resistor to provide more current, but it's recommended that you stay below 400mA with a minimum load of 12.5Ω to ensure reliable operation and avoid overheating the regulator on the Arduino Uno board.

The current needs to be measured on the high-side, so the 5V pin is connected directly to the INA219 V+ pin and V- is connected to the load resistor and then to ground.

Arduino INA219 Hookup

Arduino Code

The output of the INA219 module are registers that can be read over an I2C digital communication interface by the Arduino using libraries. The two most common libraries are the Adafruit_INA219 and INA219_WE. Both of these libraries can be found and installed the Arduino IDE Library Manager. The Wire Library is also used to communicate over I2C and is already included in the Arduino IDE by default.

The Adafruit library is the easiest to use with three pre-calculated calibration options (32V 2A, 32V 1A, and 16V 400mA) and simple function commands to make measurements. The INA219_WE library has more functionality and control that allows you to set the gain and bus voltage range, choose between continuous or trigger measurement modes, set ADC mode resolution and conversion times, set a correction factor and shunt offset voltage, and check for overflow.

Code examples using both of these libraries will be provided in the following subsections.

Adafruit_INA219 Library

The Adafruit_INA219 library was created by Adafruit for their INA219 module, although the library also works with generic modules. This library can be installed by the Arduino IDE Library Manager and is also available on GitHub. It offers an easy to use interface with pre-calculated calibration options, function calls to read measurement registers, and a power down function. A list of functions available in the Adafruit library are provided below.

Adafruit_INA219 ina219(I2C_Address):
Class constructor to set the I2C address. The default is 0x40.
void begin(void):
Initializes I2C communication address and Wire library with the Adafruit_INA219 device.
void setCalibration_32V_2A():
Highest range setting with a maximum bus voltage of 32V and maximum current of 2A. Uses a PGA gain of 1/8 that has an FSR of 320mV across the shunt resistor. The current LSBThe Least Significant Bit (LSB) in a binary number is the bit that represents the smallest place value carrying the lowest weight. It is also sometimes referred to as the low-order bit or right-most bit due to the convention in positional notation of writing less significant digits further to the right. is 100μA/bit and the power LSB is 2mW/bit. Counter overflow occurs at 3.2767A.
void setCalibration_32V_1A():
Uses a maximum bus voltage of 32V and a slightly lower maximum current of 1A range with higher precision on amps. Uses a PGA gain of 1/8 that has an FSR of 320mV across the shunt resistor. The current LSB is 40μA/bit and the power LSB is 800μW/bit. Counter overflow occurs at 1.31068A.
void setCalibration_16V_400mA():
Uses a lower 16V maximum bus voltage and 400mA maximum current range with higher precision on volts and amps. Uses a PGA gain of 1 that has an FSR of 40mV across the shunt resistor. The current LSB is 50μA/bit and the power LSB is 1mW/bit. Counter overflow occurs at 1.63835A.
float getBusVoltage_V():
Reads the bus voltage in Volts. The bus voltage is across the load: VIN- - GND.
float getShuntVoltage_mV():
Reads the shunt voltage in millivolts.
float getCurrent_mA():
Reads the current through the load in milliamps.
float getPower_mW():
Reads the load power in milliWatts.
void powerSave(bool on):
Sets power save mode by powering down the INA219. Input argument is a boolean value, where True sets the power same mode on and False turns it off.

The Arduino code sketch provided below reads voltage, current, and power from the INA219 over I2C at a sampling rate of 1 second. The load current is expected to be around 50mA, so the calibration is configured to the lowest setting to maximize the resolution: 16V bus voltage and 400mA current range. The output is printed to the serial monitor on the Arduino IDEIntegrated Development Environment (IDE) is a software application that helps develop software code efficiently. over the USB port.

read_ina219_adafruit.ino
              

The output of the measurements from a 5V source through a 0.1Ω shunt and 100Ω load resistor is provided below.

read_ina219_adafruit.ino Output
              

INA219_WE Library

The INA219_WE library was created by Wolfgang Ewald to provide more functionality and control over the INA219. This library can be installed by the Arduino IDE Library Manager and is also available on GitHub. GitHub. Wolfgang Ewald also wrote an https://wolles-elektronikkiste.de/en/ina219-current-and-power-sensor on this library with more details on how it works. A list of functions available in the INA219_WE library are provided below.

INA219_WE INA219_WE(I2C_ADDRESS):
Class constructor to sets the I2C address. If no address is provided INA219_WE(), it sets default I2C address to 0x40.
bool init():
Ensures that the INA219 is reset and then activated with the default values for ADC mode, measurement mode, PGA gain, and bus voltage range. To change these basic settings, you can run the setADCMode(), setMeasureMode(), setPGain(), and setBusRange() functions before the init() call in the setup. If init() returns False, then the INA219 was unable to reset most likely due to the I2C not being connected.
bool reset_INA219():
Writes to the configuration register to reset the INA219.
void setCorrectionFactor(float corr):
Applies a correction factor to the current values if they differ from values measured by calibrated measuring instruments such as a multimeter. The factor is the quotient of the "correct" and the INA219 value.
void setShuntVoltOffset_mV(float offs):
Some INA219 modules tend to show shunt voltage offsets. Although no current is flowing through the load, the output has a small shunt voltage and current. With setShuntVoltOffset() you can correct this by passing the shunt voltage in millivolts, that is measured without current, as parameter to the function.
void setADCMode(mode):
ADC mode for shunt and bus voltage conversion, where BIT_MODE_X are single measurements with X bit resolution and SAMPLE_MODE_X is the mean value from X measurements.
  • BIT_MODE_9:
    9-Bit Resolution and 84µs conversion time
  • BIT_MODE_10:
    10-Bit Resolution and 148µs conversion time
  • BIT_MODE_11:
    11-Bit Resolution and 276µs conversion time
  • BIT_MODE_12 (Default):
    12-Bit Resolution and 532µs conversion time
  • SAMPLE_MODE_2:
    Mean Value 2 samples and 1.06ms conversion time
  • SAMPLE_MODE_4:
    Mean Value 4 samples and 2.13ms conversion time
  • SAMPLE_MODE_8:
    Mean Value 8 samples and 4.26ms conversion time
  • SAMPLE_MODE_16:
    Mean Value 16 samples and 8.51ms conversion time
  • SAMPLE_MODE_32:
    Mean Value 32 samples and 17.02ms conversion time
  • SAMPLE_MODE_64:
    Mean Value 54 samples and 34.05ms conversion time
  • SAMPLE_MODE_128:
    Mean Value 128 samples and 68.10ms conversion time
void setMeasureMode(mode):
  • CONTINUOUS(Default):
    Makes measurements continuously.
  • TRIGGERED:
    Single-Shot measurements made "on request" where the function startSingleMeasurement() starts the measurement. The output takes place only when the measurement is completed.
  • ADC_OFF:
    Switches the ADC off to save power
  • POWER_DOWN:
    Turns the INA219 off. Better to use the powerDown() function, which backs up the content of the configuration register before switching off the INA219.
void setPGain(gain):
Sets the PGA gain and FSR across the shunt resistor.
  • PG_40:
    Gain = 1, FSR = 40mV
  • PG_80:
    Gain = 1/2, FSR = 80mV
  • PG_160:
    Gain = 1/4, FSR = 160mV
  • PG_320:
    Gain = 1/8, FSR = 320mV
void setBusRange(range):
Bus voltage range
  • BRNG_16:
    16V
  • BRNG_32(Default):
    32V
float getBusVoltage_V():
Reads the bus voltage in Volts. The bus voltage is across the load: VIN- - GND.
float getShuntVoltage_mV():
Reads the shunt voltage in millivolts.
float getCurrent_mA():
Reads the current through the load in milliamps.
float getPower():
Reads the load power in milliWatts.
bool getOverflow():
Checks whether one of the data registers is overflowed. Returns True if there is overflow and False if there is no overflow. If overflow occurs, select a higher PGAIN.
void powerDown():
Backs up the content of the configuration register before switching off the INA219.
void powerUp():
Writes back the copy of the configuration register. This writing process awakens the INA219 and ensures that the INA219 returns to the previously selected mode.

The Arduino code sketch provided below reads voltage, current, and power from the INA219 over I2C at a sampling rate of 1 second. The load current is expected to be around 50mA, so the calibration is configured to the lowest setting to maximize the resolution: 16V bus voltage and 400mA current range. The output is printed to the serial monitor on the Arduino IDEIntegrated Development Environment (IDE) is a software application that helps develop software code efficiently. over the USB port.

read_ina219_we.ino
              

The output of the measurements from a 5V source through a 0.1Ω shunt and 100Ω load resistor is provided below.

read_ina219_we.ino Output
              

Conclusion

This tutorial covered how to use the INA219 sensor to measure current, voltage, and power with the Arduino Uno R3. The INA219 IC operation and capabilities were covered and their common breakout modules.

The output of the INA219 module is an I2C digital communication interface that can be read by the Arduino using libraries such as the Adafruit_INA219 and INA219_WE. The Adafruit library is the easiest to use with pre-calculated calibration options and simple function commands to read measurements, while the INA219_WE library has more functionality to control of the INA219. Function definitions and Arduino code examples were provided to show how to use these two libraries.

The INA219 has a wide input voltage of ±26V, and with a 0.1Ω shunt resistor it supports up to 3.2A with 0.8mA resolution. The programmable gain and ADC resolution allows you to lower the input range to 16V 400mA to improve the resolution to 0.1mA. The I2C interface makes it compatible with most microcontrollers and Single Board Computers (SBCs).

Related Content

Products

INA219 DC Power Sensor Products

Created:   21Oct2022   20:27:49   UTC 2022-10-21T20:27:49Z
Updated:   13May2024   00:25:43   UTC 2024-05-13T00:25:43Z

The INA219 IC measures the current and voltage drop across an external shunt resistor with high side sensing and also measures the bus supply voltage. The shunt current, shunt voltage, and bus voltage are output through an I2C interface.


ATmega328P Uno R3 Board Products

Created:   27Jul2023   18:12:25   UTC 2023-07-27T18:12:25Z
Updated:   03Sep2024   22:05:38   UTC 2024-09-03T22:05:38Z

Processor:
8-bit 16MHz AVRAVR microcontrollers derives its name from its developers and stands for, Alf-Egil Bogen and Vegard Wollan RISC microcontroller, and is also known as Advanced Virtual RISC. ATmega328P (max speed 20MHz)
Memory:
32KB Flash, 1KB EEPROMElectrically Erasable Programmable Read-Only Memory, and 2048 bytes SRAMStatic Random Access Memory
Interface:
14x Digital I/OInput/Output with 6x PWMPulse-Width Modulation, 8 channel 10-bit ADCAnalog-to-Digital Converter (ADC, A/D, or A-to-D), and UARTUniversal Asynchronous Receiver-Transmitter/I2CInter-Integrated Circuit. Also referred to as IIC or I2C./SPISerial Peripheral Interface
Boards:
  • Arduino Uno R3
  • Compatible Boards: SparkFun Redboard, Adafruit Metro 328, Elegoo, SunFounder, DFRobot DFRduino

ATmega328P Microcontroller Products

Created:   11Dec2022   19:49:54   UTC 2022-12-11T19:49:54Z
Updated:   04Sep2024   02:03:04   UTC 2024-09-04T02:03:04Z

Processor:
8-bit 16MHz AVRAVR microcontrollers derives its name from its developers and stands for, Alf-Egil Bogen and Vegard Wollan RISC microcontroller, and is also known as Advanced Virtual RISC. ATmega
Memory:
32KB Flash, 1KB EEPROMElectrically Erasable Programmable Read-Only Memory, and 2048 bytes SRAMStatic Random Access Memory
Interface:
Up to 23x Digital I/OInput/Output with 6x PWMPulse-Width Modulation, 8 channel 10-bit ADCAnalog-to-Digital Converter (ADC, A/D, or A-to-D), and UARTUniversal Asynchronous Receiver-Transmitter/I2CInter-Integrated Circuit. Also referred to as IIC or I2C./SPISerial Peripheral Interface
Boards:
  • Arduino Uno R3, Nano, Pro Mini
  • SparkFun RedBoard, Pro Mini
  • Adafruit Pro Trinket, Metro, Metro Mini

Article Rating

Sign in to rate this article

Sign In


(0) Comments

Sign in to leave a comment

Sign In