raspberry-pi-pwm-fan-control
raspberry-pi-pwm-fan-control copied to clipboard
High CPU usage, and which PWM_FREQ?
Hello! Thank you very much for this, it's been very handy. I just wanted to ask two questions:
- Typing ´docker stats´ shows me the container is around 60% to 80% CPU, why is this so high? The 'fan.py' process in
htop
also shows the same CPU usage, but the cores above don't seem to be working that much (around 3%) - Should I edit the PWM_FREQ to something appropriate for my own fan? I'm using this fan
This is a Raspberry Pi 4B 8GB with a 64-bit Ubuntu server
I have set the PWM_FREQ to 25, and CPU usage has lowered to ~2%, and temps and fan noise seem to be ok.
As pointed to me elsewere, a hardware PWM solution wouldn't use much of the CPU, so this PWM solution is software-based right? Are there any hardware solution alternatives?
I connected a 5v mini fan to the 3v output. i need to tip it with my finger that it starts running, but then steady and very slow and silent. It’s fine to cool my Tinkerboard with node red down from 50 to 30 degree. No software needed
Update: i commit a new version with hardware PWM.
@josejsarmento the changes to enable hardware PWM are minimal. Just use https://github.com/Pioreactor/rpi_hardware_pwm library and follow the steps to enable PWM in config.txt.
Changes to apply: line 4:
- import lgpio as sbc
+ from rpi_hardware_pwm import HardwarePWM
line 127-129:
- fan = sbc.gpiochip_open(0)
- sbc.gpio_claim_output(fan, PWM_GPIO_NR)
- setFanSpeed(FAN_LOW,MIN_TEMP)
+ pwm = HardwarePWM(pwm_channel=0, hz= PWM_FREQ)
+ pwm.start(FAN_LOW) # full duty cycle
line 76:
- sbc.tx_pwm(fan , PWM_GPIO_NR, PWM_FREQ, speed, pulse_offset=0, pulse_cycles=0)
+ pwm.change_frequency(speed)
On my Raspberry PI 4 the cpu load dropped after one minute to 0.1%
Thanks @mariusmotea for the suggestion! Works perfectly 👍
I only had to change
+ pwm.change_frequency(speed)
to pwm.change_duty_cycle(speed)