The script was originally published by https://github.com/DriftKingTW
Due to the recent architectural changes (RPi.GPIO cannot operate on the GPIO), the original version of the script cannot work on the PI5.
I modified the script to make it suitable for the Raspberry PI 5.
The script can be used with ANY fan, using proper interfacing techniques. Anyway, any 5V PWM NOCTUA fans have the advantage of being very quiet and having the PWM control cable directly connectable with GPIO pin of the PI.
This YT short can show you how it works:
https://youtube.com/shorts/Tli3IQgPb0Y?si=yvr9UhOcsW-5ewzx
- YELLOW cable = +5V
- BLACK cable = GND
- GREEN CABLE = RPM sensing (needs a voltage divider, at least)
- BLUE cable = PWM control (it is an open circuit with pull-up and i measured 2.6V on the cable, making it suitable for a direct connection to the GPIO header)
You can eventually use the origina fan connector, after some modifications are made on it. Check the Pictures below.
Basically, you should change the PIN order to get from GND/+5V/RPM/PWM to +5V/GND/PWM/empty on the connector (the green cable should not be connected, if not with a proper interfacing circuit). Then you can stick the connector on the GPIO header, connecting the YELLOW cable to PIN 4, BLACK on 6 and BLUE on pin 8: doing so, you will have to use GPIO 14 to control the speed of your fan.
I measured the tension on the NOCTUA PWM pin (blue cable) and it was 2.6V in my case. ALWAYS check the tension on that pin before using your fan to the GPIO 14 on your PI: it should be lower than 3.3V!!!
One wrong FAN and you will fry the PI. You invert the connector, you will fry your fan. The connector of your fan was not modified before connecting it? You'll fry your PI.
Be carefull.
At the beginning of the script, you can find many useful parameter:
FAN_PIN # BCM pin used to drive PWM fan
WAIT_TIME # [s] Time to wait between each refresh
PWM_FREQ # [kHz] 25kHz for Noctua PWM control
MIN_TEMP # under this temp value fan is switched to the FAN_OFF speed
MAX_TEMP # over this temp value fan is switched to the FAN_MAX speed
FAN_LOW # lower side of the fan speed range during cooling
FAN_HIGH # higher side of the fan speed range during cooling
FAN_OFF # fan speed to set if the detected temp is below MIN_TEMP
FAN_MAX # fan speed to set if the detected temp is above MAX_TEMP
This is my first Python project and my first GitHub contribution.
I had to replace RPi.GPIO from the original script with gpiozero: gpiozero requires pwm values in the range between 0 and 1, where 0 stops the FAN and 1 speeds it up to 100% of the nominal speed using PWM.
That's why the speed values generated by the original script is divided by 100.
def setFanSpeed(speed):
pwm_fan.value = speed/100 # divide by 100 to get values from 0 to 1
return()
At the time of publishing, i only spent 2 hours on the project. Next i will focus on the RPM sensing script that is included but not modified.
Hope the solution will provide some relief to your hears in the meanwhile: it works really great. Thanks to DriftKingTW for his contribution.
You need two files from the repository:
- pifancontrol.service
- fan_control.py
Collect the two files the way you prefer and copy them in the locations suggested below.
You can simply...
git clone https://github.com/franganghi/Raspberry-Pi5-PWM-Fan-Control.git
cd Raspberry-Pi5-PWM-Fan-Control/
sudo cp pifancontrol.service /lib/systemd/system/pifancontrol.service
sudo cp fan_control.py /usr/local/sbin/
sudo chmod 644 /lib/systemd/system/pifancontrol.service
sudo chmod +x /usr/local/sbin/fan_control.py
sudo systemctl daemon-reload
sudo systemctl enable pifancontrol.service
sudo systemctl start pifancontrol.service
user@host:~ $ sudo service pifancontrol status
● pifancontrol.service - Dynamic FAN control
Loaded: loaded (/lib/systemd/system/pifancontrol.service; enabled; preset: enabled)
Active: active (running) since Thu 2024-01-04 12:51:13 CET; 19s ago
Main PID: 2158 (python3)
Tasks: 4 (limit: 4453)
CPU: 91ms
CGroup: /system.slice/pifancontrol.service
└─2158 /usr/bin/python3 /usr/local/sbin/fan_control.py
sudo systemctl stop pifancontrol.service
sudo systemctl disable pifancontrol.service
sudo systemctl daemon-reload
sudo rm /usr/local/sbin/fan_control.py
sudo rm /lib/systemd/system/pifancontrol.service
The script will be started on boot and will be restarted in case of errors.
Enjoy.
Giovanni - Rome - Italy