Skip to content

Instantly share code, notes, and snippets.

@SDSkyKlouD
Created March 29, 2019 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SDSkyKlouD/14866d33e2c40bae4208b671a6860b1e to your computer and use it in GitHub Desktop.
Save SDSkyKlouD/14866d33e2c40bae4208b671a6860b1e to your computer and use it in GitHub Desktop.
Raspberry Pi - GPIO - Toggle LED when button press
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
TOGGLE_PIN = 20 # Change this PIN number to your button PIN
LED_PIN = 21 # Change this PIN number to your LED PIN
GPIO.setup(TOGGLE_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
value = 0
led_last = False
while(True):
input = GPIO.input(TOGGLE_PIN)
if(input == 1 and value != input):
print("Toggle pin HIGH, ", end='')
value = input
led_last = not led_last
GPIO.output(21, int(led_last))
print("LED turned on? : " + str(led_last))
elif(input == 0 and value != input):
print("Toggle pin LOW")
value = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment