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.

BeginnerProtip2 hours4,822
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
Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

GPIO/Pins Scheme

Main Circuit Diagram

Breadboard Circuit Diagram

Alternative Breadboard Circuit Diagram

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

6 projects • 70 followers
Thanks to Alex Glow and Tyler.

Comments