{"id":960,"date":"2017-04-01T15:46:44","date_gmt":"2017-04-01T15:46:44","guid":{"rendered":"http:\/\/www.thinkering.de\/cms\/?p=960"},"modified":"2017-04-01T15:46:44","modified_gmt":"2017-04-01T15:46:44","slug":"raspberry-pi-zero-lipo-fuel-gauge-with-safe-shutdown","status":"publish","type":"post","link":"http:\/\/www.thinkering.de\/cms\/?p=960","title":{"rendered":"Raspberry Pi Zero Lipo Fuel Gauge With Safe Shutdown"},"content":{"rendered":"<p>The Pi Zero (and Zero W) are quite tolerant in respect to their power requirements. They can even be run from a LiFePO4 battery that has a voltage range of 3.6 to 2.5V. LiPo batteries are better-suited as their voltage ranges from 4.2 down to about 3V. When running the boards from the blank cell (it should have protection circuitry!), they last even longer because fewer losses occur since there is no boost converter in the system. In fact, the 5V is only needed for the USB host, although many devices are supposed to work from voltages down to 3V.<\/p>\n<p><br style=\u201dclear:both;\u201d \/><\/p>\n<p>Unfortunately, these single board computers lack power management and don&#8217;t have shutdown buttons and there are not that many simple and cost-effective solutions for these problems. https:\/\/www.tindie.com\/products\/xorbit\/lifepo4weredpi\/ is one nice example, but unfortunately not fully open source.<br \/>\nMy idea was to combine a MAX17043 LiPo fuel gauge with a pushbutton and a LiPo charging circuit to create a battery and safe shutdown management add-on for the Pi Zero.<\/p>\n<p><a href=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/fuel-gaugeRpi.png\"><img loading=\"lazy\" src=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/fuel-gaugeRpi-300x149.png\" alt=\"\" width=\"300\" height=\"149\" class=\"alignnone size-medium wp-image-962\" srcset=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/fuel-gaugeRpi-300x149.png 300w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/fuel-gaugeRpi-768x380.png 768w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/fuel-gaugeRpi-1024x507.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<br style=\u201dclear:both;\u201d \/><\/p>\n<p><a href=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165346.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165346-300x169.jpg\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-965\" srcset=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165346-300x169.jpg 300w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165346-768x432.jpg 768w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165346-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<br style=\u201dclear:both;\u201d \/><\/p>\n<p><a href=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165329.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165329-300x169.jpg\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-964\" srcset=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165329-300x169.jpg 300w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165329-768x432.jpg 768w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165329-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<br style=\u201dclear:both;\u201d \/><\/p>\n<p><a href=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165038.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165038-300x169.jpg\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-963\" srcset=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165038-300x169.jpg 300w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165038-768x432.jpg 768w, http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/20170401_165038-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<br style=\u201dclear:both;\u201d \/><\/p>\n<p>The little board occupies only the first 10 pins and uses only GPIO4 and SDA + SCL. Design files for the tiny custom TP4056 charger board can be downloaded as a .zip archive:<\/p>\n<p><br style=\u201dclear:both;\u201d \/><a href=\"http:\/\/www.thinkering.de\/cms\/wp-content\/uploads\/2017\/04\/TP4056mCH.zip\">TP4056mCH<\/a><\/p>\n<p>The Python script the make the thing work is attached below. Add it to rc.local to execute on startup.<\/p>\n<pre class=\"brush: python; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n#!\/usr\/bin\/python\r\n\r\n#Python Script for MAX17043 fuel gauge and safe shutdown button\r\n#in order to execute on startup add the following line to \/etc\/rc.local\r\n#(sleep 10;python \/home\/pi\/fuelgauge.py) &amp;\r\n#assuming that fuelgauge.py is in \/home\/pi \r\n#sleep 10 is probably not necessary\r\n\r\nimport RPi.GPIO as GPIO\r\nimport smbus\r\nimport time\r\nimport os\r\n\r\n\r\n#threshold in % below which the pi gets shut down\r\nthreshold=15.0\r\nbuttonPin=4\r\n# I2C-Adddress of MAX17043\r\naddress = 0x36\r\n\r\n#registers\r\nVCELL_REGISTER=0x02\r\nSOC_REGISTER=0x04\r\nMODE_REGISTER=0x06\r\nVERSION_REGISTER=0x08\r\nCONFIG_REGISTER=0x0C\r\nCOMMAND_REGISTER=0xFE\r\n\r\n#  open the bus by creating an instance\r\nMAX17043 = smbus.SMBus(1)\r\n\r\n#set up GPIO for shutdown button\r\nGPIO.setmode(GPIO.BCM)\r\n#Using GPIO nr 4\r\nGPIO.setup(buttonPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)\r\n\r\ndef reset():\r\n    MAX17043.write_byte_data(address, COMMAND_REGISTER, 0x00)\r\n    MAX17043.write_byte_data(address, COMMAND_REGISTER, 0x04)\r\n\r\ndef quickStart():\r\n    MAX17043.write_byte_data(address, MODE_REGISTER, 0x40)\r\n    MAX17043.write_byte_data(address, MODE_REGISTER, 0x00)\r\n\r\n#get state of charge\r\ndef getSOC():\r\n    # Konfiguration des MAX17043\r\n    MSB= MAX17043.read_byte_data(address, SOC_REGISTER)\r\n    LSB = MAX17043.read_byte_data(address, SOC_REGISTER)\r\n    percentage= MSB+ LSB\/256.0\r\n    #print percentage\r\n    if percentage &lt; threshold:\r\n        os.system(&quot;sudo shutdown -h now&quot;)\r\n\r\n\r\n#Arduino map function for convenience\r\ndef arduino_map(x, in_min, in_max, out_min, out_max):\r\n    return (x - in_min) * (out_max - out_min) \/\/ (in_max - in_min) + out_min\r\n\r\n#setup\r\nreset()\r\nquickStart()\r\n\r\n# Loop\r\nwhile True:\r\n    getSOC()\r\n    time.sleep(1.5)\r\n    #shutdown if button is pressed and held\r\n    if (GPIO.input(buttonPin)==0):\r\n        os.system(&quot;sudo shutdown -h now&quot;)\r\n\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n   main()\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Pi Zero (and Zero W) are quite tolerant in respect to their power requirements. They can even be run from a LiFePO4 battery that has a voltage range of 3.6 to 2.5V. LiPo batteries are better-suited as their voltage ranges from 4.2 down to about 3V. When running the boards from the blank cell &hellip; <a href=\"http:\/\/www.thinkering.de\/cms\/?p=960\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi Zero Lipo Fuel Gauge With Safe Shutdown<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/posts\/960"}],"collection":[{"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=960"}],"version-history":[{"count":9,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/posts\/960\/revisions"}],"predecessor-version":[{"id":975,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=\/wp\/v2\/posts\/960\/revisions\/975"}],"wp:attachment":[{"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=960"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thinkering.de\/cms\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}