RPI4 PWM fan control

After several years of using regular cases for my rpi4, i came to the point of being super annoyed by the fan noise.

I've tried several cases over the years, all failed my ears

So I've decided to buy a PWM fan that would only spin with a ratio based on the current temperature of the CPU. In principle, since my pi is mostly idling, I should then never hear the fan in my living room.

After scrolling a bit in AliExpress, I picked the 52pi Ice Tower Cpu cooling fan.

The Ice tower comes in 2 models: vertical or horizontal. I bought the latter

After receiving the product, I knew it wouldn't just work miraculously after wiring it up. So I started looking for a solution to control the PWM from my Librelec distribution and found these:

GitHub - FranzAT/libreelec_PWM_fan: Cooling Raspberry Pi, which is running LibreELEC, by controlling the fan speed of a 5V fan.
Cooling Raspberry Pi, which is running LibreELEC, by controlling the fan speed of a 5V fan. - GitHub - FranzAT/libreelec_PWM_fan: Cooling Raspberry Pi, which is running LibreELEC, by controlling th...
GitHub - krdh/libreelec-RPi-pwm-fan: PWM (3-pin/4pin) fan control for Libreelec on RaspberryPi
PWM (3-pin/4pin) fan control for Libreelec on RaspberryPi - GitHub - krdh/libreelec-RPi-pwm-fan: PWM (3-pin/4pin) fan control for Libreelec on RaspberryPi

The first project does not require a PWM fan. You can use it with a basic 5V fan but you'll need a transistor to adjust the fan speed. That's not what I am looking for but it was a good place to get started since the tutorial is well-written, especially for the creation of a service that will run automatically when the pi boots.

The second project assumes that you're controlling a PWM fan and lets you define the PWM pin (`FAN_PIN`). Perfect !

Note: With Librelec, you'll need the install the pi tools add-on

Addons --> install from repository --> LibreELEC Add-ons --> Program Addons --> Raspberry PiTools
  1. First, let's set the FAN_PIN in the script. I've wired the PWM wire of my fan (blue) to TXD (GPIO 14). Be careful, the scripts sets the configuration mode to BOARD which means that putting 14 won't work. You should use the pin number instead which is 8 here:
FAN_PIN   = 8   ### RaspberryPi GPIO PI used to drive PWM pin of Fan
  1. ssh into your pi and create a folder for the python script
> ssh root@<ip>
...
> mkdir .kodi/userdata/PWM_fan
> exit
  1. Now transfer the python script to your rpi
> scp C:\Users\cemni\Downloads\libreelec_pwm_fancontol.py root@<ip>:~/storage/.kodi/userdata/PWM_fan/PWM_fan.py
  1. Transfer the service file as well
> scp C:\Users\cemni\Downloada\PWM_fan.service root@<ip>:~/.config/system.d/PWM_fan.service
  1. Configure the service to start with the pi
> ssh root@<ip>
...
> chmod 755 .config/system.d/PWM_fan.service
> systemctl enable PWM_fan
> systemctl start PWM_fan
> exit
  1. Tune the script parameters
    This is the most time-consuming part. The default parameters of the script were not satisfying because the fan had a high pitch sound, worst than my original setup ! So i changed PWM_FREQ until finding a solution that is quiet enough. PWM_FREQ = 23 worked for me but it could be different if you're using another type of fan.

An that's it ! My rpi is now dead silent and I can finally chill in my living room while reading a book.