1

Background: I have my RPi running connected to a small UPS. This works fine when the power goes out for a short time, but I want to have the Pi power down when the power is off and it's running from the UPS battery. The UPS doesn't have any USB capability or any other way for the Pi to sense power loss.

My idea is that I can use the power wires from an external USB power supply (plugged in to mains power without the UPS) to run a circuit that will short GPIO3 when the power goes out. I've read (here) that with the line dtoverlay=gpio-shutdown in /boot/config.txt, shorting GPOI3 will gracefully shutdown the pi.

That article uses a physical button to short GPIO3 to ground, but I want to have this happen automatically when my external circuit looses power. I was thinking I could somehow discharge a capacitor on power loss, triggering a transistor that would short GPIO3 for a second and cause the Pi to power down. Or maybe I can just use power from another GPIO to do it when it senses power is lost from the external USB wires? I'm kind of lost as to how to go about this. Or is there some easier way to accomplish what I'm trying to do? Thanks!

2
  • When you say, "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?
    – Seamus
    Jul 28, 2021 at 21:59
  • Yes, you are correct. The RPi is powered from a wall wart that is plugged into the ac outlet on the UPS.
    – fivestones
    Jul 30, 2021 at 15:28

2 Answers 2

4

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:

  • shutdown
  • halt
  • poweroff

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:

schematic

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.

6
  • This is SUPER helpful. Thanks! Two things I'm not sure about: in your schematic, there are three grounds--the wall wart ground, the Rpi ground pin, and in between them another ground symbol. Is this a separate ground of some sort? Or is is just there to point out that that whole area of the schematic is a common ground between the Rpi and the wall wart? Also (unless I am mistaken), most wall warts don't have a ground as far as I know. Will this have problems if I am using such a wall wart (one without ground)?
    – fivestones
    Jul 30, 2021 at 15:32
  • One other question: what does ACPWR# stand for? I'm assuming it is a pin on the Pi, and I guess GPIO 23 based on what you wrote about configuring the gpio-shutdown dtoverlay. But I couldn't find references to the same acronym anywhere online with regards to raspberry pis.
    – fivestones
    Jul 30, 2021 at 15:34
  • 1
    There is only one ground (reference voltage) in the DC circuitry. Inside the dashed-line box is your Raspberry Pi - there are several pins in the GPIO header that are labeled GND - you will find them identified here. A ground is simply a reference voltage, and the schematic shows (in this case) that they are "tied together" - this ensures all voltages in the circuit are referenced to the same level. Think of it in the same way you do as "sea level".
    – Seamus
    Jul 30, 2021 at 17:00
  • 1
    ACPWR# is the designation I chose for the signal generated at the collector of Q1 - think of it as AC POWER - NOT; shorthand for "when ac power is present, this signal is low"
    – Seamus
    Jul 30, 2021 at 17:01
  • 1
    @fivestones: Thanks for the feedback - it's always good to hear when things work out.
    – Seamus
    Aug 6, 2021 at 5:58
1

The documentation does not mention "short … to ground". It actually says "Initiates a shutdown when GPIO pin changes".

You can use any logic circuit to pull the pin down.

I think use of GPIO3 is problematic as it also states "After shutdown, the system can be powered up again by driving GPIO3 low" so you need to remove any external drive. I use GPIO21 to shutdown which has far fewer problems.

The whole point of a UPS is to AVIOD power loss, so using the Pi doesn't make sense.

You will need to design some circuit to sense power loss to shutdown the Pi, but this is an EE question, not Pi specific. Many UPS have an indication of power loss.


Putting external power on a GPIO is fraught with danger! Use whatever input you choose to control a transistor, which can quite safely be used. Expecting someone else to design your circuitry (without any specifications) is unrealistic.

1
  • Thanks for the response! My UPS doesn't have any indication of power loss which is why I am trying to do this some other way. I'm pretty inexperienced in the world of EE. Can you elaborate any further on "You can use any logic circuit to pull the pin down"? Could i just wire in some external power to the Pi GPIO pin and expect the Pi to shutdown when that power disappears? Or will I break the Pi if I try this?
    – fivestones
    Jul 28, 2021 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.