I made a python script to control a LED strip connected to my Raspberry, the script receives an argument trhough the command line,and uses that argument to control the intensity of the light using PWM.

The problem is that as soon as the script ends, the light goes off, and I want it to be on for an indefinited amount of time (more precisely, until this script is executed again with a different parameter, or the lights are switched off with GPIO.output(25,GPIO.LOW))

import sys
import RPi.GPIO as GPIO

i = sys.argv[1]

GPIO.output(25,GPIO.LOW) #this line is here to set whatever state the light was on to off, just in case

1 Answer 1


The RPi.GPIO module uses software timed PWM which stops when the script is exited.

Your only solution with RPi.GPIO is to leave the script running.

If you can use GPIO 12, or 13, or 18, or 19 you could use hardware PWM. You would need to start hardware PWM with wiringPi or (my) pigpio.

If you leave the pigpio daemon running you could control PWM from the command line.

  • So if I were to leave the script running by using time.sleep, and then I run the script again, does it create 2 instances of the same script or does it terminate the previous one and starts again?
    – tacofisher
    May 21, 2017 at 20:43
  • @tacofisher I'm not sure.RPi.GPIO may give a warning and exit, it may set up competing PWM. Try and see.
    – joan
    May 21, 2017 at 21:05
  • Ok. I added time.sleep(3600) to the end of the script and ran 2 instances of it, now the lights flicker because the scripts have different parameters (80 and 20) and this causes the lights to switch between low and high intensity repeatedly. So for my purposes I need only one instance of the script running at a time, is there any way to ensure this?
    – tacofisher
    May 21, 2017 at 21:22
  • @tacofisher Use a command such as killall to kill the running script.
    – joan
    May 21, 2017 at 21:26

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.