I have written a script to open/close my roller shutter using two buttons and a relay. One button to open, one button to close.

Here is my script :

import RPi.GPIO as GPIO
import time

relay_pin = 18
down_button_pin = 17
up_button_pin = 27

GPIO.setup(relay_pin, GPIO.OUT)
GPIO.setup(down_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(up_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def main():
    while True:
        # Get the state of each button (pressed or not)
        down_input_state = GPIO.input(down_button_pin)
        up_input_state = GPIO.input(up_button_pin)

        # Button is pressed
        if down_input_state == False:
            print('Button Down Pressed')
            GPIO.output(relay_pin, GPIO.LOW)

        if up_input_state == False:
            print('Button Up Pressed')
            GPIO.output(relay_pin, GPIO.HIGH)

except KeyboardInterrupt:

When I run the script, everything is working as expected :

$ python button_relay.py

But when I try to run it in the background :

$ python button_relay.py &
[1] 29690

the script does not work anymore.

What am I missing here ?

Raspberry Pi 3 Model B

General advises about my script are also welcome.

  • How do you know it isn't working? – joan Mar 7 '17 at 9:21
  • @joan When I press buttons, nothing happen. While running in foreground, pressing buttons open/close shutters. – hg8 Mar 7 '17 at 9:31
  • There is a mismatch in the script. It will not work, you have down_pin as well as down_button_pin. – joan Mar 7 '17 at 11:49
  • @joan Yep sorry I corrected it. But still same issue. – hg8 Mar 8 '17 at 9:49
  • The script works for me - foreground and background. Try adding a time.sleep(0.1) in the while loop so it's not busy spinning. – joan Mar 8 '17 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.