Introduction: Raspberry Pi Shutdown Button
As everyone at some point will recognise the Raspberry Pi doesn' t include any kind of shutdown button. So the only way to power it off is by unplugging it from the power supply. To make sure you get no corruption of your data files on the SD card you should shutdown the Raspberry before powering it off. So the whole procedure can be a bit annyoing especially if you want to use the Raspberry as some kind of embedded PC e.g. a media server or an internet radio.
The Raspberry Pi Shutdown Button provides a small circuit that includes the features:
- sending a shutdown signal to the Raspberry if the Shutdown Button is pressed
- waiting for the Raspberry to shut down
- Powering off the Raspberry after save shutdown
- Powering on the Raspberry after the Button is pressed again
- LED light indicating the current state: On / Shutdown / Off
Step 1: Parts
You need the following parts:
- Atmel AVRtiny with 8 Pins (attiny13 / attiny85)
- 2 BC337 transistors
- 1 relay
- 1 electrolytic capacitor 680µF
- 1 green LED
- 4 pin connectors
- a veroboard
- some kind of AVR ISP programmer (I used AVR ISP MKii)
Step 2: Schematic
This is the Designspark Schematic I used for the Raspberry Shutdown Button. The AVR is connected to a relay that switches the supply voltage of the Raspberry Pi. A led indicates that the Raspberry is switched on. In shutdown mode it will start blinking. There is also a shutdown signal that tells the Raspberry to halt before the power is cut off.
Step 3: AVR Programming
Here is the short listing of the AVR program.
Step 4: Raspberry Pi Shutdown Script
The Raspberry Pi needs a script that tells it to shutdown if it gets the shutdown signal. So the next thing we' ll do is write a Python script that waits for this signal on a specified GPIO pin. Here we' ll watch out for GPIO pin 7. On the model B this is GPIO4. Save this short script in something like /home/pi/pishutdown/pishutdown.py.
#!/usr/bin/python # Import the modules to send commands to the system and access GPIO pins import RPi.GPIO as gpio import os #Set pin numbering to board numbering gpio.setmode(gpio.BOARD) #Set up pin 7 as an input gpio.setup(7, gpio.IN) # Set up an interrupt to look for pressed button gpio.wait_for_edge(7, gpio.FALLING) # Shutdown os.system('shutdown now -h')
Next we need a shell script that starts our Python script with root access. Put the shell script in the same directory as our Python script.
cd / cd home/pi/pishutdown sudo python pishutdown.py cd /
Add a logging directory by typing:
Use crontab to autostart the script. Open the crontab editor by typing sudo crontab -e in the Console. Append the following line:
@reboot sh /home/pi/pishutdown/pishutdown.sh >/home/pi/pishutdown/logs/cronlog 2>&1
Step 5: Put Everything Together
Now connect the Raspberry Pi and the Shutdown board via the GPIO pins. Add supply voltage to the shutdown board and after booting properly press the Shutdown button for about a second. Now the led should start blinking and the Raspberry should begin the halt sequence. After a while the supply voltage for the Pi will be switched of by the relay. To switch it back on just hold the Button for about a second.
ParisM2 made it!
We have a be nice policy.
Please be positive and constructive.