I'm planning to make a car robot with RPi. I have a python script named 'abc.py', and I'd like to kill the python program and cleanup GPIOs used in abc.py when a button connected to GPIO is pressed. What should I do?

  • 2
    I would alter the script to detect a GPIO level change and then exit the script. – joan Dec 23 '16 at 9:00
  • @joan's suggestion is definitely the simplest and cleanest way to go about doing this. I would suggest looking into a concept called "interrupts". – Jacobm001 Dec 27 '16 at 19:53
  • That being said, as is, your question is too vague to be considered on topic here. Please see the help center for more information about asking good questions. – Jacobm001 Dec 27 '16 at 19:54

If the button is connected to a GPIO pin and a GND pin,

button_gpio=<Button GPIO pin here>
GPIO.setup(button_gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if (GPIO.input(button_gpio) == 0):
        print(“Button pressed”)

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.