2

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

#!/usr/bin/python

import RPi.GPIO as gpio
import time
import random

gpio.setmode(gpio.BCM)
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)
        pwm.ChangeFrequency(100)
        pwm.start(50)
    else:
        num_stop += 1
        print("stop %d" % num_stop)
        pwm.stop()
    time.sleep(0.1)

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?

4

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. – jiandingzhe Jun 14 '17 at 2:35

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.