1. Hardware Preparation
-
Raspberry Pi -
Breadboard -
Jumper wires -
Button
2. Configuration Steps
1. Connect the hardware according to the wiring diagram

2. Enter the example folder
cd example/

Create and edit a Python file named shutdown_button.py
nano shutdown_button.py
Enter the code in the editing interface:
from gpiozero import Button
from subprocess import check_call
from signal import pause
def shutdown():
check_call(['sudo', 'poweroff'])
shutdown_btn = Button(4, hold_time=2)
shutdown_btn.when_held = shutdown
pause()

3. After editing, press ctrl+x
to exit, press y
to confirm. Run the test:
sudo python3 shutdown_button.py
Long press the button for 2 seconds, and the Raspberry Pi will shut down.
– END –
Hardware Arsenal
Click to learn more👆
If you have any thoughts or corrections about the article, feel free toleave a message!
For those who have extra time, clickRead the original text to continue learning in the community!
Previous Review
What is Raspberry Pi? Can it be eaten??
【Raspberry Pi Basics Series Tutorial】1. Configure and Initialize Raspberry Pi
【Raspberry Pi Basics Series Tutorial】2. Remote Connect to Raspberry Pi
【Raspberry Pi Basics Series Tutorial】3. Learn Linux Commands
【Raspberry Pi Basics Series Tutorial】4. Light Up an LED
【Raspberry Pi Basics Series Tutorial】5. Ultrasonic Sensor Distance Measurement
Click to read👆