So, I am trying to use the raspberry pi model 2b with Debian to control a LED light using a button. But, when I run the python script (made with python2 btw), it brings up no errors, but instead when the button is pressed, the light does not change. An image of how I wired my pi is below.
And the script that I used to control the LED is here.
#!/usr/bin/env python import RPi.GPIO as GPIO import time LedPin = 11 BtnPin = 12 Led_status = 1 def setup(): GPIO.setmode(GPIO.BOARD) GPIO.setup(LedPin, GPIO.OUT) GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.output(LedPin, GPIO.HIGH) def swLed(ev=None): global Led_status Led_status = not Led_status GPIO.output(LedPin, Led_status) if Led_status == 1: print 'led off...' else: print '...led on' def loop(): GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed, bouncetime=200) while True: time.sleep(1) def destroy(): GPIO.output(LedPin, GPIO.HIGH) GPIO.cleanup() if __name__ == '__main__': setup() try: loop() except KeyboardInterrupt: destroy()
The script did output all of the things that it was supposed to output, but the light did not change at all. Could it be because I have too much resistence (220 R). Any comments help. Thanks