From 7e84c17e31e94179cb5aafdd5f39ce4d1ff6a415 Mon Sep 17 00:00:00 2001 From: colinpate Date: Tue, 6 Feb 2024 18:23:37 -0800 Subject: [PATCH 1/2] Update __init__.py to support channels 2, 3 for pi 5 --- rpi_hardware_pwm/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rpi_hardware_pwm/__init__.py b/rpi_hardware_pwm/__init__.py index 7ce3541..bc9d8be 100644 --- a/rpi_hardware_pwm/__init__.py +++ b/rpi_hardware_pwm/__init__.py @@ -28,6 +28,7 @@ class HardwarePWM: Notes -------- - For Rpi 1,2,3,4, use chip=0; For Rpi 5, use chip=2 + - For Rpi 1,2,3,4 only channels 0 and 1 are available - If you get "write error: Invalid argument" - you have to set duty_cycle to 0 before changing period - /sys/ pwm interface described here: https://jumpnowtek.com/rpi/Using-the-Raspberry-Pi-Hardware-PWM-timers.html @@ -38,8 +39,8 @@ class HardwarePWM: def __init__(self, pwm_channel: int, hz: float, chip: int = 0) -> None: - if pwm_channel not in {0, 1}: - raise HardwarePWMException("Only channel 0 and 1 are available on the Rpi.") + if pwm_channel not in {0, 1, 2, 3}: + raise HardwarePWMException("Only channel 0 and 1 and 2 and 3 are available on the Rpi.") self.chippath: str = f"/sys/class/pwm/pwmchip{chip}" self.pwm_channel = pwm_channel From b069a46086d6dfdfa871af76a37b11c5dbf55e87 Mon Sep 17 00:00:00 2001 From: colinpate Date: Tue, 6 Feb 2024 18:25:55 -0800 Subject: [PATCH 2/2] Update README.md to reflect Pi 5 channels --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7768b23..a03fe9f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Access the hardware PWM of a Raspberry Pi with Python. More lightweight than alt 1. On the Raspberry Pi, add `dtoverlay=pwm-2chan` to `/boot/config.txt`. This defaults to `GPIO_18` as the pin for `PWM0` and `GPIO_19` as the pin for `PWM1`. - Alternatively, you can change `GPIO_18` to `GPIO_12` and `GPIO_19` to `GPIO_13` using `dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4`. + - On the Pi 5, use channels 0 and 1 to control GPIO_12 and GPIO13, respectively; use channels 2 and 3 to control GPIO_18 and GPIO_19, respectively + - On all other models, use channels 0 and 1 to control GPIO-18 and GPIO_19, respectively 2. **Reboot your Raspberry Pi**. - You can check everything is working on running `lsmod | grep pwm` and looking for `pwm_bcm2835` 3. Install this library: `sudo pip3 install rpi-hardware-pwm`