Table of Contents
Raspberry Pi Reset/shutdown button
The question arises again and again as to how the Raspberry Pi can be reset via a button or simply shut down. To accomplish this you have two options.
- Hard reset
- Soft reset
Hard reset button
All current Raspberry Pi models support a hard reset. With the Raspberry Pi 1, model B, the two contacts of the P6 header must be short-circuited for a moment, i.e. by a button.
With the B+ model and the Raspberry Pi 2, the run headers offer the same function. The easiest way is a reset switch – without any Python! This means you don’t need any python code to reset the Raspberry Pi, you can use jumper wires or simply a push button to reset the Raspberry Pi. My recommendation is use a push button, pressing the button now leads to a hard reset, i.e., the Raspberry Pi will restart seamlessly- as if you had just pulled the power plug.
Caution, risk of data loss!
In general, a hard reset is always an emergency solution. A running system is system simply switched off or restarted without warning. There is none Ability to close open files, complete saves, or the Shut down the file system correctly. Usually the Raspberry Pi responds to Such resets are surprisingly robust, but it cannot be ruled out that the file system will suffer lasting damage at some point or that just at the time an important file is written after the reset and then after the restart is only available in an incomplete or faulty state.
If the Raspberry Pi is already in a halt when you press the reset button State (e.g. after the previous sudo halt), then pressing it causes the reset button to restart the Raspberry Pi.
Amazon Purchase Links:
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Soft reset button
One speaks of a soft reset when the push of a button causes a proper shutdown of the Raspberry Pi triggers. The easiest way to implement such a function by a button that you connect to a GPIO pin configured as an input. When the computer starts up, you start a script that constantly uses this GPIO pin checked. As soon as the button is pressed, run out reboot or halt in your script. If you want, you can also rewrite your soft reset code so that a restart only occurs if the button is pressed for several seconds.
The following lines show a very simple Python script that executes reboot, as soon as the button on GPIO pin 21 is pressed. The wiring is quite simple, all you need is connect a push button between the GPIO Pin 21 and GND, this way when the button is pressed the GPIO Pin 21 is pulled to GND.
#! / usr / bin / python3 # File / home / pi / reboot .py import os, sys, time, RPi. GPIO as gpio gpio. setmode (gpio. BOARD) # Pin numbers of the P1 / J8 header gpio. setup (21, gpio .IN) # Pin 21 = reset button while 1: if gpio. input (21) == gpio. LOW: system ("reboot") sys. exit () time. sleep (0.3)
Should the Raspberry Pi restart with the soft reset button (command reboot) or just be stopped (stop)? It depends what you are aiming for: After a stop, the Raspberry Pi can safely be disconnected from the power supply, for example to carry out maintenance work on the presentation system, plugging in a new USB stick etc. but has the disadvantage that the Raspberry Pi can now only be restarted by interrupting the power supply or by an additional hard reset button.
Don’t forget to make the script executable with chmod a + x reboot.py. Now add a line before exit in /etc/rc.local to have the script at the end of the boot process to start. Note the & character at the end of the command: that the reboot script is executed as a background process.
# File / etc / rc. local ... / home / pi / reboot .py & ... exit 0
Raspberry Pi Hard reset Vs soft reset
Now which is better, a hard or a soft reset? Should the Raspberry Pi come from crash completely for one reason (e.g. hardware problem, kernel error), then this helps just a hard reset. Since there are no more programs running on the Raspberry Pi, there is also no possible reaction to the soft reset button.
On the other hand, this case is fortunately the exception, and should it really occur, you can just as easily disconnect the power supply. The advantage of the soft reset is that the computer is shut down properly. So if you have a Build a presentation system for an end customer and have a reset button would like to make a soft reset.