This is a reasonably straightforward implementation. However, I'd suggest your first step be to discard the article you've referenced - it's not quite correct overall. Also, because the semantics are confusing, I want to make this point very clear: You may shutdown the RPi when Mains power is out, but that is not the same as removing power from the RPi. The RPi will continue to consume power until you either unplug the power supply from the UPS (or Mains), or "pull the plug" from the RPi, or shut down the UPS.
In other words, using the dtoverlay for gpio-shutdown
, or any other technique, will allow you to make the RPi ready for safe power removal, but will not actually remove power. As the RPi is currently built, power removal requires external hardware.
There are 3 different commands that will cause the RPi to "shut down". These are "standard" Linux commands, and on some platforms they have different functions, and result in different machine states - refer to the man
pages of each for the details. However, on the RPi, all 3 commands wind up in the same place: the RPi will continue to draw power (if available), no processing is taking place, and one may safely disconnect power from the RPi.
The 3 commands are:
With that out of the way, my answer follows:
Assuming that your RPi power supply is plugged into one of the mains outlets on your UPS, you have correctly concluded that part of the solution you're seeking is to detect that mains power is offline. Here's one way to do that:

simulate this circuit – Schematic created using CircuitLab
When the "Wall Wart" is un-plugged or mains power is down, Q1 will be "turned off", and the voltage at Q1 collector terminal (ACPWR#
) will be a logic "high" - same voltage as the other terminal of R2: 3.3V in this case.
When the "Wall Wart" is plugged in and mains power is on, current will flow from the Wall Wart through R1, Q1 will then be "turned on", and the voltage at Q1 collector terminal (ACPWR#
) will be a logic "low" - or GND as shown here.
And so the ACPWR#
output is 3.3V when Mains power is OFF, and GND/0V when Mains power is ON. And we can connect ACPWR#
to any GPIO pin on the RPi to "signal" the status of Mains power. That GPIO pin may be read in software, and if it is HIGH (Mains power is down), your software can issue a command to shutdown the system.
Done this way, you have the option (in software) to allow the RPi to run from UPS-supplied power for a period of time. This may prove useful by avoiding an RPi shutdown for a transient, short-duration power outage.
Using the dtoverlay for gpio-shutdown
is also possible, but may be problematic if used on GPIO 3 (referring to the documentation):
GPIO 3
, like all the other GPIO pins on RPi, is a multi-function pin. Its other functions include use in the default I2C
interface, and as a "start" or "run" input. Even if you don't use I2C
, GPIO 3 may also be used to re-start the RPi as well as shut it down! This duality is useful if one wants a single pushbutton to turn the RPi on and off, but may create issues in this application with an unintended restart.
If you elect to (potentially) forego the advantage of a delayed shutdown, and wish to use the gpio-shutdown
dtoverlay, it should be configured as follows:
dtoverlay=gpio-shutdown,gpio_pin=23,active_low=0
It may be possible to get a delayed shutdown by setting the debounce
parameter to a large value; e.g. debounce=300000
might yield 5 minutes of delayed shutdown as the parameter value is in milliseconds. I have not tried this, and the range of values for this parameter aren't specified in the documentation.
Summary:
This answer provides a simple hardware solution for detection of a power outage, and two options to put the RPi in a "shutdown" state. As noted above however, there is no option that will remove power from the RPi. That is only possible with additional hardware, and was considered beyond the scope of this question.
"I have my RPi running connected to a small UPS."
, we're required to make an assumption about your UPS. I would assume that you have plugged your USB power source (your "wall wart") into an ac outlet on the UPS. Can you confirm this - or clarify it?