Seemingly unstable GPIO output while executing from RetroPie
-
I have Python code that instructs a GPIO pin to supply a MOSFET transistor with a signal to turn on/off LED lights. I have problems with the lights not staying on after executing the code via RetroPie (automatically at boot, and manually vie the RetroPie menu). I do not believe it has anything to do with the electronics; the signal seems to be dropped to the output pin.
I have the LEDs fade up with pulse width modulation (PWM) and then I attempt to set the pin to "on" following the ramp-up. It seems to have a problem after PWM. I set the pin HIGH, LOW, 0, 1 -- it just seems to do whatever it wants after the PWM, that is, either on or off whether it was told to be on or off. I've tried putting a pause after the modulation, and I currently have the whole pin re-initiated after the PWM loop (which I don't know why that isn't putting out an error). It seems to work fine from command line (but I have messed with this for so long, that I may not be correct on that). My program logic is that the pin is opened/initialized, faded up with PWM'd, and then set to "on", but something goes regularly wrong.
During boot, the Python code is run from ( /etc/rc.local):
...
python /home/pi/gameltsfade.py &
exit 0Manually, they can be operated from RetroPie menu ( ~RetroPie/retropiemenu/Game lights ON.sh):
!#/bin/bashpython /home/pi/gameltsfade.py
gameltsfade.py programming
import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # set BOARD pin numbering system
GPIO.setup(11, GPIO.OUT) # Declaring pin 11 as output pinpwm=GPIO.PWM(11,100) # Created a PWM object
pwm.start(0) # Started PWM at 0% duty cyclefor x in range(100): # loop for 200 times
pwm.ChangeDutyCycle(x) # set duty cycle
sleep(0.01) # Delay for 30mS in loop
(#) pwm.stop()
(#) GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH)
(#) pwm.start(100)
(#) pwm.ChangeDutyCycle(100)
(#) print("end of loop")
(#) sleep(2)
(#) GPIO.output(11, 1)******
sleep(0.5)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # set BOARD pin numbering system
GPIO.setup(11, GPIO.OUT) # Declaring pin 11 as output pin
GPIO.output(11, GPIO.HIGH)
(#) print("set at 1")
(#) sleep(3)(you can see commented-out troubleshooting lines. As it stands, it functions, although unreliable)
(Note: Commented lines in this copy of the code are in parenthesis so the text does not go to a large size in this post.)
(I also have this manually operable through Kodi add-ons (separate code) with similar results - actually in Kodi, it never stays on (when pulse width modulation is used).
I hope this is an appropriate place for this post.
Comments on the problem are appreciated.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.