The low battery LED will light up when the battery voltage reaches 3.2V. At this time, the GPIO16 pin will go HIGH. So in order to safely shutdown the Pi when the battery is low, we'll run a script in the background that listens for a rising edge on pin 16, then shuts down the Pi.

So first, use your editor to create a new file called safeshutdown.py in the /home/pi directory. Add the following:

import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) shutdown_pin = 16 GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) def shutdown_callback_function( shutdown_pin ): os.system("shutdown -h now") GPIO.add_event_detect( shutdown_pin, GPIO.RISING, callback=shutdown_callback_function, )

Refer to the JuiceBox Zero Github Repository for more details.

Now, edit the crontab by typing the following in a shell:

sudo crontab -e

And add the following to the end of the file:

@reboot python /home/pi/safeshutdown.py &