Saturday, January 09, 2016

Graceful shutdown for the Raspberry Pi

The Raspberry Pi is a great little board, but because it runs Linux, you risk corrupting the file system on the SD card if you simply switch off the power. The standard advice is to type "sudo shutdown -h now" and wait until the shutdown process has finished, or has at least unmounted all the file systems, before switching off the power.

What if the RPi is part of some embedded widget with no keyboard and screen? How then to initiate a shutdown, and how to know when it's finished? It was while contemplating the creation of just such an embedded widget that I hit upon this piece of brilliance. First, build this circuit.


How does this work? When you plug in the wall wart, we don't want to draw power from the 9 volt battery yet, so we want Q1 to be off. This is accomplished by using Q4 to keep the Q2/Q3 Darlington turned off. The wall wart power flows thru the three diodes in parallel and operates the switching voltage regulator (I picked up a bunch of these from some Chinese outfit on eBay). Meanwhile C1 charges to around 9 volts.

What happens when you yank the wall wart? Why, magic of course. Magic happens. Specifically, Q4 turns off, and C1 begins to discharge thru R1 and R6, turning on the Darlington pair, which turns on Q1 so that now the switching regulator is running on battery power. The battery power remains on for some multiple of the RC time constant (100 uF * 500K = 50 secs), and you end up with about two minutes to get the Raspberry Pi to shutdown.

I've started a Github repository for this, and it includes the two scripts that make graceful shutdown a service that starts automatically when the RPi boots.