Arduino INA219 (DC Current Sensor) Library & Code Example

In this tutorial, you’ll learn how to interface an INA219 With Arduino (DC Current Sensor), install the Arduino INA219 library, and test some code examples to Measure DC Current with Arduino + INA219. Without further ado, let’s get right into it!

Table of Contents

  1. Arduino INA219 DC Current Sensor
  2. Arduino INA219 Interfacing
  3. Arduino INA219 Library Installation
  4. Arduino INA219 DC Current Measurement Example
  5. More Arduino Current Sensors
  6. Wrap Up

Arduino INA219 DC Current Sensor

The INA219 DC current sensor is a common choice for Arduino projects needing (DC current, bus voltage, and power) measurement capability. The INA219 current sensor provides an I2C digital output that makes it so easy to measure the DC currents with multiple INA219 sensors using only 2 wires (up to 16 different addresses).

The INA219 sensor can also determine the polarity of the current (positive or negative) while measuring DC currents up to ±3.2A. It can also measure the bus voltage (0 up to 26v) and also do power calculation (P = I x V) in hardware. All of this and other features make the INA219 an ideal choice for a wide range of power monitoring applications.

INA219 Pinout Diagram

Below is the pinout diagram of the INA219 DC current sensor.

INA219 Pinout (Current Sensor)

Key Specifications of the INA219 DC Current Sensor:

  • 3-5v single supply operation
  • Up to ±3.2A DC current measurement
  • Supply voltage measurement from 0v up to 26v
  • Reports Current, Voltage, and Power (P = I x V)
  • Integrated 12 Bits ADC for precise measurements
  • High accuracy (0.5%) over the temperature range
  • I2C digital output with 16 programmable addresses

INA219 DC Current Sensor Working Principles

The INA219 sensor depends on an external shunt resistor connected to the high side of your load circuitry. The “to be measured” DC current goes through the shunt resistor which develops a voltage drop over the current-sensing shunt resistor. The differential voltage between the shunt’s terminals (Vin+ & Vin-) is then amplified and measured by the INA219 sensor.

The internal 12-bit ADC takes the measurement and converts the input differential voltage into current output. There is also hardware support for measuring and storing the bus voltage and calculating the power consumption (P = I x V). All these digital values are stored in internal registers and sent to the microcontroller (Arduino, ESP32, or whatever) over the I2C bus. The diagram below shows the internal structure of the INA219 sensor.

INA219 DC Current Sensor Working Principles & Shunt Resistor

The onboard shunt resistor that you’ll find on your INA219 sensor’s board is a 0.1Ω power resistor that’s used to sense the DC current going through your load. The sensor’s board has a couple of I2C pull-up resistors, so you don’t need to connect anything to the I2C lines.

Buy an INA219 Current Sensor: you can find it here on ( Amazon, AliExpress, eBay )


Arduino INA219 Interfacing

Interfacing the INA219 current sensor with Arduino is so easy as it only requires two wires (I2C bus). We’ll use the Adafruit INA219 library to handle the I2C communication with the INA219 sensor and read the measured current, voltage, and power.

Arduino INA219 Wiring (Connection)

This is a summary of the connections needed between your Arduino board and the INA219 current sensor.

INA219Arduino UNODescription
VCC5VPower supply
GNDGNDGround
SDAA4 (SDA)I2C SDA Line
SCLA5 (SCL)I2C SCL Line

Arduino INA219 Library Installation

The library we’ll use in this tutorial is called Adafruit_INA219, and this is how to install it on your system.

Step #1

Open your Arduino IDE > Tools > Manage Libraries > Search For “INA219“.

Step #2

Install the INA219 library as shown below.

Arduino INA219 Library Code Example

And that’s all about it. Now we can use the library as we’ll do in the next example project.

❕ Note

The default I2C address for the INA219 current sensor is 0x40. However, you can modify this default address by soldering the A0 & A1 address control pins on the sensor’s board. The default states for the address control pins are (A0 = 0, and A1 = 0).

In case, you need to use multiple INA219 sensors, just modify the (A0 & A1) pin states, then call the following function to get the I2C device address of your INA219:


Arduino INA219 DC Current Measurement Example

In this example project, we’ll measure DC current with Arduino + INA219 current sensor. The measured DC current will be displayed on an I2C LCD 16×2 display using the same two (I2C) pins of the Arduino board.

I’ll use a variable DC power supply and an 8Ω load to test the INA219 current sensor. Just make sure the load current is below the maximum allowed value of ±3.2A.

Arduino INA219 Wiring (DC Current Measurement)

Here is the wiring diagram for the Arduino DC current measurement setup I’ve used to test the INA219 sensor.

Arduino INA219 DC Current Sensor Wiring Diagram

❕ Note

For the voltage & power measurement features to work properly, you need to connect the GND terminal of the DC voltage supply to the GND pin of your Arduino board as well. In this example, I’m only measuring the DC current, so I didn’t connect the power circuit’s GND to the Arduino’s GND.

Example Code

Here is the full code listing for this example.

The INA219 sensor initialization configurations are set in this line of code:

The getCurrent_mA() function is used to read the measured DC current in (mA) unit.

Testing Results

Here are the results of testing this project code example on my Arduino UNO board.

The measurement error percentage is very low in all test cases as you’ve seen in the demo video.

Other Useful Functions in The INA219 Library

Here are some other useful functions that you can use from the INA219 library as well:

Arduino I2C LCD 16x2 Interfacing Tutorial & Library Examples

This tutorial will give you in-depth information about using I2C LCD 16×2 with Arduino boards, and how to install & use the LiquidCrystal_I2C library with multiple demo code examples to learn from. Check it out!


Required Parts List

Here is the full components list for all the parts you’d need to perform the practical LABs mentioned in this tutorial and the whole Arduino Programming series of tutorials found here on our website.

* Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

QTY.Component NameAmazon.comAliExpresseBay
1INA219 Current SensorAmazonAliExpresseBay
1Arduino UNO KitAmazonAliExpresseBay
1Complete Arduino Sensors/Modules KitAmazonAliExpresseBay
1DC Power SupplyAmazonAliExpresseBay
1BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Tutorial’s Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


More Arduino Current Sensors

Coming Soon Tutorial

Arduino SCT-013 AC Current Sensor

Coming Soon Tutorial

Arduino INA226 DC Current Sensor


Wrap Up

To conclude this tutorial, we’ve covered how to interface an INA219 DC Current Sensor with Arduino, and how to use this sensor to measure DC current, bus voltage, and power with your Arduino board. You can build on top of the provided example projects and use the INA219 current sensor with your Arduino boards.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

References:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment