# Pi Fan Controller

A tiny script allowing temperature controlled Raspberry Pi cooling.

This script is loosely based on work by Edoardo Paolo Scalafiotti found here1, though this is a complete rewrite using the gpiozero library. It also makes use of two temperature variables (a minimum and a maximum), allowing the user to define a temperature range to oscillate within, avoiding rapid on/off switching when trying to stabilize around a single value.

By default, the fan will turn on once the CPU reaches 60°C and will turn off when the temperature returns to 50°C.

Most small fans (like this one) are designed to be run off the Pi’s 5V GPIO pins, however, there is no way to control these pins directly. Instead, we can use a transistor as a switch to turn on and off our fan. I personally used a NPN BJT with its base pin wired to BCM pin 14 (physical pin 8) and then used the ground and 5V power pins right beside it. Edoardo describes something similar in is his write up as well, though I find pin 14 has much better positioning, plus has the benefit of being pulled low by default, meaning your fan will not be spinning when the Pi is shutdown but still receiving power.

The wiring diagram

To install the script, download or clone it from GitHub. Note that you may need to install the Python 3 gpiozero library, especially if you are running the lite version of Raspbian/Raspberry Pi OS. When running locally, line 52 can be uncommented to help with debugging. User variables such as the temperature ranges and the check frequency are all easily available towards the top of the document.

To have the script run on boot, add python3 /path/to/pifancontroller.py & to your rc.local file, making sure to include the ampersand to fork the process as the script runs in an infinite loop.

1. Mirror available here. ↩︎