Low Battery Shutdown Code

When JuiceBox Zero sees the battery voltage reach 3.2V the “LOW BATTERY” LED lights up and causes GPIO16 to go HIGH.  In order to perform a safe shutdown, do the following:

  • Type:
sudo nano LowBatteryShutdown.py

This will create a python file in your current folder for the safe shutdown code.

  • Copy and paste the following code into LowBatteryShutdown.py and save it.

Software for safe shutdown

# This code is complete and can be implemented as it is to safely shutdown
# a Rasbperry Pi using the safe shut down feature on Juice Box Zero.
# This is all Raspberry Pi 101, so it is definitely all open source. Feel 
# free to mix, copy, paste, change, etc.   

import os
import RPi.GPIO as GPIO
# This is going to let us use the BCM pin numbers.  The number on JuiceBox 
# Zero is labeled 
# according to BCM.  See https://pinout.xyz/ for more details on pinouts. 
# This is where you would change the GPIO from pin 16 to 25, if you needed
# to do so on the 
# Juice Box Zero board itself. Default is GPIO 16. In order to change the 
# hardware, you would  
# need to cut the GPIO 16 trace on the board and make a solder bridge over 
# GPIO 25.
# See http://www.blog.juiceboxzero.com/ for more details. 
shutdown_pin = 16  # defines pin 16 as the pin we're watching
GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
def shutdown_callback_function( shutdown_pin ):
    # uncomment the following line to have the Pi tell you that the pin is
    # HIGH and that the callback function has been entered. This is mostly
    # useful for debugging.
    #print("the low battery pin is HIGH now, shutting down.")

    os.system("sudo shutdown -h now")
# This is the magic line that adds pin 16 so it is always being watched.
GPIO.add_event_detect(shutdown_pin, GPIO.RISING, callback=shutdown_callback_function, bouncetime=250)
# Now we wait for something amazing to happen. (i.e. wait for the GPIO)
while True:
   # Do nothing
  • Make sure there are no errors from copying the code and then type:
Ctrl + x
  • Follow the prompts that appear to save the file.
  • Install a crontab job
  • Your Raspberry Pi will now automatically perform a safe shutdown when the battery attached to your JuiceBox Zero 3.2V.