This project uses 2 commonly found breakout boards and one custom board to handle startup and shutdown. A breakout board with the MP2315 buck converter is used to step down the voltage to 5 volts and introduces an enable input.  The other breakout board is the INA226  Two GPIO pins are used on the Rasberry PI.  One as input for a pushbutton switch to trigger safe shut down of the Pi (GPIO pin 24).  Another as output to disable the buck converter (GPIO pin 26).  Also, the I2C interface is used to communicate with the INA226 (GPIO pin 02 and GPIO pin 03).

MP2315 Adjustable Buck converter

INA226 Bi-Directional Current and Power Monitor

Custom Board - RPI Shutdown Interface

RPI Shutdown Interface Design

The buck converter board is attached at J1 with P4 as the Enable pin of the buck converter.  When the push button is pressed, 12v is supplied through the 330K resistor R4 to the Enable pin of the buck converter, which begins to turn on and supply 5V power on pin P3 and power to the Raspberry Pi (RPI), which starts up.  The 5V passes through R1 and the Diode D2 and latches the Enable pin of the buck converter high.  This keeps the buck converter enabled keeping power supplied to the RPI even after the push button is let go.  In this process Zener Diode D1 keeps the voltage on P4 below 5.6 volts.  The RPI is attached at JP2 with pin 2 being a GPIO pin.  While the RPI is powered up it keeps the GPIO pin high (3.3v).  The 10K resistor R2 ensures the current driven into the GPIO pin stays low enough to not damage the pin - needed because P4 could be as high as ~5.6 volts.  When the RPI has an internal request to be shut down, it does a safe shutdown and at the end of that process it pulls the GPIO pin low.  This pulls down the enable on the buck converter, and it stops producing 5V on it's output, reducing the latch voltage.  The buck converter shuts down and the RPI is no longer receiving power.

The RPI has a different pushbutton  on another GPIO pin to trigger shutdown request.   The same process occurs if the user requests shutdown from the command line.

MP2315 Adjustable Buck Converter Modifications

There are two modifications to make on the buck converter.  The first is to set it to an output voltage of 5V.  This can be done by following the directions for the converter and cutting the proper trace and bridging the pads for 5V output with some solder.  After this modification you check the voltage with a meter to guarantee its in the range of 4.95 to 5.2 so that it does not cause problems with the Raspberry Pi.

The next modification is to remove the pull-up resistor on the enable pin on the board.  This is needed to allow power to be off by default.See the image below. This resistor can be carefully removed with the tip of a soldering iron



Updating the Raspberry Pi config.txt file

We want to configure gpio pin 24 as an input pin that when grounded, will start the shutdown.  Also, configure gpio pin 26 as an output to provide a power-off signal to the buck controller.  This can be done by adding the below two lines to the /boot/config.txt file on the Rasperry Pi.

# input pin to start the shut down sequence
dtoverlay=gpio-shutdown,gpio_pin=24,active_low=1,gpio_pull=up
# output pin to enable power source
dtoverlay=gpio-poweroff,gpiopin=26,active_low=1

Test the Raspberry Pi Startup and Shutdown

By pressing the pushbutton attached to the Shutdown Interface board, the Raspberry PI should start up.  Pressing the pushbutton between ground and pin GPIO24 should shut down the Raspberry Pi.

Adding the Voltage, Current, and Power Monitoring

A Python script is used to collect the voltage and current and track power used.  The script is launched at startup using a systemd service.  If the battery voltage goes below a threshold, it triggers a shutdown with a 5 minute warning.  The script uses shared memory to give other Python programs access to the data.  For example, I have a command line script that displays the current battery voltage, current, and capacity.

The Python scripts and instructions can be found in this gitlhub repository: Raspberry-Pi-Battery-Power-Monitor

After you install the scripts, you will be able to run the command battery and get the following information:

pi@pi3:~ $ battery
battery voltage = 12.451 volts.  Run time = 0.3071861111111111 hours
battery current = 332.275 mA.  Capacity used = 98.555 mAh
CPU temp = 49.93 deg C.

This result is for a PI 3B with a TFT display, so draws significantly more power than a Raspberry Pi zero with no display.

Once the battery falls below the threshold in the script - designed for a 3S Li Ion (3.5v x3 = 10.5v) you will get a warning and the Pi will shut down within 5 minutes.

pi@pi3:~ $ 
Broadcast message from root@pi3 (Sat 2022-12-17 15:27:19 CST):

Battery Voltage Low
The system is going down for poweroff at Sat 2022-12-17 15:32:19 CST!