Power Management HAT (B)

From Waveshare Wiki
Jump to: navigation, search
Power Management HAT (B)
Power Management HAT (B).jpg







Power Management HAT(B) is a Raspberry Pi power management control board, which can provide a set of extremely powerful power management for Raspberry Pi, such as timing switches, measuring working voltage and current, etc. The load transient response function enables fast switching among multiple power supplies.


  • Onboard MCU (RP2040), powerful and easy to use.
  • Onboard PCF8523 RTC chip, provides precise RTC information.
  • Onboard MP28167-A buck-boost DC chip for fast load transient response.
  • Protection circuits such as power supply anti-reverse, counter-current proof, etc. ensure stable and safe operating.
  • Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards.
  • Onboard user-defined button, can be used for Raspberry Pi start-up, safe shutdown, or other customized functions.
  • It can detect the input power supply voltage and the working current, and it can be programmed to cut off the power supply immediately to ensure the working life of the lithium battery.
  • Voltage/current monitoring circuit, monitors the Raspberry Pi operating voltage and current in real-time.


  • Main chip: RP2040
  • Communication interface: UART + GPIO
  • Download interface: USB + SWD (debug)
  • Baud rate: default 115200bps (configurable)
  • Power supply: USB/PH2.0 connector
  • USB port voltage: 5V
  • PH2.0 connector voltage: 3.3-4.2V (3.7V Li battery (NOT included))
  • Built-in circuits: battery anti-reverse, counter-current proof, voltage/current monitoring
  • Dimensions: 56.5mm x 65mm
  • Fixed hole diameter: 3.0mm

Hardware Connection

RP2040 GPIO Description
GPIO0 UART0_TX pin, connect to Raspberry Pi D15 pin
GPIO1 UART0_RX pin, connect to Raspberry Pi D14 pin
GPIO6 I2C1_SDA pin, connect to II2 SDA pin
GPIO7 I2C1_SCL pin, connect to II2 SCL pin
GPIO19 User key pin, generally for controlling the power switch
GPIO21 RTC interruption pin, RTC chip will be pulled down when the RTC clock is triggered
GPIO22 Raspberry Pi soft shutdown pin, connect to Raspberry Pi D20 pin
GPIO23 Raspberry Pi run pin, connect to Raspberry Pi D21 pin
GPIO24 Power control pin, when it is pulled low, the power is cut off, otherwise, the power is on
GPIO25 Status LED pin, used to display the current operating status
GPIO29 Input voltage acquisition hardware, the voltage here is about 1/10 of the input voltage

User Guide



  • Li-ion and Li-po batteries are quite unstable. They may cause fire, personal injury, or property damage if they're not properly recharged or used.
  • Do not reversely connect the polarities when recharging or discharging the battery. Do not use an inferior charger/charging panel to recharge the battery.
  • Do not mix use old batteries with new ones, avoid using batteries of different brands.
  • When buying a Lithium battery, should always make sure the battery specification is compatible with the expansion board. *Choose batteries from the formal manufacturer, and ensure the batteries will work stably and safely by aging test.
  • Lithium batteries have a limited cycle life, they will also deteriorate as time goes by. Should be replaced with new ones when the batteries reach their max cycle life or work over two years, whichever comes first.
  • Should be placed carefully and properly, keep it away from inflammable and explosive articles, away from children, and avoid any safety accident caused by careless storage.

Working Principle & Demo

GPIO Protocol (Configurable Protocol)

  • Based on software compilation, this protocol requires RP2040 and Raspberry Pi to run the demo.
  • As the GPIO headers of RP2040 and Raspberry Pi are connected, the running state of Raspberry Pi can be detected and its shutdown can be prompted.
RP2040 GPIO RPI GPIO Description
GPIO22 D20 Raspberry Pi soft shutdown pin, pulled low by default by RP2040, pulled high when prompted for Raspberry Pi software shutdown.
The Raspberry Pi should detect that the pin is pulled high and then execute the shutdown procedure.
GPIO23 D21 Raspberry Pi run pins, pulled high by default by the Raspberry Pi after powering on.
The RP2040 can detect whether the Raspberry Pi is powered on or not through this pin.

Working With Raspberry Pi

Configure Pico Compile Environment

  1. Download the installation script.
    cd ~
    wget https://raw.githubusercontent.com/raspberrypi/pico-setup/master/pico_setup.sh
  2. Give run permission and run:
    chmod +x pico_setup.sh
  3. Restart the Raspberry Pi:
    sudo reboot


  • Power Management HAT (B) has connected SWD (debug) to the Raspberry Pi interface, you can directly use the debug interface to download programs or debug.
  1. Download the library source code required for OpenOCD.
    cd ~/pico
    sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-
  2. Download the OpenOCD source code.
    git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1
  3. Compile and install:
    cd openocd
    ./configure --enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio
    make -j3
    sudo make install

Soft Shutdown Script

  • Power Management HAT (B) requires a script to enable soft shutdown and run detection of the Raspberry Pi.
  • Download the script and run it.
wget https://files.waveshare.com/upload/4/44/Power-Management-HAT.zip
unzip Power-Management-HAT.zip
cd Power-Management-HAT
sudo chmod a+x Power-Management-HAT-Setup.sh
sudo ./Power-Management-HAT-Setup.sh
  • The result is shown below, when prompted "Would you like to reboot now? y/n", enter "y" to reboot the Raspberry Pi.
  • You must reboot the Raspberry Pi for this operation to be effective, if the Raspberry Pi is not rebooted, you must manually reboot the Raspberry Pi to take effect.
