Open schech1 opened 7 months ago
Hi @schech1, are you using the latest version (0.2.1, check with pip list
). If not, please upgrade. The RPi5 is supported by adding chip=2
, ex: HardwarePWM(..., chip=2)
Yes, I'm using 0.2.1 and i also used chip=2
.
I also believe that issue is not really related with rpi_hardware_pwm.
But should I see pwm_bcm2835
when using lsmod | grep pwm
on a RPi5 after reboot?
Testing on my Rpi5, no, I don't see lsmod | grep pwm
return anything
I'm trying to figure out what I do wrong. I connected an LED to GPIO 18 and ran this simple test:
from gpiozero import PWMLED
from rpi_hardware_pwm import HardwarePWM
from time import sleep
# Not working
pwm = HardwarePWM(pwm_channel=0, hz=60, chip=2)
pwm.start(100)
sleep(2)
pwm.stop()
# Working
led = PWMLED(18, frequency = 200)
led.value = 1
sleep(1)
led.value = 0
Using gpiozero
on the same pin, it works.
Is there anything I can do to investigate further?
hm, try the following in boot/config.txt
(this is from my RPi5):
dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
then try the same code, but targeting pin 12. Maybe something is different with the other pins?
Yeah, that works. Got it working no on pin 12 and 13.
Okay, interesting, something must be different with RPi5 for the original pins. I'll have to investigate later though.
This solution works for me as well. Thank you for this library!!
I still got problems on Pi5.
config.txt: dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
python: pwm = HardwarePWM(pwm_channel=1, hz=frequency, chip=2) pwm.start(50)
I get no PWM output on Pin 13. What am i missing? Thanks in advance.
@wayneOS1337 not sure - did you do a reboot? Can you paste here your entire config.txt file?
Yes, i did a reboot. My config.txt is:
# For more options and information see
# http://rptl.io/configtxt
# Some settings may impact device functionality. See link above for details
# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
# Additional overlays and parameters are documented
# /boot/firmware/overlays/README
# Automatically load overlays for detected cameras
camera_auto_detect=1
# Automatically load overlays for detected DSI displays
display_auto_detect=1
# Automatically load initramfs files, if found
auto_initramfs=1
# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2
# Don't have the firmware create an initial video= setting in cmdline.txt.
# Use the kernel's default instead.
disable_fw_kms_setup=1
# Run in 64-bit mode
arm_64bit=1
# Disable compensation for displays with overscan
disable_overscan=1
# Run as fast as firmware / board allows
arm_boost=1
[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller.
# This line should be removed if the legacy DWC2 controller is required
# (e.g. for USB device mode) or if USB support is not required.
otg_mode=1
[all]
dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
So basically i just added the pwm-2chan
line on the end. I did this previously on a Pi4 and everythings was working smooth with "rpi_hardware_pwm".
But my guess is that the dtoverlay
is not working correctly. After reboot i get for:
$ pinctrl get 12,13
12: no pd | -- // PIN32/GPIO12 = none
13: no pd | -- // PIN33/GPIO13 = none
i also tried to just use dtoverlay=pwm-2chan
or dtoverlay=pwm
in config.txt, to use it with the default GPIO 18
but to no avail.
There is an errant ` at the end of the last line of your config.txt: is that a type in github or your config?
Yes, sorry that was a typo in my comment. i edited my comment. The file is without it.
Not sure! How are you measuring PWM on a pin?
I tested it with a Servo (same settings as on the Pi4) and also with a LED. Did you use Bookworm as OS? or is there any additional Software i need to install?
I have the same issue with Rpi 5. Even with dtoverlay=pwm
lsmod | grep pwm
gives nothing.
pinctrl get 18
gives: 18: a3 pd | lo // PIN12/GPIO18 = PWM0_CHAN2
I see this error on startup:
clk: couldn't set clk_pwm0 clk rate to 100000000 (-22), current rate: 30720000
Problem is the pwmchip index. https://forums.raspberrypi.com/viewtopic.php?t=359251&sid=5c309b2e5afd4af2b928bda747f4296b#p2154764
I wanted to use GPIO18 pwm (dtoverlay=pwm
added to config.txt).
I used: HardwarePWM(pwm_channel=2, hz=60, chip=2)
Modified a line to accept more channels: https://github.com/Pioreactor/rpi_hardware_pwm/blob/main/rpi_hardware_pwm/__init__.py#L41
Works as it should. I haven't tested any other channels or GPIO:s.
I've released a 0.2.2 version that allows for more chips, thanks to @colinpate for the PR
Hi there, I`m having a simliar issue.
the command pwm = HardwarePWM(pwm_channel=0, hz=100, chip=2)
throws the follwing error:
[Errno 22] Invalid argument
File "/home/raspi4/Projects/scripts/control_motor.py", line 7, in set_motor_speed
pwm = HardwarePWM(pwm_channel=0, hz=100, chip=2)
See attachement for /boot/firmware/config.txt file config.txt
Thanks a lot for helping!
Hi, Try this: pwm = HardwarePWM(pwm_channel=2, hz=100, chip=2) If it doesn't work, please post your full code.
Hi, thanks for the fast replay.
that leads to the same error:
Exception has occurred: OSError [Errno 22] Invalid argument
my full code is:
from rpi_hardware_pwm import HardwarePWM
import time
from loguru import logger
def set_motor_speed(speed: int) -> None:
pwm = HardwarePWM(pwm_channel=2, hz=100, chip=2)
pwm.start(100) # full duty cycle
time.sleep(3)
pwm.change_duty_cycle(20)
pwm.stop()
duty_cycle = 10 + (speed / 9000) * 80
def stop_motor() -> None:
pass
# pwm.change_duty_cycle(10)
# pwm.stop()
def read_values() -> tuple:
pass
if __name__ == "__main__":
set_motor_speed(9000)
@bbfjumper perhaps is a kernel issue: https://forums.raspberrypi.com/viewtopic.php?t=367294 (a new kernel was released in the 24-03-12 image)
I'll be watching that thread for more
okay that could be the solution, i assume downgrading would solve the problem, what kernel version are you using?
I'm using the 6.1.63, from the 2023-12-11 release.
I'm having the same invalid argument issue with my Raspberry Pi 5 kernel 6.6. Could you instruct me on how to downgrade the kernel to 6.1.63? When I use the legacy version of the 6.1 kernel from Raspberry Pi Imager v1.8.5 for RPi 5, I just can't finish the boot...
When I use the legacy version of the 6.1 kernel from Raspberry Pi Imager v1.8.5 for RPi 5, I just can't finish the boot...
Try downloading an older image from here: https://www.raspberrypi.com/software/operating-systems/
with downgrading my kernel to 6.1.63 it works, thanks everbody for helping me.
I downgraded with sudo rpi-update 3cfd50755ffbb2f952408b59df1a9a63d3af5387
with SHA from: https://github.com/raspberrypi/rpi-firmware/commits/master/?after=f63715223f7cf0ce0cb3be57361ecab8d917699c+34
The Kernel issue seems to be fixed in 6.6.22 version. I had the same error with 6.6.20, but not with 6.6.22.
Yes, after updating the kernel from 6.6.20
to 6.6.22
, the PWM fan started working properly.
Encountered same Invalid Argument issue with my Raspberry Pi5 on Ubuntu 24.04 Kernel 6.8.0.
Linux ubuntu 6.8.0-1007-raspi #7-Ubuntu SMP PREEMPT_DYNAMIC Mon Jun 24 10:21:12 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
My code is as followed
from rpi_hardware_pwm import HardwarePWM
from time import sleep
pwm=HardwarePWM(0,60,chip=2)#channel 0 1 2 3 for GPIO12 13 18 19 respectively
pwm.start(100)
pwm.change_duty_cycle(0.5/10)
# pwm.change_frequency(25_000)
sleep(2)
pwm.stop()
added dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
to config.txt
When I execute the code without sudo, I got error log as followed
Traceback (most recent call last):
File "/home/pi/C2/hardwarepwm.py", line 3, in <module>
pwm=HardwarePWM(0,60,chip=2)#channel 0 1 2 3 for GPIO12 13 18 19 respectively
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/rpi_hardware_pwm/__init__.py", line 56, in __init__
raise HardwarePWMException(f"Need write access to files in '{self.chippath}'")
rpi_hardware_pwm.HardwarePWMException: Need write access to files in '/sys/class/pwm/pwmchip2'
When I execute the code with sudo, I got error log showing Invalid Argument
OSError: [Errno 22] Invalid argument
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/C2/hardwarepwm.py", line 5, in <module>
pwm.change_duty_cycle(0.5/10)
File "/usr/local/lib/python3.12/dist-packages/rpi_hardware_pwm/__init__.py", line 105, in change_duty_cycle
self.echo(dc, os.path.join(self.pwm_dir, "duty_cycle"))
File "/usr/local/lib/python3.12/dist-packages/rpi_hardware_pwm/__init__.py", line 78, in echo
with open(file, "w") as f:
OSError: [Errno 22] Invalid argument
I added
dtoverlay=pwm-2chan
to/boot/config.txt
on a Raspberry Pi 5, butlsmod | grep pwm
does not show any output. Also there is no pwm-signal generated either on GPIO19 or 18.