Note

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.

_images/io_pin.png
  • 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.

Note

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.

Warning

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

Wiring

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

PiPower

Raspberry Pi

IN_DT

GPIO17

CHG

GPIO18

LO_DT_PIN

GPIO27

GND

GND

Download and Test

Sample code for the safe shutdown is provided:

  1. Download from PiPower Github or clone using:

    git clone https://github.com/sunfounder/pipower.git
    
  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:

    python3 read_all.py
    

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

Warning

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 enable_safe_shutdown.sh
    
  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 safe_shutdown.py. 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 enable_safe_shutdown.sh if you make changes to safe_shutdown.py.

sudo bash enable_safe_shutdown.sh