raspberry-pi-pwm-fan-control icon indicating copy to clipboard operation
raspberry-pi-pwm-fan-control copied to clipboard

High CPU usage, and which PWM_FREQ?

Open josejsarmento opened this issue 2 years ago • 5 comments

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

josejsarmento avatar Jul 18 '22 11:07 josejsarmento

I have set the PWM_FREQ to 25, and CPU usage has lowered to ~2%, and temps and fan noise seem to be ok.

josejsarmento avatar Jul 18 '22 16:07 josejsarmento

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?

josejsarmento avatar Jul 18 '22 22:07 josejsarmento

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

andesse avatar Jul 18 '22 22:07 andesse

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%

mariusmotea avatar Sep 25 '22 17:09 mariusmotea

Thanks @mariusmotea for the suggestion! Works perfectly 👍

I only had to change

  + pwm.change_frequency(speed)

to pwm.change_duty_cycle(speed)

therealthingy avatar Sep 10 '23 11:09 therealthingy