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

Written by edoardo849 | Published 2016/06/11
Tech Story Tags: raspberry-pi | diy | tech

TLDRvia the TL;DR App

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 [email protected]

import osfrom time import sleepimport signalimport sysimport RPi.GPIO as GPIO

pin = 18 # The pin ID, edit here to change itmaxTMP = 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 testingreturn tempdef 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 loggingGPIO.output(pin, mode)return()

try:setup()while True:getTEMP()sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you wantexcept KeyboardInterrupt: # trap a CTRL+C keyboard interruptGPIO.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.


Published by HackerNoon on 2016/06/11