SB Components
Published

Include Power Button to Your Raspberry Pi

Tired of ‘sudo shutdown?' Now you can add a power/restart button to your Raspberry Pi.

BeginnerFull instructions provided336
Include Power Button to Your Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi
×1
Momentary Switch
×1
Breadboard
×1
Connecting wires
×1

Story

Read more

Code

Code snippet #1

Plain text
#!/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)

Code snippet #5

Plain text
[Unit]
Description=Shutdown Button Thread
[Service]
Type=simple
ExecStart=/file/path/shutdown.py
[Install]
WantedBy=multi-user.target

Credits

SB Components

SB Components

36 projects • 9 followers
The Maker Community Store – We help you to make things intelligent using Raspberry Pi, Micro:Bit, Arduino, robotics, etc.

Comments