Skip to content

Commit 2edaea3

Browse files
Merge pull request #11 from colinpate/colin_fix_channel
Add support for channels 2 and 3 to control GPIO_18 and GPIO_19 on the Pi 5
2 parents 3d246bb + b069a46 commit 2edaea3

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Access the hardware PWM of a Raspberry Pi with Python. More lightweight than alt
88

99
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`.
1010
- 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`.
11+
- 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
12+
- On all other models, use channels 0 and 1 to control GPIO-18 and GPIO_19, respectively
1113
2. **Reboot your Raspberry Pi**.
1214
- You can check everything is working on running `lsmod | grep pwm` and looking for `pwm_bcm2835`
1315
3. Install this library: `sudo pip3 install rpi-hardware-pwm`

rpi_hardware_pwm/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class HardwarePWM:
2828
Notes
2929
--------
3030
- For Rpi 1,2,3,4, use chip=0; For Rpi 5, use chip=2
31+
- For Rpi 1,2,3,4 only channels 0 and 1 are available
3132
- If you get "write error: Invalid argument" - you have to set duty_cycle to 0 before changing period
3233
- /sys/ pwm interface described here: https://jumpnowtek.com/rpi/Using-the-Raspberry-Pi-Hardware-PWM-timers.html
3334
@@ -38,8 +39,8 @@ class HardwarePWM:
3839

3940
def __init__(self, pwm_channel: int, hz: float, chip: int = 0) -> None:
4041

41-
if pwm_channel not in {0, 1}:
42-
raise HardwarePWMException("Only channel 0 and 1 are available on the Rpi.")
42+
if pwm_channel not in {0, 1, 2, 3}:
43+
raise HardwarePWMException("Only channel 0 and 1 and 2 and 3 are available on the Rpi.")
4344

4445
self.chippath: str = f"/sys/class/pwm/pwmchip{chip}"
4546
self.pwm_channel = pwm_channel

0 commit comments

Comments
 (0)