3

I am working a problem that seems to be fairly popular with the Raspberry Pi community. In essence, I am designing a circuit that will utilize a DS3231, a 555 timer, and a load switch to cut power from the pi when it is off as well as schedule the next time power will be re-applied.

On to my point, How long do I need to wait after a "sudo shutdown -h now" to safely pull power? I am determining the time delay of a 555 timer to propagate the low-going edge of a GPIO pin to the enable of the load switch.

I have also read about the UART TX pin being active in certain states of the OS, namely a halt where the SD card might still be doing something but an active high GPIO might already be pulled back low again. I can't find anything about that right now to figure out if I can leverage this as a final check. I will also have a DS3231 in the circuit to be able to schedule the next wakeup of the pi (we have a headless situation where the pi needs to have programmable control over when it wakes back up).

Questions:

1) How long do I need to wait after a shutdown command before I can safely remove power?

2) Is using a GPIO pin with the python library RPI.GPIO the best solution for this?

3) Is there any other pin like the UART TX that will provide any information as a final analog circuitry check to ensure the power loss won't result in any damage to the SD card?

Once I have this circuit finalized and tested, I'll be uploading my schematics along with PCB layout to the community.

nichollsg
  • 143
  • 1
  • 6
  • Perhaps [this](https://raspberrypi.stackexchange.com/questions/22579/pi-headless-how-to-confirm-that-the-pi-is-shut-down) helps... – RubberStamp Sep 22 '18 at 00:41

2 Answers2

4

Q: How long do I need to wait after a shutdown command before I can safely remove power?

If you add the device tree overlay named gpio-poweroff to your /boot/config.txt file, the answer is simple: You need not wait at all.

Some references for this answer:

  1. A forum discussion titled "How-To Create a GPIO Halt Signal", which went on for over two years with the developer of the gpio-poweroff overlay.

  2. The gpio-poweroff overlay utilizes a driver (gpio-poweroff.c) that provides the implementation and timing details.

  3. Due to my own shortcomings, I wanted to verify that I understood what was written in 1. and 2. above, and so I did an experiment and took an oscilloscope measurement:

    • In /boot/config.txt, the following line was added:

      dtoverlay=gpio-poweroff
      

      Which, by default uses GPIO 26 and is active_high

    • Upon issuing a sudo halt command in my terminal (shutdown & poweroff also work), the waveform shown below was recorded. The scope was set to trigger on a rising edge. The duration of the rising-edge pulse is 100msec, followed by a return to zero of 100 msec, and finally returning to 3.3 Volts where it remains until power is removed from the RPI's 3V3 bus.

gpio 26 after halt with gpio-poweroff overlay

Answer: It is safe to remove power from the RPi at the leading edge of the first pulse.

Power may be removed using a variety of external hardware, if desired. In my case, it was convenient to drive a small transistor with the output of GPIO 26 which in turn drove the coil of a latching relay which gives me a zero-power mode for the RPi. Power may be re-applied using the START switch in the schematic, or via an alarm on a RTC, or a sensor input, or...

power removal external circuitry

Seamus
  • 18,728
  • 2
  • 27
  • 57
2

Many people seem to be concerned about managing Pi power (although unless running from battery this seems pointless).

There is no simple answer to the question you asked; the ACT LED flashes 10 times before extinguishing when shut down correctly. This indicates that often the delay can be quite long, but is not readily detectable.

There is one method which "Drives a GPIO high or low on poweroff", built into recent firmware which can be used - I believe this was included to control power circuitry. NOTE read the instructions carefully as there is some interaction with other services.

Name:   gpio-poweroff
Info:   Drives a GPIO high or low on poweroff (including halt). Enabling this
        overlay will prevent the ability to boot by driving GPIO3 low.
Load:   dtoverlay=gpio-poweroff,<param>=<val>
Params: gpiopin                 GPIO for signalling (default 26)

        active_low              Set if the power control device requires a
                                high->low transition to trigger a power-down.
                                Note that this will require the support of a
                                custom dt-blob.bin to prevent a power-down
                                during the boot process, and that a reboot
                                will also cause the pin to go low.
        input                   Set if the gpio pin should be configured as
                                an input.
        export                  Set to export the configured pin to sysfs
Milliways
  • 54,718
  • 26
  • 92
  • 182