The code is simple, just randomly start/stop a PWM in an infinite loop:


import RPi.GPIO as gpio
import time
import random

gpio.setup(21, gpio.OUT)

pwm = gpio.PWM(21, 100)

num_start = 0
num_stop = 0

while True:
    mode = random.randint(0, 1)
    if mode:
        num_start += 1
        print("start %d" % num_start)
        num_stop += 1
        print("stop %d" % num_stop)

The circuit is also simple: GPIO21 and ground is directly connected to a LED.

After several hundreds of start/stop, the PWM on GPIO21 will not starts again. But if you exit python and restart the program, it will recover.

Why this behavior occurs? Is it lies on the software side or circuit side?

1 Answer 1


I am fairly sure it is a bug in the RPi.GPIO module.

Look through https://sourceforge.net/p/raspberry-gpio-python/tickets/

As a workaround I suggest you do not use the start() and stop() methods in a loop, use the ChangeDutyCycle() method instead to set the duty cycle to zero to stop PWM.

  • Yes, I also found that the pthread started in start() was not collected in stop(), thus threads are accumulated and not able to create any new ones after reached a threshold. Jun 14, 2017 at 2:35

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.