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 minutes12,586
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

36 projects • 98 followers
Graduated in Psychology Artificial Intelligence department. Expert in electronics, automation and IoT. Now working on VR-AR experiences.

Comments