Fading LEDs and Trying PWM Drivers along with the Linux sysfs

I have a PWM Driver and I was testing it for fading a LED.

#!/usr/bin/python3
 
from time import sleep
import signal
import sys
 
from pwm_driver import Pwm
 
LED = Pwm(path="/sys/class/pwm/pwmchip0/pwm0/", enabled=False, period=100000, duty_cycle=85000)
 
Duty = 0
low  = 0.8 # Smallest angle (in ms)
hi   = 2.4 # Largest angle (in ms)
pos  = 1.5 # Current position, about middle ms)
step = 0.4 # Step size to next position
 
try:
    LED.enabled=True
    while True:
        pos += step    # Take a step
        if(pos > hi or pos < low):
            step *= -1
        Duty = LED.duty_cycle
        Duty = str(pos)
        print('pos = ' + str(pos) + ' duty_cycle = ' + Duty)
        (LED.duty_cycle)
        sleep(0.2)
except KeyboardInterrupt:
    LED.enabled=False
    pass
 
# Trying to fade a LED
# This is from your (@zmatt) PWM driver written a while back and some code from docs.beagleboard.org

The driver allows for setting (path=’/sys/class/pwm/pwmchip0/pwm0/”, enabled=False, period=100000, duty_cycle=85000) and I was using the above code to handle this effort.

Does anyone see an error or how the above code from the driver may or may not work as intended?

That is where I got the code for the back and forth motion…

Why do you only want to set LED.enabled = False, when the user presses Ctrl+C ? Surely that should be in a finally: so it’s put back after any Exception at all, no matter what type it is. The pass is pointless too.

try:
    ...
except KeyboardInterrupt:
    LED.enabled=False
    pass

Does activating the PWM driver really only need LED.enabled = True, not some method call?

What’s the point in reassigning Duty, and the duplicating str(pos)? I don’t really understand the point of pos at all, other than the code’s best guess at what LED is doing. Isn’t there anything available in the pwm_driver API to introspect LED to get definitive information from?

If the sleep was explcitly related to step, that would at least make more sense as a time tracker.

1 Like

No…LED is made by me to call it later. Pwm is the class.

There is no point in reassigning Duty. I just figured it would make it easier. True and False for .enabled is how the code works that is being called.

while True:
    pos += step    # Take a step
    if(pos > hi or pos < low):
        step *= -1
    duty_cycle = str(round(pos*1000000))    # Convert ms to ns
    # print('pos = ' + str(pos) + ' duty_cycle = ' + duty_cycle)
    f.seek(0)
    f.write(duty_cycle)
    time.sleep(ms/1000)

That is some original code from the docs. pages: Motors — BeagleBoard Documentation

So, I could in fact add the sleep(ms/1000) code back to the source but I figured I could get away without it.

Also. I found this lib for PWM along with other peripheral accessing. See here: GitHub - linux4sam/mpio: Microchip Peripheral I/O Python Package · GitHub

Right now, I may be able to get it to work but came across some issues after changing the library up some.

1 Like