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

Add support for channels 2 and 3 to control GPIO_18 and GPIO_19 on the Pi 5 #11

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading