This now works. I've added a second button to change the sequence of the LEDs. The second button needs to cancel any previous running .py scripts first. Can that be done through a kill all, or is there a better way? How do you assign multiple tasks to a single os.system?

 #!/usr/bin/env python
 from time import sleep
 import os 
 import RPi.GPIO as GPIO

 GPIO.setup(6, GPIO.IN)
 GPIO.setup(5, GPIO.IN)

 while True:
        if ( GPIO.input(6) == False ):
               print("Button 1")
               os.system("python /home/pi/runmyleds.py")

        if ( GPIO.input(5) == False ):
               print("Button 2")
               os.system("python /home/pi/runmyleds2.py")

The indentation is fine here, it got corrupted in copy/paste.

I'd like a .py to run on a button press, to twinkle some Christmas LEDs Using the code below, it works for a simple "hello world" message, but not to fire of my seasonal lights, it doesn't wait for a button press, it just runs. Nice, but not what I want..... Any ideas as to why, please?

import RPi.GPIO as GPIO
import time
import os


button = 6

GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    if (GPIO.input(button)):
        # This line works OK when button is pressed.
        os.system("python /home/pi/helloworld.py")
        # This line [when not commented out!] just runs automatically, a button press is not required.
        # os.system("python /home/pi/runmyleds.py")
  • Indentation is a key aspect of program flow in python. If you entered it into the interpreter the way listed here it is no wonder it does not work (actually the interpreter should complain at the while True). Please check the proposed edit.
    – Ghanima
    Dec 8, 2016 at 9:34
  • This has become a different question.
    – joan
    Dec 8, 2016 at 18:38

1 Answer 1


There is no program logic difference between the os system calls. They will both run if uncommented.

There may be a difference in the side-effects. In particular your runmyleds.py script may be changing GPIO 6.

You have a pull-up set on GPIO 6 which means that the default button state is True. That suggests the other end of the button is connected to ground. In that case your logic is wrong and to detect a button press you should use if not GPIO.input(button).

If the other end of the button is actually connected to 3V3 you should be using a pull-down instead and the current logic will then be correct.

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.