I wrote this code to check for a button connected to GPIO pin 13 being pushed. My circuit is going from 3.3v power to a 220ohm resistor to a push button and to GPIO pin 13. My LED circuit works perfectly fine on its own. Whenever i run this script, the LED seems to turn off and on randomly, and the associated text is printed without me even pushing the button. It seems to be completely random.
When I do press the button, either no input is detected or multiple inputs are detected.
This is my source code:
#Turn LED on and off with push button
#By H
#Start date: February 12th, 2021
#End dat
#Importing GPIO and time libraries for delays and use of RPi3B GPIO pins
import RPi.GPIO as GPIO
import time
#Setting GPIO mode to BOARD in order to use on-board GPIO numbering
GPIO.setmode(GPIO.BOARD)
#Setting 13 as an input for the push button
#Setting 11 as an output for the LED
GPIO.setup(13,GPIO.IN)
GPIO.setup(11,GPIO.OUT)
#Infinite while statement so the script doesnt end, containing checks for the button being pushed, and turning the LED on/off
while True:
print ("Off")
GPIO.output(11, GPIO.LOW)
while GPIO.input(13) == 1:
time.sleep(0.1)
print ("On")
GPIO.output(11, GPIO.HIGH)
while GPIO.input(13) == 0:
time.sleep(0.1)
I have also used a script from the Raspberry Pi forums which uses pull up & down resistors with GPIO_EVENT_DETECT and 2 separate buttons for on and off. Whenever I push either button with that code, i get 2,3 and sometimes more inputs in rapid succession, even when I only pressed the button once.
Here is the source code for that:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
btn_input = 11;# button to monitor for button presses.
btn_input2 = 13;# 2nd button to monitor for button presses.
LED_output = 15; # LED to light or not depending on button presses.
# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(btn_input2, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)
# handle the button event
def buttonEventHandler_rising (pin):
# turn LED on
GPIO.output(LED_output,True)
print("on")
def buttonEventHandler_falling (pin):
# turn LED off
GPIO.output(LED_output,False)
print("off")
# set up the event handlers so that when there is a button press event, we
# the specified call back is invoked.
GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler_rising)
GPIO.add_event_detect(btn_input2, GPIO.FALLING, callback=buttonEventHandler_falling)
# we have now set our even handlers and we now need to wait until
# the event we have registered for actually happens.
# This is an infinite loop that waits for an exception to happen and
# and when the exception happens, the except of the try is triggered
# and then execution continues after the except statement.
try:
while True : pass
except:
print("Stopped")
GPIO.cleanup()
How might I fix these issues so that I could use 1 single button to turn the LED on and off, where pressing the button switches it on, and it stays on after release, and pressing it again will turn it off, staying off after release?
Try to set the pull_up_down property GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
and remove the resistor.
the use of the resistor might not force the tension to go up enough as the current will make it drop, which perhaps is the reason why you have this erratic behaviour. It should if the internal resistor is around the 50k.
Try to have a look into the recipes from gpiozero Button - led.
gpiozero also have a debouncing parameter.
the resistor is needed with the led because it handles only 0.7v of voltage. to reach 3.3v or 5v you need the resistor to drop the additional voltage. the lower the resistance the brighter the led will be. if it's to high there will not be enough current for the led to light on.