0

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!

3
  • Joan may have a better solution but it is difficult to understand your code. Why freq = 0.5Hz (ChangeFrequency says frequency - frequency in Hz (freq > 1.0)? Why stop and start and try to create new PWM object? Under the wraps PWM creates new threads, but given the nature of python timing can't be guaranteed.
    – Milliways
    Jan 19 '21 at 10:49
  • Frequency is 0.5 meaning 1 pulse every 2 seconds - every 1% will thus be 20ms (Europe 50Hz net frequency) meaning 1 sinus - that's fast en granulate enough to modulate a saunaheater (9kW) - start & stop with new PWM object (in between # ---) was just a test - removing this yields the same results
    – Wannes
    Jan 20 '21 at 12:22
  • My comment was that the documentation states freq > 1.0. The PWM works if you don't try to stop; just set duty cycle to 0.
    – Milliways
    Jan 20 '21 at 22:42
0

It's a known bug of the RPi.GPIO Python module.

Look through RPi.GPIO tickets.

You could use the pigpio Python module or gpiozero (if you use pigpio as the backend). pigpio is likely to be pre-installed as is gpiozero.

For a non-daemon based alternative to pigpio you could use the lgpio Python module.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.