I'm building a sauna controlling mechanism, controlled by the Pi, using PWM and solid-state relays. Works fine, but after a while, the PWM breaks down. At this moment, when I run the program and the PWM starts, it works at first; changing the duty cycle also works fine. When I stop and re-start the PWM however, it results in an oscillating pin, no expected output.
This is the testcode:
import RPi.GPIO as GPIO
import time
print("PWM start - GPIO setup")
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)
time.sleep(2)
print("PWM start - set PWM")
sauna_pwm0 = GPIO.PWM(16, 0.5)
time.sleep(2)
print("DC 20")
sauna_pwm0.start(20)
time.sleep(5)
print("PWM stop (DC 0)")
sauna_pwm0.ChangeDutyCycle(0)
time.sleep(3)
print("DC 20")
sauna_pwm0.ChangeDutyCycle(20)
time.sleep(5)
print("PWM stop")
sauna_pwm0.stop()
time.sleep(3)
# ---
print("GPIO cleanup")
GPIO.cleanup()
time.sleep(2)
print("PWM restart - GPIO setup")
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)
time.sleep(2)
print("PWM restart - set PWM")
sauna_pwm0 = GPIO.PWM(16, 0.5)
time.sleep(2)
# ---
print("DC 20")
sauna_pwm0.start(20) # DC is NOT 20, but PWM is oscillating at high frequency
time.sleep(5)
print("PWM stop")
sauna_pwm0.stop()
time.sleep(3)
print("GPIO cleanup")
GPIO.cleanup()
At the second "start(20)" the pin does not what is expected. When I stop and re-start the program, it works again (up until the 2nd start).
There is a lot out there on PWM not functioning as it should - it is suggested to use ChangeDutyCycle(0) instead of stop().
Even when I do not cleanup() in between the starts, and "just restart" after a stop(), it oscillates. (in this case, the code between the # --- is commented out --> same behaviour)
Is the conclusion that PWM is just too buggy to use? (also see https://www.raspberrypi.org/forums/viewtopic.php?t=277137) Should I use another PWM mechanism? (https://github.com/sarfata/pi-blaster, for example? didn't try it yet, though)
Or is there something wrong with the way I use the GPIO module?
thanks a lot!