Tired of ‘sudo shutdown’? Now you can add a Power/Restart button to your Raspberry Pi. At some moment while using our Pi’s, it occurred to all of our minds why didn’t they provided a Raspberry Pi. Well, the reason behind no power switch on Raspberry Pi is fairly simple, it saves manufacturing costs and which ends up as a 35$ computer.
With no power button, we are left with two options first one is to shut it down from graphical or command-line interface or second is to pull off the power plug. Switching off the power directly can be dangerous and can corrupt the memory card. It is always instructed to run a shutdown command before we turn off the power. So without a keyboard or mouse, and following the instruction yes we can do both at the same time with minimal headache.
We all have used switches for controlling LEDs or taking inputs for performing some tasks. In this tutorial, we are going to use switches to read the user input and perform our required task of Shutdown/Restart our Raspberry Pi.
In this article, we are going to use systemctl commands for running a service which will read our input and perform the power functions. Python language will be used to read and execute commands.Things you will need
- Raspberry Pi
- Momentary Switch
- Connecting wires
You can choose your own pins to read the output, for this article we have used Raspberry Pi header pin 6, 38, 40. Pin 6 is ground pin while pin 38 is used for restarting the Raspberry Pi and pin 40 for Shutdown.
The pins are using software pull up logic, so the ground is used to complete the circuit to pins. When the momentary switch is pressed, circuit completes and the input is read by the program.
The program uses RPi.GPIO library to read the user input. An event detection function from library is being used which detects the falling edge of the pulse.
Another os module is used to execute the shell commands. On the detection of the button press on different pins the relevant command is executed with the help of the os module.
Save the following line of code to any location with ‘py’ extension as ‘shutdown.py’
#!/usr/bin/python3 import RPi.GPIO as GPIO from time import sleep import os shutdown_pin = 40 restart_pin = 38 # Using BOARD pin numbering GPIO.setmode(GPIO.BOARD) # Set Pins to Input GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(restart_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) def Shutdown(channel): os.system("sudo shutdown now") def Restart(channel): os.system("sudo shutdown -r now") # Detect Button press GPIO.add_event_detect(shutdown_pin, GPIO.FALLING, callback = Shutdown, bouncetime = 3000) GPIO.add_event_detect(restart_pin, GPIO.FALLING, callback = Restart, bouncetime = 3000) while True: sleep(1)
Provide the execution permissions to file
Sudo chmod +x /file/path/shutdown.py
After our python code is ready to perform some action, we need to run it as a service so that we can keep working, and the program detects the button press. To create a service follow the below steps.
Go to /etc/systemd/system
Create a service file-
sudo nano shutdown.service
Add following line to shutdown.service
[Unit] Description=Shutdown Button Thread [Service] Type=simple ExecStart=/file/path/shutdown.py [Install] WantedBy=multi-user.target
Save the file and exit
Now we’ll use systemctl commands to enable and run the service
Enable the service so that it starts after every bootup with -
sudo systemctl enable shutdown
Start the service with -
sudo systemctl start shutdown
You are all set to use the buttons. Raspberry Pi is listening to a chirp caused by the buttons.