Uladzislau Bayouski
Published © MIT

Blackout: The Power Button

Complete guide to create led power on/off button for your project, includes different options of hardware and software implementation.

EasyProtip1.5 hours3,525
Blackout: The Power Button

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Adafruit Illuminated Pushbutton
×1
Resistor 330 ohm
×1
Micro SD Card 8GB+
×1

Software apps and online services

Python
Microsoft Visual Studio 2017
Raspberry Pi Raspbian

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

GPIO/Pins Scheme

Pins only grande jw3zscurha pqv71irrj6

Main Circuit Diagram

Scheme djpmpwryfb

Breadboard Circuit Diagram

Breadboard circuit 0xd0g8d5un

Alternative Breadboard Circuit Diagram

Alternative circuit kihmafb9xj

Code

Serial Approach Сode

Python
#!/usr/bin/python

import RPi.GPIO as GPIO
import subprocess

# Starting up
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)

# Wait until power button is off
# Recommended to use GPIO.BOTH for cases with switch
GPIO.wait_for_edge(3, GPIO.BOTH)

# Shutting down
subprocess.call(['shutdown', '-h', 'now'], shell=False)

GPIO Approach Code

Python
#!/usr/bin/python

import RPi.GPIO as GPIO
import subprocess

# Starting up
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(3, GPIO.IN)

# Light up the led button
GPIO.output(23, True)

# Wait until power button is off
# Recommended to use GPIO.BOTH for cases with switch
GPIO.wait_for_edge(3, GPIO.BOTH)

# Shutting down
subprocess.call(['shutdown', '-h', 'now'], shell=False)

Github

Credits

Uladzislau Bayouski

Uladzislau Bayouski

5 projects • 32 followers
Thanks to Alex Glow and Tyler.

Comments