This is using https://pypi.org/project/rpi-hardware-pwm/ Python library in order to generate an PWM signal using integrated Raspberry Pi hardware PWM driver to control the fan speed.
RPI_5
to True
!
sudo pip3 install rpi-hardware-pwm
Make sure you connect the control port of the fan to the pin 12. You can also use pin 35, but change PWM_CHANNEL to 1.
Inside the /boot/config.txt
you should have set dtoverlay=pwm-2chan
echo "dtoverlay=pwm-2chan" | sudo tee -a /boot/config.txt
To install, you need to be super user, so use sudo.
sudo wget -O /usr/local/bin/fan_control.py https://gist.githubusercontent.com/cheadrian/5616f2980b96e5044622c929fbdaea70/raw/fan_control.py
sudo chmod +x /usr/local/bin/fan_control.py
If you want this to automatically run at startup, then create an service that will run the script:
sudo wget -O /etc/systemd/system/fan_control.service https://gist.githubusercontent.com/cheadrian/5616f2980b96e5044622c929fbdaea70/raw/fan_control.service
# Enable and start the service
sudo systemctl enable fan_control.service
sudo systemctl start fan_control.service
You can adjust frequency, temperature, and other variables by editing the fan_control.py :
sudo nano /usr/local/bin/fan_control.py