This project consists of a Python script and a systemd service unit file to control the speed of a 40mm 5V PWM fan on a Raspberry Pi. The fan speed is adjusted proportionally to the CPU temperature, ideal for setups like the Pi Desktop Case with an OLED Stats Display. The project is based on instructions and code by Michael Klements.
- FanProportional.py - Python script to control the fan speed.
- systemd/pwmfancontrol.service - Systemd service unit file to run the Python script as a service.
This Python script reads the CPU temperature of the Raspberry Pi and adjusts the speed of a connected PWM fan accordingly. The fan speed is set between a minimum and maximum speed, corresponding to a defined range of CPU temperatures.
- Dependencies: RPi.GPIO, time, subprocess, numpy
- GPIO Mode: BCM
- PWM Frequency: 100Hz
- Temperature and Speed Range: Customize the
minTemp,maxTemp,minSpeed, andmaxSpeedvariables as needed.
This systemd service file is used to run FanProportional.py as a service on the Raspberry Pi, ensuring that it starts automatically on boot.
- Service Type: simple
- ExecStart: Path to the Python interpreter and the
FanProportional.pyscript. - Restart Policy: The service will restart on failure.
- User: root
- Environment: PYTHONUNBUFFERED=1
- Place
FanProportional.pyin/opt/pwmfancontrol/. - Place
pwmfancontrol.servicein/etc/systemd/system/. - Make
FanProportional.pyexecutable:chmod +x /opt/pwmfancontrol/FanProportional.py. - Reload systemd:
sudo systemctl daemon-reload. - Enable the service:
sudo systemctl enable pwmfancontrol.service. - Start the service:
sudo systemctl start pwmfancontrol.service.
Once installed, the service will run automatically on boot. The fan speed will adjust based on the Raspberry Pi's CPU temperature.
- Ensure the GPIO pin and PWM frequency in the Python script match your fan's specifications.
- Check the status of the service with
sudo systemctl status pwmfancontrol.service.
- Script and concept by Michael Klements.
- Adaptation and systemd integration for Raspberry Pi by Michael Ramstein
- NumPy integration by Philippe Enzler