This project provides examples of fan control based on the current CPU temperature.
It includes implementations of a relay controller and a proportional controller.
The fan is connected via a transistor switch to the 4th pin of the I/O ports. The fan connection pin can be changed in scripts. The remaining parameters can be set at startup.
Clone repository:
git clone https://github.com/Nkeramov/rpi_fan_control.gitSwitch to repo directory:
cd rpi_fan_controlCreate and activate virtual environment:
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
./venv/Scripts/activate # WindowsInstall dependencies:
pip install -r requirements.txtRun relay fan controller with command (args for example):
python3 relay_controller.py --lower 45 --upper 50 --delay 5Run proportional fan controller with command (args for example):
python3 proportional_controller.py --temp 45 --delay 5 --p 15.0 --dmin 50 --dmax 100Install dependencies and create virtual environment automatically:
uv syncRun relay fan controller with command (virtual environment is handled automatically, args for example):
uv run relay_controller.py --lower 45 --upper 50 --delay 5Run proportional fan controller with command (virtual environment is handled automatically, args for example):
uv run proportional_controller.py --temp 45 --delay 5 --p 15.0 --dmin 50 --dmax 100Or with explicit activation:
source .venv/bin/activate # After uv sync
python3 relay_controller.py --lower 45 --upper 50 --delay 5
# or
python3 proportional_controller.py --temp 45 --delay 5 --p 15.0 --dmin 50 --dmax 100Also you can use the launch script run.sh, making it executable first
chmod +x run.shThe script arguments can be changed as desired.
The connection diagram is shown below. The fan is controlled by a transistor switch based on the IRLML2402 N-channel MOSFET.
In my case, I used a 12-volt fan instead of a 5-volt one, because experiments showed that the 5-volt fan was noticeably noisy even at minimum speed. The fan cools the radiator down to an acceptable 45 °C pretty quickly. The photo also shows how the fan and transistor switch are connected to the board.
You can set up automatic script launch at system startup.
Open the /etc/rc.local file in editor:
sudo nano /etc/rc.localAdd to the end of file this line:
/home/pi/raspberry_pi_fan_control/run.sh &Press Ctrl+O → Enter → Ctrl+X to save and exit. With these few easy steps, you now have automatic fan control.
If you want to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them.
- Push to your fork and create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.

