Trying out the INA219

I have been looking into developing a device to monitor a solar charging unit. This would be a remote sensor and relay the information back to a base to be presented on a user interface. There are a lot of elements to this type of project and one problem I have been thinking about was how to measure the voltage and current. Since most Microcontrollers have Analogue to Digital Conversion (ADC), I was originally thinking the measurement of voltage was not going to be a problem. But there are still a lot of things to think about when getting a measurement that is accurate and reliable over various load and temperature conditions. After seeing the YouTube video ” #321 7 Sensors tested: Measuring Current with Microcontrollers (Arduino, ESP32, ESP8266)” I became interested in the INA219 and thought to try it out.

Goal

The goal is to learn how the INA219 can be employed to monitor the voltage and current of a solar charging unit that will eventually be installed in a remote sensing unit. A couple of simple experiments will help to understand the interfacing, configuration and calibration of the device along with any issues and special considerations along the way.

To achieve this goal, some firmware will be required to communicate with the INA219. Normally, I would be using Atmel Studio 7. For this experiment, I will be trialing the MPLAB X Integrated Development Environment from Microchip.

Method

Equipment

Approach

Connection

The equipment listed was connected in the configuration shown in Diagram 1. The main devices to be connected were a Lab Power Supply (set to 14V and 2A), a MCU, the INA219 development board a Current Sink to simulate various loads on the system. A 5V regulator, powered by a 9V battery, was used to power the control circuitry of the Current Sink. The 9V battery was used as I did not want to introduce any additional load on the Lab Power Supply.

The MCU board was powered from a PC via the USB port that was used for programming and debugging.

To consider the eventual usage of the whole system a USART to USB adapter was connected to the MCU board to transfer the voltage and current readings to the PC. Finally, the INA219 was connected to the MCU TWI (IIC) interface.

One of the voltmeters was used to measure the Vbus (IN- to GND, range 20V), the other to measure Vshunt (IN+ to IN-, range 200mV) and the ammeter was used to measure the current flowing to the Current Sink, in this case, simply IN+ to GND, range 20V.

Calibration and Configuration

The configuration steps were taken from the INA219 datasheet using the values in Table 2 below. In summary, The conditions for the experiment will be: Maximum expected load current = 1.5 A, Nominal load = 1 A, VCM = 14V, Rshunt = 0.1mΩ, Vshunt Full Scale Range (FSR) = 320mV (PGA=/8) and Vbus Range (BRNG) = 0 (VBUS range = 16 V).

equation1
Equation 1.

Equation2
Equation 2.

The Current Least Significant Bits (LSB) was calculated (Equation 1.) to be 0.061 mA/bit, rounded up to 0.1 mA/bit. The Calibration value (Equation 2.) came to 4096 (1000h).

1514131211109876543210
RSTBRNGPG1PG0BADC4BADC3BADC2BADC1SADC4SADC3SADC2SADC1MODE3MODE2MODE1
0001100110011111
16VDiv 8Shunt and bus continuous
Table 1. Configuration Bits
PropertyValueUnitHexHex MSB First
Vcc3.3V
Vcm14V
BRNG16V
LSB0.004A/bit
Rshunt0.1Ohm
FSR Vshunt0.32V
Calibration1000
Imax2A
Inom1A
Vshunt0.2V
Vdifferential0.1
VIN+14V
VIN-13.8V
Current_LSB0.061
Rounded Current_LSB0.100
Rshunt Pwr0.40.4
Configuration00011001-10011111199F9F19
Calibration409610000010
Table 2. Properties

Firmware

The firmware went through several iterations before settling on the final version for this experiment. The iterations helped to work out the teething problems with communication with the TWI Bus and USART. The USART was used to communicate the values read from the INA219 to the PC. A pin interrupt was used on the User Push Button (SW300) to trigger when a reading was to take place. This also doubled as a reset button when held for more than three seconds.

The output was comma separated values (CSV) so that it could be easily read into a spreadsheet and graphed. In the dataset, a “reading number” or identifier and a timestamp were included. The timestamp was the number of seconds since the MCU was started or a Reset invoked. A reset could be invoked by holding the User Push Button for more than three seconds. Afterwhich the Reading Number and timestamp would be reset to zero.

reading, timestamp, Vshunt-raw, Vshunt, Vbus-raw, Vbus, current-raw, current, power-raw, power
1, 24, 0x000d, 0.130, 0x6d02, 13.952, 0x000d, 1.299, 0x0007, 14.000
2, 92, 0x045a, 11.139, 0x6c9a, 13.900, 0x03fd, 102.099, 0x02dc, 1464.000

Sample CSV output

Steps

  1. The Current Sink was set to minimal current load.
  2. The lab power supply was started to power the 5V regulator and the Current Sink.
  3. The USART to USB adapter was switched on
  4. The HTerm program was started and connected to the USART to USB adapter – Baud: 9600, Data: 8, Stop: 1, Parity: None
  5. The MCU board was powered on.
  6. The reading from the three meters was recorded and the User Push Button pressed (i.e. less than three seconds) to initiate a reading that was output to the HTerm Received Data area. The reading number was also recorded to associate the three voltmeters with the reading from the INA219.
  7. The Current Sink current load was increased so that the current reading on the lab power supply increased by 100 mA
  8. Steps 6 and 7 were repeated until the lab power supply reached 1.5 amperes.
  9. The data captured by the HTerm program was then saved off as a CSV file and imported into a spreadsheet where the data was then collated and graphed with the data captured from the meters.

