Access the hardware PWM of a Raspberry Pi
OTHER License
Access the hardware PWM of a Raspberry Pi with Python. More lightweight than alternatives.
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
.
GPIO_18
to GPIO_12
and GPIO_19
to GPIO_13
using dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
.lsmod | grep pwm
and looking for pwm_bcm2835
sudo pip3 install rpi-hardware-pwm
For Rpi 1,2,3,4, use chip=0; For Rpi 5, use chip=2
from rpi_hardware_pwm import HardwarePWM
pwm = HardwarePWM(pwm_channel=0, hz=60, chip=0)
pwm.start(100) # full duty cycle
pwm.change_duty_cycle(50)
pwm.change_frequency(25_000)
pwm.stop()
The original code is from jdimpson/syspwm, We've updated it to Python3 and
made it look like the RPi.GPIO
library's API (but more Pythonic than that.), and we use it in Pioreactor bioreactor system.