Shutdown Headless Raspberry Pi (English)


A modern operating system does not like being turned off on a dime, that is to say, being turned off before being shut down. For computers with a graphic interface this is as easy as pressing the right button. However, if a computer doesn’t have a screen or a mouse, like a Raspberry Pi (RPi) working as a server hidden away in a dark corner, this offers some difficulty. The only way to shut it down is by logging in through SSH and running the “shutdown -h now” command.
Een Nederlandse vertaling vindt u hier.

In this post I will explain a way to use software, two buttons, four resistors and one LED to shutdown or reset the RPi neatly.
For this project I will use two General Purpose Input/Output (GPIO) ports on the RPi. These ports can be programmed as either Input ór an Outpot port. The RPi has several of these ports. These are found on the P1 connector, but there are also four ports on the Rev2 board at the P5 connector. The P5 connector needs to be soldered to the RPi. Because I plan on using the P1 connector for other purposes, I have chosen to use the P5 connector. If you want to use another port, you can change the software to select which GPIO port you want to use.

On the Rev2 boards of the RPi there is space for a connector which can be used to reset the RPi or to start the RPi after a “shutdown -h now” command. To do this, you need to solder a P6 connector on the RPi. By making pin 1 (the square on the illustration) “low” (connecting it with earth) the RPi will boot again.

There are two ways to connect a switch to a GPIO defined as an input. In the example on the left the rest state for a GPIO port is set to “high” (+3V3). This is because the 10K ohm resistor draws the port to +3V3 (called a Pull-Up resistance). When the switch is pushed, the port changes to “low” (GND). The extra resistor in serie with the GPIO is not necessary, but it prevents a GPIO port which is defined as an output from burning down.
In the example on the right the rest state of the GPIO port is set to “low” (or GND, 0V). This is because of the resistor of 10K ohm pulls the port to GND (this is called the Pull-Down resistor). When the switch is pressed, the port becomes “high” (+3V3). In this example the extra resistor helps as a security barrier in case the port is defined as Output.

The schematic on the left shows how the RPi, with the use of some software, can be shutdown neatly. For me, the label “DO NOT PRESS” makes me curious to do the opposite. That is why I linked the reset button in series with the shutdown button and set the GPIO-Input in rest state to “high”. Pin 1 of P6 becomes “low” only when both buttons are pushed at the same time.

Because you don’t want the RPi to shutdown when the button is pushed by accident, this command is triggered at a delay which can be changed with the software (standard is between four and six seconds).

When testing these schematics I noticed that the lack of feedback is not helpful. By adding a LED –which is triggered via software– in to the chain it becomes possible to see the current state of the program. When software is running the LED flashes once every two seconds. When the shutdown button is pressed, the frequency changes to four times per second. When the LED start to emit constant, the shutdown command is executed.

This is the definitive schematic:
Complete Schematic
I soldered the different components onto a PCB-board in such a way that the print falls exactly over the pins of P5 and P6.

Proto01b Proto01a Ready2Test2 Ready2Test1

This is the code to control the GPIO port’s:

Paste this code in to “/usr/local/bin/” and then
sudo chmod +x /usr/local/bin/
To run the program automatically when the RPi starts you have several options. I like running the program as ‘daemon’. If you want to run the program as daemon as well, paste the following code into a file called “/etc/init.d/GPIOshutdown”:

Don’t forget to
sudo chmod +x /etc/init.d/GPIOshutdown
To start the daemon type:
sudo /etc/init.d/GPIOshutdown start
The LED starts blinking.
To stop the daemon type the commando:
sudo /etc/init.d/GPIOshutdown stop
The LED turns off.
To start the daemon automatically with every reboot of RPi, you have to install the service with the command:
cd /etc/init.d
sudo insserv GPIOshutdown

Rest assured that the script does not take up too much of the available processor capacity. When the ‘sleep’ command is ran, it uses almost no processor time.
Please Note: the P5 connector is meant to be soldered to the bottom of the RPi. Because I found this inconvenient for this particular project, I soldered it to the top (component side). Because of this, the Pin numbers are in the wrong order. The squares on the RPi-board always mark pin 1.
I only used the even pin-numbers where the +3V3 is on the outside of the RPi.

P5_Header P1_Header


This entry was posted in Computer, Scripts and tagged . Bookmark the permalink.

4 Responses to Shutdown Headless Raspberry Pi (English)

  1. Gerard Lunow says:

    How did I end up here!! Recognize your writing, still. Hope you are doing very well!!

  2. Pingback: Shutdown Headless Raspberry Pi | Willem's Website

  3. J. Hartog says:

    Nadat ik iets toevoegde in het config file krijg ik de placeholder page niet meer tevoor-schijn. Ik tik in :~$ sudo /etc/init.d/lighttpd restart
    Duplicate config variable in conditional 0 global: fastcgi.server
    2015-08-31 23:56:15: (configfile.c.957) source: /etc/lighttpd/lighttpd.conf line: 47 pos: 23 parser failed somehow near here: (EOL)
    Als ik sudo netstat ntulp intik krijg ik het volgende:
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0* LISTEN 17001/smbd
    tcp 0 0* LISTEN 1295/mysqld
    udp6 0 0 :::41569 :::* 598/avahi-daemon: r
    udp6 0 0 :::5353 :::* 598/avahi-daemon: r
    udp6 0 0 :::27396 :::* 1417/dhclient
    Wat ziet U dan?
    Ik heb eerst via kennis gemaakt met mysql en dat geinstalleerd. Later heb ik xbmc geinstalleerd en toen overgestapt op kodi, daardoor liep het allemaal niet zo soepel meer. Nu probeer ik lighttpd te installeren samen met php en phpmyadmin om een webside op te starten.
    Nou zegt U ergens van voor af aan opnieuw beginnen, moet ik dan niet eerst alles de-installeren en hoe doe ik dat?
    scriptje s.v.p.

    • Beste J. Hartog,
      Ik heb zo snel geen idee wat je tracht te bereiken en hoe ik je daarbij zou kunnen helpen.
      De RPi (de “oude” versies zonder de quad core) zijn in mijn ervaring niet geschikt voor Kodi of xbmc. Wil je toch Kodi draaien, dan kun je het beste een image van hun website downloaden en deze op een SD kaartje zetten.
      “Opnieuw beginnen” betekend meestal dat je een recente image van een RPi OS download en op een SD kaartje zet om vervolgens hierop aanpassingen te doen. Baby stapjes!

Comments are closed.