enter image description hereUsing a very simple input script to read a button press, Python GPIO isn't reading high when the button is pressed. The wiring is correct - I have checked it using wiringpi readall.
Rpi. GPIO has been updated and is current and I've run the pintest script to test all pins are OK.
Is there anything I'm missing that could cause Rpi. GPIO to miss a high input pin that wiringpi is detecting?
My code is:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.IN)
try:
while True:
if GPIO.input(13):
Print("button pressed")
Edit: I should have mentioned that I've already tried the pull up/downs, and even tried using physical resistors - still the same result (Without the resistors, I can see from wiringpi that the input is floating.)
I'm not at home to send a photo of the wiring - here's a sketch of the setup. However, I don't believe the physical setup is the issue since wiringpi is picking up the input without issue - it's just Python GPIO that's not behaving.
-
1Can we see a pic(s) of your wiring? What is the other side of the button connected to?Steve Robillard– Steve Robillard2015年10月04日 22:24:27 +00:00Commented Oct 4, 2015 at 22:24
-
GPIO 13 is connected to pin 33 on the 40 pin expansion header. Which pin is connected to your switch?joan– joan2015年10月04日 22:33:51 +00:00Commented Oct 4, 2015 at 22:33
1 Answer 1
Consider setting the GPIO pin as a pull up or pull down resistor.
For example:
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
That sets it as a pull up resistor meaning of it is exposed to ground it will trigger the button being pressed.
You can read it with:
if GPIO.input(13) == False:
Print "desired print text"
-
Thanks for your suggestion - I should have mentioned that I've already tried the pull up/downs, and even tried using physical resistors - still the same result. Without the resistors, I can see from wiringpi that the input is floating.pa1983– pa19832015年10月05日 09:24:53 +00:00Commented Oct 5, 2015 at 9:24