0

I am currently trying to create a program that does something each time a button is pushed, it seems simple but for me this has been very difficult to acomplish. I am on python 2.7 (but I can go to 3 if you need me too) and I am using a pull-up-resistor setup.

pull-up-resistor setup

The yellow wires are the pins (22, 23, and 24). The orange is positive 3.3V going to the 10K resistors. The green is ground.

I tried this code below but it simply returns, "Pin 24 is true" continuously and the buttons do nothing.

    import os, RPi.GPIO as GPIO
    from time import sleep

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(23,GPIO.IN)
    GPIO.setup(24,GPIO.IN)
    GPIO.setup(25,GPIO.IN)

    while True:
       if(GPIO.input(23) == False):
          print ("Pin 23 is true")
          # do stuff based on pin 25 here
       elif(GPIO.input(24) == True):
          print ("Pin 24 is true")
          # do stuff based on pin 18 here
       elif(GPIO.input(25) == False):
          print ("Pin 25 is true")
          # and again for pin 22

Any help is much appreciated!

Thanks, Cody

Second Image (different view)

  • I can't tell from the photo which gpios are connected to the yellow wires. gpio 23 is on P1-16, 24 is P1-18, 25 is P1-22 (elinux.org/RPi_Low-level_peripherals). Have you made the correct connections? – joan Jun 17 '14 at 19:40
  • @joan I just added another picture of how I have the pin plugged in. I believe this is right. – CodyJHeiser Jun 17 '14 at 19:48
  • GPIO.input(24) == True should be GPIO.input(24) == False just like you already have for the two other inputs. – Gerben Jun 17 '14 at 19:59
  • Also check the orientation of the switches. I've never found it obvious as to which contacts are connected when the button is pressed. – joan Jun 17 '14 at 20:22
  • I just tried that and I am still having no success. Here is a video of what is happening, this my help. drive.google.com/file/d/0BxXhZOZEPb5FX0VjRU1MU3RGVjQ/… – CodyJHeiser Jun 17 '14 at 20:27
1

GPIO.input() returns either GPIO.HIGH or GPIO.LOW, so it will never be equal to False.

Your check should be something like this:

if(GPIO.input(23) == GPIO.LOW):
    print ("Pin 23 button is pressed")
elif(GPIO.input(24) == GPIO.LOW):
    print ("Pin 24 button is pressed")
elif(GPIO.input(25) == GPIO.LOW):
    print ("Pin 25 button is pressed")

Be aware that by using elif if the first button is pressed the second and third buttons will never be detected as pressed until the first is released.

  • In what way? It's quite normal to compare gpio status (usually 0 or non-zero) against True/False. docs.python.org/reference/expressions.html#boolean-operations – joan Jun 18 '14 at 8:55
  • It is possible for a value to evaluate to true but not be equal to the constant True for instance 33 is true but 33 == True is false. GPIO.HIGH and GPIO.LOW evaluate to true and false but are not equal to True and False – Craig Jun 18 '14 at 15:37
1

Which of those photos is actually your setup?

In the second photo, the colors on the resistors seems to be Brown, Black, Black which would mean 10 Ohms rather than Brown, Black, Orange which would be 10 KOhms. I think you really want 10 K resistors in there.

Your GPIO might not be able to pull that to a low. Could you verify?

  • Welcome to Raspberry Pi Stack Exchange! This is really a comment, not an answer. With a bit more rep, you will be able to post comments. – RPiAwesomeness Jun 17 '14 at 23:40
  • The second one is mine. Thanks for pointing that out! I will get that fixed right away. – CodyJHeiser Jun 18 '14 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.