UPDATE: The fan is now controlled automatically in the latest updates (See this answer https://askubuntu.com/a/1497778/1746852). This cron job is no longer needed.
A quick hack to get around the fact Ubuntu for the Pi5 currently does not control the fan.
This needs to be run with sudo permissions:
sudo python3 pi5_fan_controller.py
And, by default, will monitor the Pi5's temperature for one minute every 2 seconds adjusting the fan based on some arbitrary boundaries I came up with on the spot :^)
This is intended to be set up as a system-wide cron job (system-wide because of the sudo privileges). To do this, edit this file:
sudo nano /etc/crontab
And add this cron job:
* * * * * root python3 /path/to/pi5_fan_controller.py
(with the updated path to the python file)
Many thanks to the following Ask Ubuntu answers:
Hi James. I have a NAS with a Raspberry Pi 5 and a built in fan/hat automatically controlled. The case I've made for the NAS also has a large (40mm) 2 phase case fan that is just on or off (for cooling the RPI5 and an NVME/PCie SSD + 2 USB3/SATA SSDs). What I'd planned to do is write some code to controlled this fan via GPIO and based on the RPI5 temperature. E.g fan strictly on or off as a back up to the on board fan/hat that now comes with the RPI5. Any thoughts ??? Your code is used via Cron rather than using a daemon. Any particular reason? Many thanks Les Adams ...