Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Safe Shutdown for Raspberry Pi using PiPower

The PiPower board provides several expansion pins that can be leveraged to enable functionalities like a safe shutdown for Raspberry Pi. Here’s a detailed breakdown of these pins and their functions.

  • GND: Ground connection.

  • BT_LV: Indicates the battery voltage, which is 1/3 of the actual battery voltage.

  • IN_DT: Helps determine if USB power is connected. Outputs high when USB power is detected.

  • CHG: Signals when the device is charging.

  • LO_DT: Signifies low battery voltage status. Outputs high when low battery is detected.

  • EN: Serves as a switch signal. When connected to an external switch and grounded, it turns off the PiPower. This is effective only when the on-board switch is active.

  • LED: Provides power indication. Outputs 5V when powered on. When connecting an external LED, a current limiting resistor is necessary.


These pins are not soldered. You’ll need to solder them using a soldering iron.

For this project, we’ll be focusing on the IN_DT, CHG, and LO_DT pins to determine if an external battery is present, if the USB charging cable is plugged in, and if the battery is low. This ensures the Raspberry Pi shuts down safely when the battery level is low.


Do not plug in both the external battery and the included battery simultaneously!


This table showcases how the PiPower should be connected to the Raspberry Pi:


Raspberry Pi









Download and Test

Sample code for the safe shutdown is provided:

  1. Download from PiPower Github or clone using:

    git clone
  2. Navigate to the examples directory:

    cd pipower/examples
  3. Run the test program to verify the Raspberry Pi can read the power states correctly:


You can simulate different power states by unplugging the USB cable, removing the battery, or altering the Raspberry Pi’s pin connections. The printed messages will indicate the power state. For instance, if the power is supplied only by the battery, the following message will be displayed:

External power disconnected
Not charging
Battery OK


Never connect both the external battery and the built-in battery at once!

Setup Safe Shutdown

To enable the safe shutdown functionality:

  1. In the pipower/examples directory, execute:

    sudo bash
  2. Restart the Raspberry Pi:

    sudo reboot

With this setup, your Raspberry Pi will shut down automatically in cases of not charging or low battery.

Advanced Configurations

For those looking for more customization, you can add further actions in Insert any necessary code under # Do some stuff before shutting down to execute specific actions before shutting down, such as sending a notification to your phone or shutting down certain services.

Remember to run if you make changes to

sudo bash