7

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.

Nadim
  • 73
  • 1
  • 1
  • 5

6 Answers6

4

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.

Araho
  • 141
  • 2
4

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.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147
2

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.

Prayuktibid
  • 157
  • 5
2

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.

Milliways
  • 62,573
  • 32
  • 113
  • 225
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.

Dougie
  • 5,381
  • 11
  • 22
  • 30
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.

bobsburner
  • 121
  • 4