Electronic – Circuit to safely power-down Raspberry Pi

capacitordischargepower supplyraspberry pi

I want to use a Raspberry Pi as an XBMC server in the car. The XBMC docs say that you should always use the shutdown command before disconnecting the power. I don't want to have to (tell my wife to) log into the Pi and shut it down before turning the car off – I want to be able to

I've been thinking that it should be possible to create a simple circuit with a capacitor and probably a diode to detect when the power supply was disconnected (and raise an interrupt on one of the GPIO pins) but the capacitor would provide current long enough for the system to shut down properly.

Does this look correct and sufficient?

second draft

The circuit will be powered by a car battery – 12.6 to 11.7V. The Raspberry Pi takes 5V (5.25 to 4.75V) and draws 700-1200mA. I haven't timed it yet, but I'm guessing the shut-down process probably takes around 5 seconds.

So I suppose what I need to know is:

  • What kind of capacitor would I need to store enough charge to keep the Pi going long enough for XBMC to shut down properly?

  • Given that the Rasperry Pi's GPIO port takes 3.3V, what's the best comparator/op-amp to use (I suppose I could use a couple of resistors to bring the output down from 5 to 3.3)

  • Would there be any benefit in having the GPIO line normally high or normally low?

Best Answer

An alternative strategy would be to power the Raspberry Pi continuously and use the ignition line to initiate the power down sequence. I've done that in the past but with systems where the exact solution wouldn't apply to a Pi but in general:

Use a DC-DC converter for the best efficiency, there are many examples around but the following is one example of something that would be convenient to use and it can supply 1A at 5V from a 6.5V to 32V input:

http://www.digikey.com/product-detail/en/V7805-1000/102-1715-ND/1828608

A car supply can be quite harsh, so you might want to use a 30V TVS diode across the input to protect against spikes with a chunky Schottky diode with the anode at ground and the cathode at the 12V input to protect against negative voltages along with either a normal fuse or a PTC resettable fuse in series with the connection between the car's power and your system. Otherwise you may be able to 'hack' a car to USB charger that should already have all that in place.

I'm not sure what a Raspberry Pi draws in normal idle mode, but presumably well under 500mA which is the maximum USB can supply and more likely 100mA. Say it's using 100mA at 5V that will be under 50mA at 12V using that circuit, a car battery is normally in the order of 50Ah so that would be around 20 days to drain the battery to 50%. If the car is in regular use there's probably no need to go any further, and you may just be able to leave it running and just turn off any peripherals you're not using.

Otherwise for detecting the ignition change either way and both informing the Pi it needs to shut down followed by removing power a minute later the most practical way is probably to use an external microcontroller that drives a FET. It could be done with discrete logic but you also need to make sure power is re-applied when the ignition goes high, so it's not an entirely trivial excercise but parts costs will be lower than using a large cap.