0

I am using an NES as a Pi case. I want to wire the power button to the Pi to do some commands. I have connected one cable to GPIO 1 and another with a 10K Ohm resistor to GPIO 17. I found this command to perform a button press:

import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)

while True:
    input_value = gpio.input(17)
    if input_value == False:
        print('The button has been pressed...')
        while input_value == False:
            input_value = gpio.input(17)

However, whenever I run it, it constantly reads "The button has been pressed..." and pressing in the button makes it stop. How should I rewrite this script to make it display a message when the button is NOT pressed (but, is really pressed on my console)?

BTW-I am a complete novice to the Pi/Python. :)

1
  • what are you connecting to GPIO 1? Do you mean pin1, the +3.3V line? All you need is an internal pullup resistor and a connection via the switch to the pi's Gnd (pin 6).
    – francis
    Commented Jan 22, 2014 at 8:55

2 Answers 2

3

your problem is you leave pin 17 floating when button is not pressed.

your schematics should look like this:

pin 17 o---+----[10k]------> +3.3V
           |
            \  your button
           |
           o GND

then when your button is not pressed, the pin 17 will read '1' (true) and when pressed the reading will become '0' (false).

no changes are necessary in your script.

3
  • Can you explain what exactly this will look like? I don't understand how schematics work...Like I said, I'm a novice... :( I simply was following a guide that I found to wire this up, but it doesn't seem to work. Learning how to read schematics would be best for me so I can actually understand how this all works. Commented Jan 22, 2014 at 19:40
  • well. you connect one side of your 10k resistor to the 3.3V and other side to the pin 17. then you connect your button (it has 2 wires) to the pin 17 and GND.
    – lenik
    Commented Jan 22, 2014 at 23:50
  • Ah. That made sense!!! And it worked! Thank you! +1 Commented Jan 24, 2014 at 5:53
0

There are a number of potential problems.

It is unclear from your description how you have wired the switch and whether it is connected to the correct pins.

Even if wired correctly contact bounce will cause it to loop. Include a delay time.sleep(.3)

See raspberrypi.stackexchange.com/a/12908/8697 for possible solutions

Your Answer

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

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