Results

ReadingLoad Lab SupplyLoad AnalogRead VshuntRead VbusMetered VshuntMetered Vbus
1200.14913.9520.113.90
212010012.26913.88812.413.83
321420022.52913.83622.113.79
431730032.71913.78832.813.73
541540044.07913.73642.913.68
651050052.87913.68452.813.63
760760062.91913.61262.913.56
870770073.25013.59273.113.52
980280083.76913.54883.313.50
1090490094.82913.50094.113.44
119951000103.77913.468103.913.41
1210821100113.97013.380113.413.36
1311921200124.50013.340125.313.30
1412871300138.05013.284135.513.29
1513991400149.66013.284148.013.25
1615051500159.38913.236159.813.21
Table 3. Read and measured results

Vshunt
Graph 1. Vshunt vs Load

Vload
Graph 2. Vbus Vs Load

ReadingRead CurrentRead PowerLoad Lab SupplyLoad Analog
11.2001620
2123.8001734120100
3219.6003040214200
4330.7004436317300
5435.7005896415400
6524.9007156510500
7629.0998608607600
8735.4009982707700
9837.40011344802800
10946.00012694904900
111050.199140229951000
121144.3001518410821100
131261.4001683411921200
141352.3001831212871300
151477.5991959213991400
161594.0002109415051500
Table 4. Current comparison

Current
Graph 3. Current Comparison

Discussion

There were some teething problems with the connection of the system. These were worked through in association with the TI E2E Support Forum where the load ground was not directly connected back to the load supply. Once this issue was fixed, the values read back from the INA219 for the Vshunt and Vbus were very close to the values measured on the voltmeters.

I did have an issue where the Vbus was a lot higher than expected. I eventually found out that one of the multimeters used for measuring voltage is out by at least 8%. This caused some confusion in the results and the experiment needed to be repeated several times.

I had a further issue where It can be seen in Graph 3, for the current comparison, that the result read from the INA219 tends to move away from the measured value in an almost linear fashion. At first I thought this is related to other loads and errors in the system. I.e. The 5V regulator being supplied also by the lab power supply. The extra loads were removed by supplying the 5V regulator with a 9V battery, but still the trend persisted.

Finally, I introduced an ammeter into the configuration. Due to the resources available, a 30V 1.5A analogue meter was used. Once again, the results were consistent with previous readings. I then applied the formula for the Current Register based on the Vshunt and the calibration value. Here I found that the calculated value was consistent with the values read from the INA219.

Going back to the datasheet, the calculation for the Corrected Full Scale Calibration was applied

Equation3

Equation 3.

Equation4

= 3854 ⇒ 0F0Eh

The new calibration value was applied to the INA219 and the test procedure was executed. The results were much better and the current value read from the INA219 was in line with the expected results.

ReadingRead VshuntRead CurrentRead PowerLoad Lab SupplyLoad AnalogCurrent Register
10.1301.29914201.223
211.139102.0991464105100104.809
321.189205.6992782207200199.371
433.040306.6004200314300310.879
541.629393.7995410404400391.695
652.559491.0006724507500494.537
761.639579.7997920597600579.972
873.790695.5999450707700694.303
984.729798.29910882812800797.230
1093.250875.29911894892900877.406
11105.250986.799132329921000990.316
12113.5991077.59914428108411001068.873
13126.1801172.59915646118412001187.250
14136.6601280.80017178129413001285.858
15145.3791395.00018354139714001367.897
16162.0891489.59919586151215001525.125
Table 5. Calibrated Current Comparison

Current_Corrected
Graph 4. Calibrated Current Comparison

Conclusion

INA219

I had little doubt that the INA219 would be ideal for measuring the current and voltage in the intended system. Using a formal approach to testing and recording the results has helped to identify some areas to watch out for when using this device. Some of the things learned along the way include

  • Ensure that the Ground paths are connected correctly to their respective power domains
  • Relying on the base values for calibration from the calculations is not enough.
  • The final system where the INA219 is implemented will need a means to apply the corrected calibration value.

It was interesting to use an analogue meter to set the current. I found that it was much simpler to set a respectable current level than using the digital display on the Lab Power Supply. Lining up the needle to the mark is much quicker than trying to chase numbers on a display.

MPLAB X IDE

The usage of the MPLAB X IDE did not feature much in this report. Overall it worked very well and the MPLAB Code Configurator (MCC) was useful in setting up the pin usage and generating some base code for TWI and USART. The MCC is somewhat similar to the Atmel Start configurator. One area that took some working out was the setting of the Fuse Bits. In Atmel Studio 7, this is a part of the programmer dialog. For MPLAB X, this is reached via the Windows -> Target Memory Views -> Configuration Bits

Diagram 2. MPLAB X Accessing Fuse Bits

Before any changes are made, the values of the bits should be read from the device and then written back after changes. This is achieved via the respective buttons on the Configuration Bits window. Diagram 3 highlights these buttons.

Diagram 3. Reading and writing Fuse Bits

References

Firmware

TI E2E Support Forum

#321 7 Sensors tested: Measuring Current with Microcontrollers (Arduino, ESP32, ESP8266)

MPLAB X

Adafruit INA219 Dev Board

Current Sink or Swim – CSoS project from Contextual Electronics

ATTiny 3217 Xplained-pro development board

Leave a comment