How to control a fan to cool the CPU of your RaspBerryPi

Following this tutorial you’ll be able to assemble a fan using a transistor to draw 5V current into the fan’s motor. Then you’ll write a Python script to activate the fan when the CPU reaches a certain temperature. Finally, you’ll be able to run the script automatically at boot time.


I use the latest RaspBerryPi 3 for a lot of things. Plex is one one of them. The RPI3 is very fast in indexing all my media files but it struggles in transcoding .mkv files: it works well but the movie has frequent interruptions, which is not a great user experience. Fortunately, Plex has the built-in converter that enables free .mp4 to browser streaming. To do that in the background, the CPU reaches 380% and the temperature increases greatly over 80C. So I thought: time to add some cooling.

Hardware setup

First things first: I have bought this fan on Amazon. All the fans that I’ve seen are DC 5V with a maximum of 200mA: therefore the active power is 1W. However, the GPIO pins on the pi can only deliver 3.3V with a maximum of 16mA each: we cannot — and should not — feed the motor with the pin’s power. Huston, we have a problem (?).

What we need is some sort of relay that can be activated by a standard pin to draw current at full throttle to our fan. Meet the transistor. More specifically, an NPN Transistor (S8050). The final setup is the following (the fan is represented as a DC motor, this is NOT a real DC motor):

The Breadboard
The Schematic
Do not, EVER, connect a real DC motor to your board: you’ll definitely damage it.
My finalhacky solution

Once everything is setup, it’s time for the software part.

Software setup

Create a file called “” in a custom location (mine is in ~/Development/ and copy-paste the following code:

#!/usr/bin/env python3
# Author: Edoardo Paolo Scalafiotti <>
import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
pin = 18 # The pin ID, edit here to change it
maxTMP = 40 # The maximum temperature in Celsius after which we trigger the fan
def setup():
GPIO.setup(pin, GPIO.OUT)
def getCPUtemperature():
res = os.popen(‘vcgencmd measure_temp’).readline()
temp =(res.replace(“temp=”,””).replace(“’C\n”,””))
#print(“temp is {0}”.format(temp)) #Uncomment here for testing
return temp
def fanON():
def fanOFF():
def getTEMP():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
def setPin(mode): # A little redundant function but useful if you want to add logging
GPIO.output(pin, mode)
while True:
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program

Ok, now let’s test it from the console:


If everything was setup correctly you should see your fan spinning and stopping accordingly to the CPU temp. Cool.

One more thing…

Obviously we don’t want to manually run the program each time we reboot the pi, won’t we? We want everything to be loaded at startup, right? Then let’s create a boot script in /etc/init.d simply called “fan”:

nano /etc/init.d/fan

…and copy-paste in it the following code:

# Provides: dnscheck
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Short-Description: Start fan script at boot time
# Description: Enable service provided by daemon.
python /home/pi/Development/

…then create the symbolic links by running:

update-rc.d /etc/init.d/dnscheck defaults

…and next time you’ll reboot your pi, your fan script will run automatically.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!
Like what you read? Give Edo Scalafiotti a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.