Simple python daemon to control a fan via GPIO pins and a NPN transistor based on the CPU temperature.
- Guide I followed
- What I used:
- NPN transistor (PN 2222A 6E) with 220 Ohm resistor at the base
- Raspberry Pi 4B with 8GB RAM
- 64-Bit Ubuntu Server 20.04 LTS
- Cooler used: GeeekPi Low-Profile Cooler
- Just run
docker-compose up -d
- Python version used is 3.8
- Dependencies can be installed with:
apt install python3-rpi.gpio
- To setup the daemon I recommend to:
- Create a new user:
sudo useradd -m fancontrol
- Lock the user, so one can not login with it:
sudo passwd -l fancontrol
- Allow new user to access gpio pins by adding to a group:
sudo gpasswd -a fancontrol dialout
- Checkout the repository in new users home:
cd /home/fancontrol && sudo -u fancontrol git clone https://github.com/danielgolf/rpi-fan-control.git
- Copy the systemd unit file:
sudo cp /home/fancontrol/rpi-fan-control/rpi_fan_control.service /etc/systemd/system
- Reload systemd:
sudo systemctl daemon-reload
- Activate and run the daemon/service:
sudo systemctl enable --now rpi_fan_control.service
- Create a new user:
- The
/home/fancontrol/rpi-fan-control/src/cputemp.py
script can be copied or symlinked to$HOME/.local/bin/cputemp
In the main script rpi_fan_control.py
are four variables for configuration
GPIO_PIN
: Number of GPIO pin to control the fanSLEEP_TIME
: How long to sleep between each cpu temp checkCPU_TEMP_FAN_ON
: Temperatur on which the fan is turned onCPU_TEMP_FAN_OFF
: Temperatur on which the fan is turned off