Fan control for better Overclocking of the Raspberry Pi

Who has his Raspberry Pi running as a Server, etc., will quickly realize that the little giant can become very hot. To a certain extent, one can surely ignore this, but if the Pi is running day and night, you should pay attention to the temperature (especially in a housing). Since I also had this problem, I took it as a project to keep the case and the CPU cool.


I took a 5V fan and a DS18B20 temperature Sensor. The Transistor installed is a BC547. If you do not have a temperature sensor or you do not want to measure it, you can still use the Script with a small adjustment.


As Resistors, I have taken 4.7k Ohms each. The output pin for the Transistor is pin 23, although any other GPIO pin that does not have an assigned function can be taken.
Important: The temperature Sensor is connected to 3.3V, whereas the Cooler is connected to 5V.


In order to make the work as easy as possible, I have already prefabricated a script that you only need to adapt.


and unpack:


Well, then we’ll edit the script:

sudo nano

Here, lines 19 to 23 are important and possibly adaptable.

Now, IMPULS_PIN simply indicates the GPIO pin, which is used to drive the transistor.

SLEEP_TIME indicates all how many seconds the temperature should be checked. I do not recommend a too small number to give the Pi more calculation time for other applications.

MAX_CPU_TEMP and MAX_SENSOR_TEMP indicate the respective temperatures at which the fan should start to rotate. It does not matter what is achieved first; as soon as one has been reached it starts to turn. If the cooler is always turning, set MAX_CPU_TEMP = 0.

Now to the sensor ID: How to get this out, I have already shown here. If you do not have a sensor, just leave it that way. For me it is e.g. the ID 10-000802b4ba0e:

So far so good. Basically, we are already done. With

sudo python

the Script can already be started. If you want to have the values displayed on every check, you can delete the # in lines 59 and 60 of the Script.

Automatic starting

If you want to start the Script automatically, you can take a look at this Tutorial. Optimally you move the script to /usr/local/bin/

sudo cp /usr/local/bin/

Then you just have to adjust the names, as in line 8:



Now that we have made sure that the Pi is always cool enough, we can also overclock:

sudo raspi-config

Under point 7 Overclock you can set the clock rate. For example, I have set Medium or High as needed. You can adjust this depending of the Application (in the case of computationally intensive usage, a higher clock rate makes more sense and brings speed at the same time).

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!