Skip to content

Commit

Permalink
Merge pull request #11 from colinpate/colin_fix_channel
Browse files Browse the repository at this point in the history
Add support for channels 2 and 3 to control GPIO_18 and GPIO_19 on the Pi 5
  • Loading branch information
CamDavidsonPilon authored Mar 6, 2024
2 parents 3d246bb + b069a46 commit 2edaea3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 3 additions & 2 deletions rpi_hardware_pwm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2edaea3

Please sign in to comment.