Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Add pwm support #40

Open
pedrohoffmann opened this issue Feb 5, 2019 · 5 comments
Open

[feature request] Add pwm support #40

pedrohoffmann opened this issue Feb 5, 2019 · 5 comments

Comments

@pedrohoffmann
Copy link

Hello,
Do you plan to add Pwm support the the library?

@rm-hull
Copy link
Owner

rm-hull commented Feb 5, 2019

I have no plans to add i myself, but PRs welcome

@pedrohoffmann
Copy link
Author

No problem, im a noob on dev, but I'll take a look and try :)
great job on this btw

@enesbcs
Copy link

enesbcs commented Apr 15, 2020

There is a simple SoftPWM (not so fast) implementation which can be used with OPi.GPIO by some line changes. (it is for pyA20 originally)

https://github.com/evergreen-it-dev/orangepwm/blob/master/orangepwm.py

@gounthar
Copy link

gounthar commented Nov 18, 2021

There is a Demo/PWM_Demo.py that does nothing on my machine (orangepizero, OS: Debian 11 bullseye, Kernel: armv7l Linux 5.10.60-sunxi), even if I manage to get a LED blinking with a simple script (with pin 7, BOARD mode).
In the PWM_Demo.py file, the PWM_pin is listed as 0. When I use 7 as for the simple LED blink, I get this error:

OSError: [Errno 19] No such device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/OPi.GPIO/Demo/PWM_Demo.py", line 10, in <module>
    p = GPIO.PWM(PWM_chip, PWM_pin, frequency_Hz, Duty_Cycle_Percent)    # new PWM on channel=LED_gpio frequency=38KHz
  File "/usr/local/lib/python3.9/dist-packages/OPi.GPIO-0.5.2-py3.9.egg/OPi/GPIO.py", line 743, in __init__
    raise e
  File "/usr/local/lib/python3.9/dist-packages/OPi.GPIO-0.5.2-py3.9.egg/OPi/GPIO.py", line 729, in __init__
    sysfs.PWM_Export(chip, pin)  # creates the pwm sysfs object
  File "/usr/local/lib/python3.9/dist-packages/OPi.GPIO-0.5.2-py3.9.egg/OPi/sysfs.py", line 95, in PWM_Export
    fp.write(str(pin))
OSError: [Errno 19] No such device

In armbian-config, I selected pwm and w1-gpio.
Here is the resulting file I used:

import orangepi.zero
import OPi.GPIO as GPIO
from time import sleep          # this lets us have a time delay

PWM_chip = 0
PWM_pin = 7
frequency_Hz = 3800
Duty_Cycle_Percent = 100
channel =  PWM_pin

# Simple mode, let's make it blink-kkk
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PWM_pin, GPIO.OUT)
GPIO.output(PWM_pin, GPIO.HIGH)
sleep(0.1)
GPIO.output(PWM_pin, GPIO.LOW)
sleep(0.1)
GPIO.output(PWM_pin, GPIO.HIGH)
sleep(0.1)
# Let's play with PWM now
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_UP)
p = GPIO.PWM(PWM_chip, 0, frequency_Hz, Duty_Cycle_Percent)    # new PWM on channel=LED_gpio frequency=38KHz

print("turn on pwm by pressing button")
input()
p.start_pwm()

print("dimm pwm by pressing button")
input()
p.duty_cycle(50)

print("change pwm frequency by pressing button")
input()
p.change_frequency(500)

print("stop pwm by reducing duty cycle to 0 by pressing button")
input()
p.stop_pwm()

print("change polarity by pressing button")
input()
p.pwm_polarity()

print("increase duty cycle but inverted so light will dim. press button to contunue")
input()
p.duty_cycle(75)

print("duty cycle reduced press button to contunue")
input()
p.duty_cycle(25)

print("stop pwm (it was inverted so it shoudl be full brightness), press button to contunue")
input()
p.stop_pwm()

print("remove object and deactivate pwm pin, press button to contunue")
input()
p.pwm_close()

del p  # delete the class

GPIO.cleanup()

Do you know what I did wrong?

Thanks.

@Fionnoch
Copy link
Contributor

Fionnoch commented Aug 5, 2022

I think you're changing the pwm chip not the pin. Try leaving the chip as 0 and see what happens, if it gives an error saying the device doesn't exist try change it to 1, you shouldn't go above 2. Note as well that the chip is mapped to a specific pin and that pin may not be whats listed as the pwm pin on the pinout diagrams for the board (its a pain I know). For example in the image below I pointed where the pwm pin is located on that board.

Hope this helps!

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants