Rp5 Trials - PWM anyone?

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,630
Trying to use the Pi5 PWM feature for pin 32 = GPIO12. Using Python code.
I have never come accross so many contradictory instructions for suposedly achieving this that are out there.
Has anyone here succesfully done it in practice?
 

panic mode

Joined Oct 10, 2011
4,967
not in a while...

software PWM is possible but you would want hardware PWM. but that requires enabling it.
from command line:
sudo nano /boot/firmware/config.txt

then add following line
dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4

save and close
then reboot pi


rpi software pwm:
from gpiozero import PWMOutputDevice
from time import sleep

# Initialize GPIO12 (Pin 32)
# Frequency defaults to 100Hz; you can specify it here.
pwm = PWMOutputDevice(12, frequency=1000)

try:
    while True:
        # value is the duty cycle from 0.0 to 1.0
        pwm.value = 0.5  # 50% duty cycle
        print("PWM at 50%")
        sleep(2)
      
        pwm.value = 0.1  # 10% duty cycle
        print("PWM at 10%")
        sleep(2)

except KeyboardInterrupt:
    pwm.close()
fir high precision hardware pwm install rpi-hardware-pwm library ( pip install rpi-hardware-pwm )
then

rpi hw pwm:
from rpi_hardware_pwm import HardwarePWM

# GPIO12 is Channel 0 on the Pi 5
pwm = HardwarePWM(pwm_channel=0, hz=1000, chip=2)
pwm.start(50) # 50% duty cycle
 
Last edited:

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,630
Thanks I will give it a try,
From what I have I have gathered so far, Pi5 PWM is a veritable minefield !
I have seen in a couple of places mentioning that the fan is using one of the PWM outputs, I am not sure how I can confirm this and which designated PWM exactly is being used, ?
 
Top