{"id":1106,"date":"2023-08-17T14:57:18","date_gmt":"2023-08-17T06:57:18","guid":{"rendered":"https:\/\/xujiwei.com\/blog\/?p=1106"},"modified":"2023-08-17T14:57:18","modified_gmt":"2023-08-17T06:57:18","slug":"using-ina219-with-arduino-to-measure-power-en","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/","title":{"rendered":"Using INA219 with Arduino to Measure Voltage, Current, and Power in a Circuit"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\">\u6587\u7ae0\u76ee\u5f55<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fb2342a9d5e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fb2342a9d5e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Schematic_Diagram\" title=\"Schematic Diagram\">Schematic Diagram<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Using_INA219_with_Arduino\" title=\"Using INA219 with Arduino\">Using INA219 with Arduino<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Setting_the_gain\" title=\"Setting the gain\">Setting the gain<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Using_software_emulation_for_I2C\" title=\"Using software emulation for I2C\">Using software emulation for I2C<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Sampling_resistor_heating\" title=\"Sampling resistor heating\">Sampling resistor heating<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Expanding_the_current_range\" title=\"Expanding the current range\">Expanding the current range<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/#References\" title=\"References\">References<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p>Recently, while working on a DIY project, I needed to measure voltage, current, and power in the circuit. After comparing several chips, I decided to use the INA219 chip from Texas Instruments. Coincidentally, I had previously used an INA219 module to <a href=\"https:\/\/ohdarling88.medium.com\/build-a-power-meter-with-esphome-and-ina219-d965c8ab31b1\">build a simple power meter<\/a>, which made me quite familiar with it.<\/p>\n<p>The official introduction of INA219 is as follows:<\/p>\n<blockquote>\n<p>The INA219 is a current shunt and power monitor with an I2C- or SMBUS-compatible interface. The device monitors both shunt voltage drop and bus supply voltage, with programmable conversion times and filtering. A programmable calibration value, combined with an internal multiplier, enables direct readouts of current in amperes. An additional multiplying register calculates power in watts. The I2C- or SMBUS-compatible interface features 16 programmable addresses.<\/p>\n<\/blockquote>\n<p>The usage of INA219 is relatively simple. Data can be read directly through the I2C bus, and it supports a maximum measurement voltage of 26V with 12-bit precision, which satisfies the requirements in most scenarios. On the <a href=\"https:\/\/oshwhub.com\">OSHWHub<\/a>, many USB power meters also use this chip.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Schematic_Diagram\"><\/span>Schematic Diagram<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2023\/08\/using-ina219-in-arduino-schematic.png\" alt=\"using-ina219-in-arduino-schematic\" style=\"zoom:50%;\" \/><\/p>\n<p>INA219 requires only a few external components to operate. Adding a bypass capacitor to the power supply and a current sensing resistor completes the setup.<\/p>\n<p>A0 and A1 are used to specify the address of INA219 when communicating via I2C. If there is only one INA219 chip in the circuit, these two pins can be directly connected to ground.<\/p>\n<p>Typically, INA219 is used for high-end sensing. Therefore, in the circuit, VIN+ is connected to the positive terminal of the measured power source, and VIN- is connected to the input terminal of the circuit using the power source.<\/p>\n<p>By default, INA219 uses a 0.1-ohm current sensing resistor. However, in practical applications, the current sensing resistor can be replaced with a different value based on the current range to be measured.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Using_INA219_with_Arduino\"><\/span>Using INA219 with Arduino<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Arduino, there are several libraries available to support the setup and data reading of INA219. After comparing several libraries, I ultimately chose the INA219_WE library. It can be easily installed through PlatformIO or cloned from GitHub to be used in the source code.<\/p>\n<pre><code class=\"language-c++\">\/\/ The address for communication with INA219 when both A0 and A1 are grounded is 0x40.\nINA219_WE ina219 = INA219_WE(0x40);\n\nvoid readINA219() {\n    \/\/ Voltage drop across the current sensing resistor\n    float shuntVoltage_mV = ina219.getShuntVoltage_mV();\n    \/\/ Power supply input voltage\n    float busVoltage_V = ina219.getBusVoltage_V();\n    \/\/ Magnitude of the sampled current\n    float current_mA = ina219.getCurrent_mA();\n    \/\/ Magnitude of power in the circuit\n    float power_mW = ina219.getBusPower();\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Setting_the_gain\"><\/span>Setting the gain<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>By default, INA219 uses the PG_320 gain setting, which allows a maximum voltage drop of 320mV across the current sensing resistor. When using a 0.1-ohm current sensing resistor, this corresponds to a maximum current measurement of 3.2A with a minimum resolution of 780uA.<\/p>\n<p>If higher accuracy is required, the gain of INA219 can be adjusted based on the specific measurement circuit. For example, if the measured current does not exceed 800mA, the PG_80 gain setting can be used to achieve a minimum resolution of around 20uA.<\/p>\n<p>The gain can be set using the setPGain method:<\/p>\n<pre><code class=\"language-c++\">ina219.setPGain(PG_80);<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Using_software_emulation_for_I2C\"><\/span>Using software emulation for I2C<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In general, when using the MCU's built-in I2C bus, there are usually no compatibility issues when communicating with the INA219. However, if the MCU's I2C bus is insufficient, it may be necessary to use software emulation for I2C.<\/p>\n<p>When using software emulation for I2C, I searched for some libraries on the PlatformIO platform. However, during the process of using it with ESP32, there were communication errors. After testing, it was found that using <a href=\"https:\/\/github.com\/bxparks\/AceWire\">AceWire<\/a> for software emulation of I2C allows normal communication with the INA219.<\/p>\n<p>If there is a need to communicate with the INA219 using software emulation for I2C, you can try using <a href=\"https:\/\/github.com\/bxparks\/AceWire\">AceWire<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sampling_resistor_heating\"><\/span>Sampling resistor heating<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If the sampling resistor is relatively large and the measured current is high, there may be a significant heating issue with the sampling resistor. In a previous test, when using a 2512 0.05-ohm sampling resistor with a continuous current of around 3A, the temperature of the sampling resistor would rise to 66 degrees Celsius.<\/p>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2023\/08\/\/using-ina219-in-arduino-heating.png\" alt=\"using-ina219-in-arduino-heating\" style=\"zoom:50%;\" \/><\/p>\n<p>Without proper heat dissipation, this temperature is not acceptable. Therefore, if there is a need for continuous high-current sampling, it is recommended to compromise on some sampling accuracy and use a smaller sampling resistor. This way, it can effectively prevent the sampling resistor from heating up too much.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Expanding_the_current_range\"><\/span>Expanding the current range<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>By default, when using the INA219 with a 0.1-ohm sampling resistor, the maximum measurable current is 3.2A. However, in the case of USB PD charging with a maximum of 100W, this exceeds the measurement range.<\/p>\n<p>In such situations, a smaller sampling resistor can be used. Although the sampling accuracy will decrease, an accuracy of around 1mA is sufficient for most scenarios.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The INA219, as a popular power monitoring chip, is quite user-friendly in DIY projects. It comes in an SOP-8 package, making it easy for manual soldering. When used with Arduino, it has a strong open-source library support, allowing for plug-and-play functionality right out of the box.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"References\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/build-power-meter-with-esphome-and-ina219\/\">\u4f7f\u7528 ESPHome + INA219 \u6a21\u5757\u6784\u5efa\u4e00\u4e2a\u7b80\u6613\u529f\u7387\u8ba1<\/a><\/li>\n<li><a href=\"https:\/\/ohdarling88.medium.com\/build-a-power-meter-with-esphome-and-ina219-d965c8ab31b1\">Build a Power Meter with ESPHome and INA219<\/a><\/li>\n<li><a href=\"https:\/\/www.ti.com.cn\/product\/cn\/INA219\">INA219 Zer\u00f8-Drift, Bidirectional Current\/Power Monitor With I 2C Interface<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/wollewald\/INA219_WE\">INA219_WE<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/bxparks\/AceWire\">AceWire<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Recently, while working on a DIY project &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2023\/08\/using-ina219-with-arduino-to-measure-power-en\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[262,328,329,349],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1106"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=1106"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}