I'm a beginner at programming so bare with me. I'm having some problems with implementing a way to stop my script manually. CTRL+C works perfectly fine in the terminal, but it seems it doesn't always work. I found that out the hard way. See, I set up a script for controlling a LED with a switch, then I set up that script to run on boot which worked fine. The problem is the script is in an infinite loop. It constantly checks if the button has been pressed. Nothing I tried could stop the script and I couldn't even log in to my Pi anymore. I had to format the SD card and reinstall Raspbian. I want to prevent that, so how would I do that?

Here's the code I'm using:

#import of libraries
import RPi.GPIO as GPIO

#pin 8 input, pin 10 output
GPIO.setup(8, GPIO.IN)
GPIO.setup(10, GPIO.OUT)

while True:
    value = GPIO.input(8)

    #if the value on pin 10 equals False light up the LED
    if value == False:
        GPIO.output(10, GPIO.HIGH)
        print("Button pressed")

        #constantly check for changes on pin 10
        while value == False:
            value = GPIO.input(8)

    #else turn it off
        GPIO.output(10, GPIO.LOW)

I tried implementing this:

while True:
    input = raw_input("")
    if input == "stop": 

But now both inputs (button and keyboard) are getting mixed up, and the main script doesn't work correctly anymore.

How would I implement a way for the script to check if there has been any keyboard input, while the script is actually waiting for a switch press? Is that even possible? What are other alternatives?

  • 2
    At least, try adding some delay in your loop. Then it gives the system a chance to run other tasks. For checking keypresses, search for a function that does not wait for input, only reports if any key is pressed, then call that function in your loop.
    – Frepa
    Feb 28, 2013 at 10:28
  • 1
    have you been able to solve this problem or still need help?
    – SteveIrwin
    Apr 5, 2013 at 19:14
  • all I want to do, is inside a while loop "press any key to exit" It can't be THAT hard???
    – user10589
    Nov 14, 2013 at 17:52
  • Has this been resolved? If so, could you please mark an answer as such, or create a self-answer and mark that as Answer? That would be great. We are trying to get the site Q:A ratio up and having questions officially resolved helps that and moves us closer to graduation. Thanks! Mar 20, 2014 at 0:53

2 Answers 2


You could import time at the start of your script and, and use something like


to sleep for 1 second every time the loop is executed. This doesn't have to be a whole second - you could use 0.1 for example.

Alternatively, instead of polling the input, you could use interrupt driven IO. Create a function to do something when the button has been pressed, and use GPIO.add_event_detect to register that function as a callback:

# callback function to be executed when GPIO interrupt is triggered
def callbackfunction():
    # handle the input...

GPIO.add_event_detect(8, GPIO.RISING, callback=callbackfunction)

There's good information on interrupt driven GPIO here: https://code.google.com/p/raspberry-gpio-python/wiki/Inputs

There's a tutorial on interrupt driven IO here: http://raspberrywebserver.com/gpio/using-interrupt-driven-gpio.html


Not sure how you set it up to run at boot, but if you replaced the login process with it, you really have no shell to return to, so ^C is not going to do anything.

You could set it to run once you login (.profile and/or .bashrc come to mind here) and you have a shell to exit to, but I don't have enough info to answer this question.

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.