Soft Shutdown Script01.png

Note: You should check whether there is an error reported after the execution of the above operation, there is an error reported according to the prompts to correct before continuing.

Demo Usage

Get the Demo

cd ~
wget https://files.waveshare.com/upload/2/27/Power-example.7z
7z x ./Power-example.7z

Compile the Demo

  • The default demo is for periodic power-on.
cd ~/Power-example/build/
export PICO_SDK_PATH=/home/pi/pico/pico-sdk
cmake ..
make -j

Program the Demo

  • Note: Program the demo will cause the RP2040 to reset and the power supply to be interrupted.
  • Therefore, you should use the Raspberry Pi's power port for power supply instead of the Power Management HAT(B) when programming.

Program via SWD Interface

  • We already connect the SWD interface of the RP2040 to the Raspberry Pi, and you can directly operate it through Openocd.
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c "program ./Power_Management_HAT.elf verify reset exit"

Program via USB

  • The Raspberry Pi Foundation has cured the UF2 bootloader within the RP2040 by following a few steps:
  1. Connect the USB connector of the Raspberry Pi and the USB connector of the board.
  2. Press the Reset key and the BOOT key.
  3. Release the Reset key first and then the BOOT key.
  4. Drag and drop the corresponding files into the pop-up mass storage.
  • You can refer to the following diagram (the diagram is the official Raspberry Pi GIF for the Hello World demo).

Power Management B09.gif

UART Listening

Enable UART Function

  • Execute the following commands to configure the Raspberry Pi.
sudo raspi-config
  • Enable the UART function:
1. Choose Interfacing Options and Enter:
UART Listening01.png
2. Select Serial Port:
UART Listening02.png
3. Select close the serial port debug function.
UART Listening03.png
4. Choose to enable the serial port hardware.
UART Listening04.png
5. System prompts as follows:
UART Listening05.png


  • Minicom is a common command-line serial debugging tool for Linux.
  • Its function is similar to that of HyperTerminal under Windows, and it can control external hardware devices via a serial port, which is usually used for managing embedded devices.
1. Install and run minicom:
sudo apt-get install minicom -y
sudo minicom -D /dev/ttyS0
2. Now you can see the running information returned by the demo timer (here the demo is a timer switch demo).

Power Management HAT (B)11.jpg

Demo Description

  • To switch between demos, you need to turn on the corresponding initialization and loop functions in the main function, and block the functions of the remaining demos.
  • In the following figure, Period_Time demo is enabled.

Power Management HAT (B)06.jpg

Common Rules/Functions

Status Indicator

  • The status indicators all follow the rules below:
  1. In the shutdown state or power off state, the status indicator flashes at a frequency of about 0.83HZ (slow flashing).
  2. Boot-up state or power-on state, the status indicator about 2.5HZ frequency flashing (fast flashing).
  3. Waiting for the power on or waiting for the power off, the status indicator is always on.

Judging by Current

  • Judging by Current
  1. This function can judge the device on and off in real time and turn off the power to save energy.
  2. This feature is disabled by default (can be turned on via ". /examples/examples.h/USE_CURRENT_TO_CHECK_PI_STATUS").
  3. By determining whether the current value is less than the shutdown current if it is, the device is considered to be shut down, and the power is turned off (shutdown current setting . /examples/examples.h/Shutdown_Current_Ma, you need to adjust it according to your own device, otherwise the function will not work or the device will not be powered on).

Power on and off

  • Power on
  1. Power on via RTC or the key.
  2. Turn on the power, and determine whether the power has been turned on through GPIO, the default maximum wait time for power on is 90 seconds, more than the maximum value is considered to have been turned on (the maximum wait time is set by lib/Power_Management_Lib/Power_Management_Lib.c/Power_Wait_Time_S).
  • Soft shutdown
  1. Soft shutdown triggered by key press.
  2. The default maximum wait time for power off is 60 seconds, if the maximum wait value is exceeded, the power will be forced to shut down (the maximum wait time is set by lib/Power_Management_Lib/Power_Management_Lib.c/Wait_Boot_). Time_S).
  • Forced shutdown process
  1. Shutdown triggered by RTC, long key press, and current.
  2. Forced power off and device shutdown.

Button_Ctr Demo

  • Implement function: Use the PWR button to power on/off.
  1. When the Raspberry Pi is turned off.
    • When the PWR button is pressed, it turns on.
  2. When the Raspberry Pi is powered on.
    • Default long press PWR button more than 2S but less than 8S, try to soft shut down the Raspberry Pi.
    • Default long press PWR button more than 8S then try to power off directly.

Period_Time Demo

  • Timed power on/off:
  1. Power on when the time reaches the specified time Power_On_Time (default is 9:00:30).
  2. Shut down when the time reaches the specified time Power_Off_Time (default is 10:00:00).

Cycle_Time Demo

  • Cycle power on/off
  1. Power_On_Keep_Time (default is 1800S).
  2. Power_Off_Keep_Time (default is 3600S).






The theoretical maximum output current is 3A, the actual maximum output current is limited by the power supply, such as using a 3.7V lithium battery (when fully charged), the maximum output current can reach 2A.


It is related to the power supply and output current. If the matching 3000mah3.7V lithium battery is used, the output current will remain at 1A, and it can be used for about 2 hours (depending on the temperature and battery status).



Technical Support

If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 AM GMT+8 (Monday to Friday)