Measure DC Power with an INA219 and RPi Pico
Updated: 04Aug2024 03:56:00 UTC 2024-08-04T03:56:00Z
Rating: (0 reviewsThis article has not been rated yet)
This tutorial shows how to use the INA219 sensor module to measure voltage, current, and power with the Raspberry Pi Pico microcontroller board. The INA219 ICIntegrated Circuit, modules (including pinout), Pico Hookup, and CircuitPython and MicroPython libraries with 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.
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.
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)
|
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 |
|
Offset Voltage Drift | 0.1μV/°C |
Max Shunt Voltage |
|
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.
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.
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 CircuitPython library on GitHub for communicating with the device over I2C.
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.
RPi Pico Hookup
The RPi Pico has three power output ports (5V VBUS, 5V VSYS, and 3.3V OUT) that can be used to power the INA219 Module and provide power to the load. The Pico has two I2C interfaces, I2C0 and I2C1, each with two lines (SCLSerial Clock (SCL) is the output clock signal line from the master device. Also referred to as SCK, SCLK, or CLK., SDASerial Data Line), that can be accessed on many of its GPIO pins. More details on how I2C works can be found in the article I2C Communication. The Pico pinout in the figure below shows where the power and I2C pins are located.
For this tutorial, the INA219 module will be powered from the Pico's 3.3V OUT pin. Connect the Pico 3.3V OUT pin to the INA219 VCC and the Pico GND pin to the INA219 GND. The two I2C communication lines (SCL and SDA) also need to be connected between the INA219 and Pico.
The external load in this example is a 100Ω resistor that is powered by the Pico's VBUS 5V pin. The VBUS pin is directly connected to the power line of the USB port, so the voltage and max current output depends on the USB power source and the voltage drop across the USB cable. According to the USB 2.0 specs, the voltage ranges are different for low and high power buses (4.35V - 5.25V 100mA for the Low Power Bus and 4.75V - 5.25V 500mA for the High Power Bus).
The 5V VBUS will provide a current through the 100Ω load resistor of about 5V/(100Ω + 0.1Ω) ≈ 50mA. You can use a lower value resistor to provide more current, but it's recommended that you stay below the maximum rating of the USB Bus for the combined current consumption of the load, Pico board, and INA219 Module. The load current needs to be measured on the high-side, so the 5V VBUS pin is connected directly to the INA219 VIN+ pin and VIN- is connected to the 100Ω load resistor and then to ground.
RPi Pico Code
The output of the INA219 module are registers that can be read over an I2C digital communication interface by the Pico using libraries. The two most common libraries are the Adafruit_CircuitPython_INA219 in CircuitPython and pyb_ina219 in MicroPython that can be downloaded from GitHub.
The Adafruit CircuitPython library uses its own BusDevice and Register libraries for communicating over I2C. The pyb_ina219 MicroPython library uses the I2C class from the built-in Machine library.
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 read measurements. The pyb_ina219 library has more functionality and control that allows you to set the gain and bus voltage range, set ADC resolution/sampling, set wake or sleep modes, and check for overflow. Both libraries currently only support continuous reads of voltage and power, but not triggered reads.
Code examples using both of these libraries will be provided in the following subsections.
Adafruit CircuitPython INA219 Library
The Adafruit_CircuitPython_INA219 library was created by Adafruit for their INA219 module, although the library also works with generic modules. This library is available on GitHub, along with its dependencies Adafruit CircuitPython, Bus Device, and Registers. The Bus Device and Register libraries can be downloaded separately on GitHub or you can download the project bundle (ZIP). Adafruit has a guide on how to install CircuitPython on the RPi Pico and how to install libraries.
The Adafruit library 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.
- INA219(i2c_bus):
- Class constructor to set the I2C address to i2c_bus and initialize calibration. If i2c_bus is not provided, then the default is 0x40. This constructor initializes the calibration with a maximum bus voltage of 32V and maximum current of 2A.
- None set_calibration_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.
- None set_calibration_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.
- None set_calibration_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 bus_voltage():
- Reads the bus voltage in Volts. The bus voltage is across the load: VIN- - GND.
- float shunt_voltage():
- Reads the shunt voltage in millivolts.
- float current():
- Reads the current through the load in milliamps.
- float power():
- Reads the load power through in Watts.
- boolean overflow():
- Overflow bit in the Bus Voltage Register that can be used to check for overflow. Returns True if there is overflow and False if there is no overflow.
The Pico CircuitPython code 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 computer console over the USB port.
The output of the measurements from a 5V source through a 0.1Ω shunt and 100Ω load resistor is provided below.
pyb_ina219 MicroPython Library
The pyb_ina219 MicroPython library available on GitHub was created by Chris Borrill to provide more functionality and control over the INA219 and can be used with other microcontroller boards such as the pyboard, ESP8266, and ESP32. A list of functions available in the pyb_ina219 library are provided below. The GitHub README page has more details on the library usage and functions.
- INA219():
-
Class constructor to set the shunt resistance, maximum expected current, I2C address, and logging level. The
input arguments are defined below.
- shunt_ohms:
(required) Value of shunt resistor in Ohms. -
max_expected_amps:
(optional) The maximum expected current in Amps that can be used to calculate the best gain to achieve the highest resolution when GAIN_AUTO mode is set in configure(). -
address:
(optional) An instance of the I2C class from the MicroPython Machine module, either I2C(1) or I2C(2). Defaults to 0x40. -
log_level:
(optional) Sets the logging level for the MicroPython logging module. Default is logging.ERROR. To understand the calibration calculation results and automatic gain increases, informational output can be enabled with logging.INFO. Detailed logging of device register operations can be enabled with logging.DEBUG.
- shunt_ohms:
- None configure():
-
Configures and calibrates how the INA219 will take measurements. The input arguments are defined below.
-
voltage_range:
The bus full scale voltage range, this is either 16V or 32V set by the constants RANGE_16V or RANGE_32V (default). -
gain:
The gain which controls the maximum range of the shunt voltage set by the constants GAIN_1_40MV, GAIN_2_80MV, GAIN_4_160MV, GAIN_8_320MV, or GAIN_AUTO (default). If GAIN_AUTO is configured without setting the max_expected_amps, then GAIN_1_40MV is used. -
bus_adc:
The bus ADC resolution (9, 10, 11, or 12-bit) or the number of samples used when averaging results. Set by the constants ADC_9BIT, ADC_10BIT, ADC_11BIT, ADC_12BIT (default), ADC_2SAMP, ADC_4SAMP, ADC_8SAMP, ADC_16SAMP, ADC_32SAMP, ADC_64SAMP, or ADC_128SAMP. -
shunt_adc:
The shunt ADC resolution (9, 10, 11, or 12-bit) or the number of samples used when averaging results. Set by the constants ADC_9BIT, ADC_10BIT, ADC_11BIT, ADC_12BIT (default), ADC_2SAMP, ADC_4SAMP, ADC_8SAMP, ADC_16SAMP, ADC_32SAMP, ADC_64SAMP, or ADC_128SAMP.
-
voltage_range:
- float voltage():
- Reads the bus voltage in Volts. The bus voltage is across the load: VIN- - GND.
- float supply_voltage():
- Returns the computed bus supply voltage in volts. This is the sum of the bus voltage and shunt voltage. A DeviceRangeError exception is thrown if current overflow occurs.
- float shunt_voltage():
- Reads the shunt voltage in millivolts. A DeviceRangeError exception is thrown if current overflow occurs.
- float current():
- Reads the current in milliamps. A DeviceRangeError exception is thrown if current overflow occurs.
- float power():
- Reads the load power in milliWatts. A DeviceRangeError exception is thrown if current overflow occurs.
- bool current_overflow():
- 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 sleep():
- Puts the INA219 into power down mode.
- void wake():
- Wakes the INA219 from power down mode.
- void reset():
- Resets the INA219 to its default configuration.
The Pico MicroPython code 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 computer console over the USB port.
The output of the measurements from a 5V source through a 0.1Ω shunt and 100Ω load resistor is provided below.
Conclusion
This tutorial covered how to use the INA219 sensor to measure current, voltage, and power with the RPi Pico. 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 Pico using libraries such as the Adafruit CircuitPython library and pyb_ina219 MicroPython library. The Adafruit library is the easiest to use with pre-calculated calibration options and simple function commands to read measurements, while the pyb_ina219 library has more functionality to control of the INA219. Function definitions and Pico 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).
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.
Created:
27Jul2023 23:50:32 UTC
2023-07-27T23:50:32Z
Updated:
04Sep2024 00:31:23 UTC
2024-09-04T00:31:23Z
- Processor:
- 32-bit 133MHz Dual-Core ARMAdvanced Reduced Instruction Set Computer (RISC) Machines Cortex-M0+
- Memory:
- 2MB QSPIQuad Serial Peripheral Interface (QSPI) is a serial communication interface designed for talking to flash chips by using 4 data lines. Flash and 264KB SRAMStatic Random Access Memory
- Interface:
- 1x Micro-B USB, Up to 30x Digital I/OInput/Output with PWMPulse-Width Modulation, 4x Analog Inputs 12-bit ADCAnalog-to-Digital Converter (ADC, A/D, or A-to-D), 2x UARTUniversal Asynchronous Receiver-Transmitter, 2x I2CInter-Integrated Circuit. Also referred to as IIC or I2C., 2x SPISerial Peripheral Interface, and PIOProgrammable Input/Output (PIO) can be programmed to process data transmission, such as a non-standard serial interface, without using resources from the CPU.
- Boards:
- RPiRaspberry Pi Pico, Pico H, Pico W, and Pico WH
Created:
11Dec2022 23:50:41 UTC
2022-12-11T23:50:41Z
Updated:
04Sep2024 22:33:49 UTC
2024-09-04T22:33:49Z
- Processor:
- 32-bit 133MHz Dual-Core ARMAdvanced Reduced Instruction Set Computer (RISC) Machines Cortex-M0+
- Memory:
- 2MB QSPIQuad Serial Peripheral Interface (QSPI) is a serial communication interface designed for talking to flash chips by using 4 data lines. Flash and 264KB SRAMStatic Random Access Memory
- Interface:
- Up to 30x Digital I/OInput/Output with PWMPulse-Width Modulation, 4 channel 12-bit 500kbs ADCAnalog-to-Digital Converter (ADC, A/D, or A-to-D), USB 1.1 Host and Device Full/Low speed, and UARTUniversal Asynchronous Receiver-Transmitter/I2CInter-Integrated Circuit. Also referred to as IIC or I2C./SPISerial Peripheral Interface/PIOProgrammable Input/Output (PIO) can be programmed to process data transmission, such as a non-standard serial interface, without using resources from the CPU.
- Boards:
-
- RPiRaspberry Pi Pico, Pico H, Pico W, Pico WH
- Adafruit QT Py, ItsyBitsy, Feather
- SparkFun Pro Micro, Thing Plus
- Pimoroni Tiny 2040, Pico LiPo
- Seeed Studio XIAO
- Waveshare RP2040-Zero, RP2040-Plus
(0) Comments
Sign in to leave a comment
Sign In