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.

Scope

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 “run-fan.py” in a custom location (mine is in ~/Development/run-fan.py) and copy-paste the following code:

#!/usr/bin/env python3
# Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com>
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.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
return()
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():
setPin(True)
return()
def fanOFF():
setPin(False)
return()
def getTEMP():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
fanON()
else:
fanOFF()
return()
def setPin(mode): # A little redundant function but useful if you want to add logging
GPIO.output(pin, mode)
return()
try:
setup()
while True:
getTEMP()
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:

python run-fan.py

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:

#!/bin/sh
### BEGIN INIT INFO
# 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.
### END INIT INFO
python /home/pi/Development/run-fan.py

…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.

More by Edoardo Paolo Scalafiotti

Topics of interest

More Related Stories