{"id":17073,"date":"2022-08-08T19:42:19","date_gmt":"2022-08-08T23:42:19","guid":{"rendered":"https:\/\/scruss.com\/blog\/?p=17073"},"modified":"2022-08-08T19:43:30","modified_gmt":"2022-08-08T23:43:30","slug":"ina219-current-sensor-and-micropython","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2022\/08\/08\/ina219-current-sensor-and-micropython\/","title":{"rendered":"INA219 Current Sensor and MicroPython"},"content":{"rendered":"\n<p>More Micropython programmers \u2014 and especially beginners \u2014 should know about <a href=\"https:\/\/awesome-micropython.com\/\">Awesome MicroPython<\/a>. It&#8217;s a community-curated list of remarkably decent MicroPython libraries, frameworks, software and resources. If you need to interface to a sensor, look there first.<\/p>\n\n\n\n<p>For example, take the <a href=\"https:\/\/www.adafruit.com\/product\/904\">INA219 High Side DC Current Sensor<\/a>. It&#8217;s an I\u00b2C sensor able to measure up to 26 V, \u00b13.2 A. It does this by measuring the voltage across a 0.1 ohm precision shunt resistor with its built-in 12-bit ADC. I got a customer return from the store that was cosmetically damaged but still usable, so I thought I&#8217;d try it with the simplest module I could find in Awesome MicroPython and see how well it worked.<\/p>\n\n\n\n<p>I guess I needed a test circuit too. Using all of what was immediately handy \u2014 a resistor I found on the bench and measured at 150.2 ohm \u2014 I came up with this barely useful circuit:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/ina219.svg\" alt=\"simple circle with 3.3 V DC supply ad two resistors of 150.2 ohms and 0.1 ohms in series\" class=\"wp-image-17074\" height=\"422\"\/><figcaption>Should indicate a current of 3.3 \/ (150.2 + 0.1) = <strong>21.96 mA<\/strong><\/figcaption><\/figure>\n\n\n\n<p>The INA219 would be happier with a much higher current to measure, but I didn&#8217;t have anything handy that could do that.<\/p>\n\n\n\n<p>Looking in Awesome MicroPython&#8217;s <a href=\"https:\/\/awesome-micropython.com\/#current\">Current<\/a> section, I found <a href=\"https:\/\/github.com\/robert-hh\/INA219\">robert-hh\/INA219: INA219 Micropython driver<\/a>. It doesn&#8217;t have much (okay, any) documentation, but it&#8217;s a very small module and the code is easy enough to follow. I put the <em>ina219.py<\/em> module file into the <em>\/lib<\/em> folder of a WeAct Studio <a href=\"https:\/\/www.aliexpress.com\/item\/1005003708090298.html\">RP2040<\/a> board, and wrote the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# INA219 demo - uses https:\/\/github.com\/robert-hh\/INA219\n\nfrom machine import Pin, I2C\nimport ina219\n\ni = I2C(0, scl=Pin(5), sda=Pin(4))\nprint(&quot;I2C Bus Scan: &quot;, i.scan(), &quot;\\n&quot;)\n\nsensor = ina219.INA219(i)\nsensor.set_calibration_16V_400mA()\n\n# my test circuit is 3V3 supply through 150.2 ohm resistor\nr_1 = 150.2\nr_s = 0.1  # shunt resistor on INA219 board\n\n# current is returned in milliamps\nprint(&quot;Current       \/ mA: %8.3f&quot; % (sensor.current))\n# shunt_voltage is returned in volts\nprint(&quot;Shunt voltage \/ mV: %8.3f&quot; % (sensor.shunt_voltage * 1000))\n# estimate supply voltage from known resistance * sensed current\nprint(&quot;3V3 (sensed)  \/ mV: %8.3f&quot; % ((r_1 + r_s) * sensor.current))\n\n<\/pre><\/div>\n\n\n<p>with everything wired up like this (Blue = SDA, Yellow = SCL):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"1024\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/pico_ina219-large-573x1024.jpg\" alt=\"breadboard with RP2040 pico board and INA219 sensor board benath it, and the 150 ohm wired as a circuit on the side\" class=\"wp-image-17077\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/pico_ina219-large-573x1024.jpg 573w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/pico_ina219-large-179x320.jpg 179w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/pico_ina219-large-89x160.jpg 89w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2022\/08\/pico_ina219-large.jpg 680w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><figcaption>all of the wires<\/figcaption><\/figure>\n\n\n\n<p>Running it produced this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nI2C Bus Scan:  &#x5B;64] \n\nCurrent       \/ mA:   22.100\nShunt voltage \/ mV:    2.210\n3V3 (sensed)  \/ mV: 3321.630\n<\/pre><\/div>\n\n\n<p>So it&#8217;s showing just over 22 mA: pretty close to what I calculated!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More Micropython programmers \u2014 and especially beginners \u2014 should know about Awesome MicroPython. It&#8217;s a community-curated list of remarkably decent MicroPython libraries, frameworks, software and resources. If you need to interface to a sensor, look there first. For example, take the INA219 High Side DC Current Sensor. It&#8217;s an I\u00b2C sensor able to measure up [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3315,2],"tags":[1879,3094,3102],"class_list":["post-17073","post","type-post","status-publish","format-standard","hentry","category-electronics","category-goatee-stroking-musing-or-something","tag-awesome","tag-micropython","tag-sensors"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-4rn","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/17073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=17073"}],"version-history":[{"count":4,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/17073\/revisions"}],"predecessor-version":[{"id":17079,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/17073\/revisions\/17079"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=17073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=17073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=17073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}