Favorited Favorite 1

Hardware Hookup The connection is quick. If you have not already, simply stack the Qwiic pHAT v2.0 on top of your Raspberry Pi's GPIO header. If you are using an enclosure, you may need to have additional pair of stackable headers for a secure connection. The image below shows the pHAT v2.0 connecting to a Pi 3 with the help of stackable headers. For the scope of this tutorial, we'll using a desktop setup with a monitor, keyboard, and mouse to easily configure your Raspberry Pi. If you have not already, connect the necessary peripherals and power up your Pi!

Example 1: Safe Shutdown For those familiar with the text based command line, we can shutdown the Pi using the following command. language:bash sudo shutdown -h now The following example loads a Python script at startup and safely shuts down the Raspberry Pi using that command when the button connected to GPIO17 is pressed. Example Code On your Raspberry Pi, download the Python script by pressing the button below. Download "safe_shutdown_Pi.py" Here (PY) You can also copy the code and paste it in a text editor. Just make sure to name this file as safe_shutdown_Pi.py and remember the location that the file was saved. language:python # safe_shutdown_Pi.py # # ----------------------------------------------------------------------------- # Raspberry Pi Safe Shutdown Python Script # ----------------------------------------------------------------------------- # WRITTEN BY: Ho Yun "Bobby" Chan # @ SparkFun Electronics # DATE: 3/31/2020 # # Based on code from the following blog and tutorials: # # Kevin Godden # https://www.ridgesolutions.ie/index.php/2013/02/22/raspberry-pi-restart-shutdown-your-pi-from-python-code/ # # Pete Lewis # https://learn.sparkfun.com/tutorials/raspberry-pi-stand-alone-programmer#resources-and-going-further # # Shawn Hymel # https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-with-the-raspberry-pi/experiment-1-digital-input-and-output # # ==================== DESCRIPTION ==================== # # This python script takes advantage of the Qwiic pHat v2.0's # built-in general purpose button to safely reboot/shutdown you Pi: # # 1.) If you press the button momentarily, the Pi will shutdown. # # ========== TUTORIAL ========== # For more information on running this script on startup, # check out the associated tutorial to adjust your "rc.local" file: # # https://learn.sparkfun.com/tutorials/raspberry-pi-safe-reboot-and-shutdown-button # # ========== PRODUCTS THAT USE THIS CODE ========== # # Feel like supporting our work? Buy a board from SparkFun! # # Qwiic pHAT v2.0 # https://www.sparkfun.com/products/15945 # # You can also use any button but you would need to wire it up # instead of stacking the pHAT on your Pi. # # LICENSE: This code is released under the MIT License (http://opensource.org/licenses/MIT) # # Distributed as-is; no warranty is given # # ----------------------------------------------------------------------------- import time import RPi.GPIO as GPIO # Pin definition shutdown_pin = 17 # Suppress warnings GPIO.setwarnings(False) # Use "GPIO" pin numbering GPIO.setmode(GPIO.BCM) # Use built-in internal pullup resistor so the pin is not floating # if using a momentary push button without a resistor. #GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Use Qwiic pHAT's pullup resistor so that the pin is not floating GPIO.setup(shutdown_pin, GPIO.IN) # modular function to shutdown Pi def shut_down(): print "shutting down" command = "/usr/bin/sudo /sbin/shutdown -h now" import subprocess process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) output = process.communicate()[0] print output # Check button if we want to shutdown the Pi safely while True: # For troubleshooting, uncomment this line to output button status on command line #print GPIO.input(shutdown_pin) if GPIO.input(shutdown_pin)== False: shut_down() Note: The code is written to use the Qwiic pHAT's pullup resistor. If you are using a momentary pushbutton without a resistor, you can use the built-in internal pullup resistor so the pin is not floating. Simply uncomment the following line by removing the # from the following line:



#GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) And add the # in front of the following line:



GPIO.setup(shutdown_pin, GPIO.IN) It should look like this after adjusting the code:



. . . # Use built-in internal pullup resistor so the pin is not floating # if using a momentary push button without a resistor. GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Use Qwiic pHAT's pullup resistor so that the pin is not floating #GPIO.setup(shutdown_pin, GPIO.IN) The code is written to use the Qwiic pHAT's pullup resistor. If you are using a momentary pushbutton without a resistor, you can use the built-in internal pullup resistor so the pin is not floating. Simply uncomment the following line by removing thefrom the following line:And add thein front of the following line:It should look like this after adjusting the code: Setting Up the Path Note: The following directions use the text based terminal to move the Python script since we will be modifying the rc.config file. You can also move the file by dragging the file from the Downloads folder to the /home/pi.



The following directions use the text based terminal to move the Python script since we will be modifying thefile. You can also move the file by dragging the file from thefolder to the This will be saved in your Downloads folder. Once downloaded, you'll need to move the Python script to the /home/pi. To do that, open the command line. Move to the Downloads folder with the following command. language:bash cd Downloads We'll use the mv Linux command to move the file to a certain location (in this case /home/pi) with the following command: language:bash mv shutdown_Pi.py /home/pi Just to make sure that the file was moved correctly, use the change directory command to move back up a level: language:bash cd .. Followed by the list command to see what's in the path. If you look closely at the images above, it was used to verify what was in the location. You should notice the shutdown_Pi.py file in the location. language:bash ls Modify rc.local Note: If you have not already, we recommend checking out this tutorial on setting up your Raspberry Pi to run a script on startup. We'll be using



How to Run a Raspberry Pi Program on Startup September 18, 2018 In this tutorial, we look at various methods for running a script or program automatically whenever your Raspberry Pi (or other Linux computer) boots up. Favorited Favorite 11 If you have not already, we recommend checking out this tutorial on setting up your Raspberry Pi to run a script on startup. We'll be using method 1 to modify the rc.local file for this tutorial. With the terminal still open, type the following command: language:bash sudo nano /etc/rc.local Scroll down using the ↓ button on your keyboard, and just before the exit 0 line, enter the following: language:bash python /home/pi/safe_shutdown_Pi.py & Save and exit by pressing CTRL + X on your keyboard, followed by y when prompted, and then hit Enter . To ensure that the changes take effect, type the following command. language:bash sudo reboot What You Should See After rebooting, hit the GPIO17 button on the Qwiic pHAT v2.0. This should shutdown the Pi. You'll notice the monitor disconnect if you have one attached but make sure to give it a few more seconds to finish shutting down before removing power. The green status LED on the Pi will stop blinking when it is completely shutdown. You can now safely remove power from the Pi. To power your Pi again, just insert the power connector back into the Pi.