Rappel : le Raspberry Pi fonctionne grâce à son système d'exploitation (Raspbian le plus souvent) stocké sur une carte micro-SD (ou SD pour les versions antérieures à la 3). Or on peut facilement corrompre cette carte en cas d'arrêt brutal.

La seule façon d'éteindre correctement sa machine est de le faire en envoyant un ordre d'extinction soit via l'interface graphique ou par ssh à distance (via la commande "sudo poweroff" qui fait clignoter la diode verte une dizaine de fois avant l'arrêt). Mais si on ne peut pas se connecter à distance et si l'on n'a pas d'écran relié à notre framboise, comment faire ? Il peut donc être intéressant de rajouter un bouton permettant d'éteindre proprement son Raspberry Pi physiquement.

Sources :

I- Matériel

On va donc envoyer un signal en court-circuitant 2 broches GPIO grâce à un bouton poussoir. Nous allons utiliser les broches 21 et de masse (ground ou GND) en face qui seront reliées à ce bouton qui les fermera.
raspberry 40 gpio
On fixera ce bouton sur le couvercle du boîtier (même procédure que pour le bouton ON/Reset expliquée ici : https://lofurol.fr/joomla/electronique/116-bouton-d-allumage-et-de-reset-sur-raspberry-pi-3 ).

raspberry bouton arret

 II- Programme

On créera un fichier python ".shutdown_daemon.py" (le point devant permet de cacher ce fichier sensible)  avec le programme ci-dessous (source : http://raspberrypi.stackexchange.com/a/42945) :

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets activated at the end of the boot process by
#               cron. (@ reboot sudo python /home/pi/shutdown_daemon.py)
#               It monitors a button press. If the user presses the button, we
#               Halt the Pi, by executing the poweroff command.
#
#               The power to the Pi will then be cut when the Pi has reached the
#               poweroff state (Halt).
#               To activate a gpio pin with the poweroff state, the
#               /boot/config.txt file needs to have :
#               dtoverlay=gpio-poweroff,gpiopin=27
#
# Author:      Paul Versteeg
#
# Created:     15-06-2015, revised on 18-12-2015
# Copyright:   (c) Paul 2015
# https://www.raspberrypi.org/forums/viewtopic.php?p=864409#p864409
#-------------------------------------------------------------------------------

import RPi.GPIO as GPIO
import subprocess
import time

GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)

# I use the following two GPIO pins because they are next to each other,
# and I can use a two pin header to connect the switch logic to the Pi.
# INT = 17    # GPIO-17 button interrupt to shutdown procedure
# KILL = 27   # GPIO-27 /KILL : this pin is programmed in /boot/config.txt and cannot be used by any other program
INT = 21    # GPIO button interrupt to shutdown procedure

# use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def main():

    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)
#       print "button pressed"
        time.sleep(1)   # Wait 1 second to check for spurious input
        if( GPIO.input(INT) == 0 ) :
            subprocess.call(['poweroff'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if __name__ == '__main__':
    main()

 Ce programme envoie le signal d'extinction à condition de maintenir le bouton appuyé au moins 1 seconde pour éviter les extinctions accidentelles.

 Il faut par contre que ce programme se lance dès le démarrage de l'ordinateur, pour cela on va utiliser une tâche cron avec la commande :

crontab -e

Si ce fichier n'existe pas, un nouveau sera créé et on rajoutera à la fin cette ligne :

@reboot sudo python /home/pi/.shutdown_daemon.py

 C'est fini. Après un redémarrage, pour éteindre, il suffira d'appuyer sur ce bouton pendant au moins 1 seconde.