2

Here is my Python code:

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(22, gpio.IN)

while True:
    input_value = gpio.input(22)
    if input_value == False:
        print('Pressed')
        while input_value == False:
            input_value = gpio.input(22)
    else:
        print('Not pressed')

    time.sleep(0.1)

gpio.cleanup()

But my output is consistantly

Pressed
Not pressed
Not pressed
Not pressed
Not pressed
Not pressed
Pressed
Not pressed
Not pressed
Not pressed
Not pressed
Not pressed
Pressed
Not pressed

etc, etc.. If I have a button plugged in (similar to this), the output is still the same. wiringPi readall shows similar results. Why is this happening?

2
  • do you have the button wired using a pulldown resistor as shown in the cited picture? Also note the nested while loop is nearly redundant, except it doesn't call the sleep timer. Commented Jan 8, 2014 at 2:21
  • This is my circuit @tedder42
    – user11894
    Commented Jan 8, 2014 at 2:25

2 Answers 2

1

Your wiring looks ok, and your code looks like it should work. But if I understand correctly, the input value you're getting is randomly wobbling high and low, whether you press the button or not. That sounds to me like your input is floating and not connected where you think it is.

Check carefully the ribbon connector is connected properly to your pi's GPIO pins, and connected the right way round! Check that your white wire is really connected to pin 22 of your cobbler. Plug the other end of this white wire directly to your ground and check that your input value is always low. Then just have the white wire to your resistor to +V and check that it is properly connected, then your input value should always be high.

Finally check that the button is connecting the way you think it is, it might be necessary to rotate it by 90 degrees.

0

You're having this output because you check your inputs every 0.1s ( time.sleep(0.1) from your code).

First of all : Is your button a mono-stable or bi-stable (if it is like a clicking pen it's bistable)

If you want to detect a positive edge, your tests need to be something like below (almost C):

while(button == low);

while(button == high);

// you now have detected a positive edge

The "while(condition);" will block the execution of your code until "condition" is True.

I do not know python well enough to help you with the syntax of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.