PWM flicker between pwm duty changes



  • So after trying out quite a few different power ramp circuits from around the web to make an LED "breath", I decided I would give using pwm to change the brightness of the LED. Calling fast-gpio pwm 1 50 50 seemed to have the desired results, and other calls with varying duty percents had the desired effect. The LED would brighten or dim to about what you'd expect calling 10%, 50% or 100%. 50Hz also seemed to be the minimum frequency I could go to before the flicker became noticeable.

    So then I wrote a python script to cycle from 1% duty cycle up to 100% and then back down to 1%. The script worked great! I could watch the LED brighten and dim over the course of a few seconds, but the flicker became very noticeable. I figured I'd crank the frequency up, so I set it to 5000 and ran the script again. Again, the flicker showed up. I tried 1000Hz, 2000Hz, numbers in between, but never got the flicker to go away.

    So I figured, maybe it's because of the amount of time I'm waiting between duty changes. So I shortened the time between changes from 0.5 to 0.1, then to 0.01, then to 0.001. While the flickers became less noticeable at 0.1, that seemed to be as good as it got. Any subsequent fractions of a second didn't improve the situation.

    Is this a limitation of the hardware? Is fast-gpio pwm doing something that causes the pin to go low between calls, creating the flicker? Is there a way around fast-gpio that I can call on to change the pwm duty percentage without re-calling the whole command?

    Let me know if you've encountered this too and what you did to get around it! Right now those ramp circuits are looking pretty good...

    Code below, in Python:

    import os
    import sys
    import time
    import subprocess
    
    duty=1
    pulse="50"
    sleeper=0.001
    min=1
    max=100
    direction="up"
    
    read="fast-gpio read 2"
    pwm="fast-gpio pwm 1 " + pulse + " "
    
    high="> Read GPIO2: 1\n"
    
    os.system("gpioctl dirout 1")
    os.system("gpioctl dirin 2")
    
    status = subprocess.check_output(read, shell=True)
    
    while(status != high):
            print(duty)
            if(duty == max):
                    direction="down"
                    duty = duty - 1
            elif(duty == min):
                    direction="up"
                    duty = duty + 1
            elif(duty > min and direction == "up"):
                    duty = duty + 1
            elif(duty < max and direction == "down"):
                    duty = duty - 1
            status=subprocess.check_output(read, shell=True)
            os.system(pwm + str(duty))
            time.sleep(sleeper)
    
    os.system("fast-gpio set 1 0")
    

Log in to reply
 

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.