Safe power-down for RaspBerry Pi.
Power-off a Pi without a "shutdown" of the operating system will result in a "operating system crash". This endangers the integrity of the SD-card. We must therefore "delay" the power-off until the Pi has closed the io on the SD-card and HALTed. This circuit will do this.
Pressing the Power button on the left will charge the left upper capacitor. The PNP-transistor will conduct, and the gate of the MOSFET will be "high". The MOSFET conducts, the rightmost LED will glow and the Pi (symbolized by R=10 Ohm on the far right) will be powered, and it will boot.
The capacitor slowly discharges, thru the base-emitter circuit of the PNP-transistor and the 150kOhm resistors.
The switch stands for a GPIO pin A: it gets HIGH = 3.3V when a Python script starts when the Pi has booted. The capacitor is now charged thru the NPN-transistor, so the power will remain. Obviously this pin A must get HIGH within the period the capacitor is completely discharged. The green LED will glow when the pin is HIGH. The circuit is now stable, and delivers power to the Pi.
A shutdown will make pin A eventually LOW (=0 V), and the capacitor will discharge slowly. The Pi has the discharge period to completely HALT.
30 Seconds is normally more than enough. You can decrease the period by decreasing the value of the capacitor, or reducing the resistor to the base of the PNP-transistor.
The (Python) script in the Pi to control pin A also uses the Power button for controlled shutdown. The script checks another GPIO pin B, connected to the button via a diode. The two resistors will limit the HIGH voltage to 3,3V (safe for the Pi). Let's assume power = ON.
A short push on the Power button makes pin B LOW, and the script will issue a "shutdown halt after 1 minute". A long push (> 1 sec) will result in "shutdown reboot after 1 minute".
The script will blink the green led when a shutdown is planned (NB: a duty cycle of 10% is still enough to keep the capacitor charged). While a shutdown is planned a push will cancel the shutdown.
Parts and design considerations:
N-mosfet = IRLZ24N, it has a voltage drop of only 80 mV. NB: A P-mosfet (in the Vcc line) generally has a greater ON-resistance than an N-mosfet. PNP and NPN transistors are low-power like BC556B, BC547B. The capacitor across the lower resistor (for pin B) is necessary for "debouncing": I have interrupts attached to my buttons in the script. The Pi-input-pins are connected thru 10kOhm resistors, for safety.
I have tested the circuit and the script on a RPi1 B+ with RuneAudio. Discharge period is now about 30 seconds. Interested in the Python-script? Please send a comment.
So far , so good (2015-12-11): the hardware works, and the software too, and I don't see any flaws.
I will use the circuit on my Pi2 /Hifiberry DAC+ RuneAudio music-and-radioplayer.
|