Collection of scripts to control the PWM fan speed on my Ubuntu 22.04 Raspberry Pi 4. Details of the PWM fan case and also sample demo Python scripts I used as a base can we found on the PI4-FAN-PWM Wiki page. To create the actual service creation part I drew insparation from a medium.com article
Note: I know thsi is not the most efficient code and will optimise it when I have some more time.
Creating the relevant Python script on your Raspberry Pi
-
Create a scripts folder, in my case it is under the
/home/$USER/
folder.mkdir scripts
-
Change to the scripts folder and then create a fan_service.py file and dump the contents of the fan_service.py file
cd scripts vi fan_service.py
-
You will also install the relevant Python Raspberry Pi GPIO dependancies using pip
sudo apt update pip install RPi.GPIO pip install gpiozero
-
The you should be ready to test the script. If you want to see the actual temrature and current fan speed make sure to uncomment the each
# print
line in thefan_service.py
filesudo python fan_service.py
Now lets create a service
- Now create the service so that the script automaticall starts on reboot and no manual intervention is needed. Make sure you copy over the contents of the
fan.service
file that is part of this repo. Make sure to replace the$USER
attrivute with your username where the script is storedsudo nano /etc/systemd/system/fan.service
- Now we need to reload the daemon
sudo systemctl daemon-reload
- Let’s enable enable the service
sudo systemctl enable fan.service
- Starting the service.
sudo systemctl start fan.service
Other userful service commands There are several commands you can do to start, stop, restart, and check status.
- To stop the service.
sudo systemctl stop fan.service
- To restart.
sudo systemctl restart fan.service
- To check status.
sudo systemctl status fan.service