Giovanni Gentile
Published © GPL3+

Raspberry Pi Zero Switch Off by Button

I've a small and sweet Raspberry Pi Zero. Now I can switch off my Raspberry Pi Zero without a monitor or keyboard or LAN.

BeginnerFull instructions provided30 minutes10,459
Raspberry Pi Zero Switch Off by Button

Things used in this project

Story

Read more

Code

Switch OFF your Raspberry PI ZERO

Python
import RPi.GPIO as GPIO
import time import os
buttonPin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
last_state = True
input_state = True
while True:
  input_state = GPIO.input(buttonPin)
  if (not input_state):   print("Shutdown")
    os.system('sudo shutdown -h now')
    time.sleep(0.05)
GPIO.cleanup()

Credits

Giovanni Gentile

Giovanni Gentile

31 projects • 65 followers
Graduated in Psychology expert in new technologies and communication, with expertise in electronics, automation and home automation.

Comments