3

I'm building a kiosk app for a raspberry pi, and the final intention is that it will sit in a pelicase with all the external hardware, with one power cord in. My program has a quit button which safely shuts down the pi by running sudo halt, but it would make it easier if the pi could automatically shutdown when it detects that it has been unplugged. Could this be done if I had a capacitor on the power rails and when the power was disconnected the pi could measure the power drop and shutdown safely if it detected a loss of power? Thanks.

  • Jut to add - this use case covers pulling the power cord, or a power-outage (which happen more or less frequently depending where your kiosk is located). – Ralph Bolton Aug 12 '19 at 10:26
1

You seem to be looking for something like https://juice4halt.com/ : a UPS which provides a GPIO signal to the RPi which can be used to initiate shutdown when the external power is lost.

Now, sudo halt is not some kind of magic which makes the shutdown safe by releasing pixie dust. You can write a script to do the same thing that halt does: prevent new processes from starting, kill existing processes, flush all filesystem caches and finally unmount all filesystems. I just don't think you have a good reason to re-implement it yourself.

|improve this answer|||||
  • thanks for the help, I'll look into juice4halt – Nadim Aug 13 '19 at 8:00
1

What you're asking for is impossible. There's no battery backup on the RPi without additional hardware.

Pulling the power may be OK 9 times out of ten, but every time you pull the power you risk corruption of your SDCard.

Even if you move to a HDD or SSD for the root filesystem you risk corruption with a sudden power loss.

How hard is it to use sudo poweroff then wait ten seconds for that to complete? You know it's complete when the activity LED blinks ten times.

|improve this answer|||||
  • Just to add you can increase your odds of "getting away" with a power-cut shutdown by cutting the number of writes you make to your SD. There are articles about this around, but turning off logging and moving your working data set into a ramdisk (or just into RAM) can avoid a lot of it. However, all that does is increases your odds - if you need a guarantee, then you need a power-backup solution (eg. a small battery back up), or an entirely read-only storage solution. – Ralph Bolton Aug 12 '19 at 10:25
1

No computer system which writes to internal storage can just be powered off without risk of loss. The Pi is no different in this respect.

Conventional computers have a managed shutdown and/or battery backup.

It is not difficult to provide the same to a Pi (the additional circuitry is likely to cost more than the Pi) and there are many articles on this, and a number of commercial products.

NOTE a capacitor is unlikely to provide sufficient voltage for long enough.

The other option is to use a read only filesystem, and store working data in volatile storage (RAM) - it depends on your application if this is applicable.

|improve this answer|||||
  • 1
    Re: your note about storing data in RAM; I have use AUFS (RAM fs + read only ext4 mount) to successfully implement a Linux driven embedded project that can handle hard power events. Something similar to what is mentioned here: linuxquestions.org/questions/linux-general-1/… – sbell Aug 12 '19 at 2:53
1

If you don't need to write to your filesystem (i.e. SD Card) regularly, this can be done pretty safely by making the file-system read only. This will ensure there never are any writes occurring to the SD Card at the time of power loss.

The operating system can use a tempfs for any temporary files etc, however, this means that you cannot update the system without re-flashing your SD-card or knowing quite a lot about filesystems in Linux (remounting++).

Adafruit has a nice tool for doing this, see: https://www.raspberrypi.org/blog/adafruits-read-only/. They also have a guide that walks through the tool and shows what it does: https://learn.adafruit.com/read-only-raspberry-pi/.

You can of course choose to only do some of the things that script does - like only enabling read-only and moving files to tmpfs, instead of also removing unwanted packages and configuration.

|improve this answer|||||
0

To achieve this thing you need two things.

  1. First, the power failure detection circuit.
  2. A power backup system that can give power back up to shut down the raspberry pi.

There are many ways to implement the power detection circuit. I am giving an example of this

schematic

simulate this circuit – Schematic created using CircuitLab

In this circuit, the R1 and R2 you have to chose such that RPI gpio will get 3.3V, 3mA current. When the power supply presents the gpio will get a logic high(3.3V) signal and when failure will occur gpio will get logic low(0V).

For power back up you need a supercapacitor circuitry that can store 5V*2.5mA * 60sec energy minimum such that it can provide power to RPI when it performs shut down the operation. This circuitry should be placed in between the dc power supply and Rpi.

|improve this answer|||||
0

I don't think a "capacitor", or other esoteric power supply is needed.

If you can find a USB battery bank which is able to charge and supply power at the same time, it functions as a pretty reliable UPS. I do something similar by powering one of my Pis from a laptop's (secondary home server) USB port.

I'd suggest just trying out the battery-bank thing, maybe with a relay so the pi can actually turn itself off, and kill its power input properly. If you need the auto-off functionality, you can use a wiring setup like suggested in Prayuktibid's answer, connected to the +5v powering the battery bank.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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