Temperature Controlled Raspberry Pi Fan (Keep Your Pi Within a Fixed Temperature Limits)

Introduction: Temperature Controlled Raspberry Pi Fan (Keep Your Pi Within a Fixed Temperature Limits)

About: I am a Nuclear scientist by profession & Tech Enthusiastic by heart !

Hello | Hola | Bonjour | Zdravstvuyte | Other Hello's i dont know :P

Welcome all to the world of Raspberry pi.

Well after my grand success of earlier instructable of how to setup pi for dummies, here i am with you once again to cool your pi ! What ? you haven't watched my earlier instructable. don't panic find the link below :)


Well if you have watched my earlier tutorial, you might have understood how stubborn pi is ! So, one fine day he told me that "I won't function unless you cool me ! after all everyone need a bit comfort :P"

So finally we agreed on terms of adding a temperature controlled fan and heat sink. This is a step by step guide which will help you set up your Fan & Heat Sink in a very systematic way. A tutorial is also available on Youtube. I will not mind if you watch it :-) but if you do press the subscribe button please ! :-P

Step 1: Heat Sink and Fan

Well, most of you would think that this would be the end just paste the heat sink and attach the fan directly to the GPIO !

A big NOOOOOooooo!!!

My pi wanted something more.. he complained that the fan was running all the time at that gave him a very bad headache !

He demanded a fan that would :-

1. Run only when he is working

2. Help maintain his temperature limits within a fixed interval of time !

Step 2: Gather Your Supplies ! Sacrifice Needs to Be Done..

So, my pi selected this case ( said it was beautiful :-) )

For triggering the fan, you also require, one S8050 (PNP) transistor & a few connectors !

Step 3: It's Time for the Hardware Part !

Connect your fan and transistor as shown in the schematic.

Here i have connected the base of transistor with GPIO 21.

Step 4: Installing the Script - "run-fan.py"

Just like all great things !

We need a small script that would run continuously on pi and control the fan.

Just execute these commands on the terminal !

<p>sudo nano run-fan.py</p>

Go ahead and paste the following script in run-fan.py

#!/usr/bin/env python

import os import time import RPi.GPIO as GPIO

GPIO.setwarnings(False) pin = 21 # The pin ID, edit here to change it maxTMP = 40 # The high temperature in Celsius which will trigger the fan on minTMP = 35 # The low temperature in Celsius which will trigger fan off

GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.setwarnings(False) def getCPUtemperature(): res = os.popen('vcgencmd measure_temp').readline() temp =(res.replace("temp=","").replace("'C\n","")) #print(temp) return temp

def getTEMP(): CPU_temp = float(getCPUtemperature()) if CPU_temp>maxTMP: GPIO.output(pin, True) elif CPU_temp<minTMP:

GPIO.output(pin, False)


try: while True: getTEMP() time.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

Do modify the pinID ie. your GPIO no, upper & lower temperature limits as per the GPIO pin connected with the transistor base. Save and exit by pressing Cntrl X + Y;

Next, make this executable by running following commands

<p>sudo mv run-fan.py /usr/local/bin/</p><p>sudo chmod +x /usr/local/bin/run-fan.py</p>

Step 5: Installing the Script - "run-fan.sh"

Go ahead and create a new file run-fan.sh

sudo nano run-fan.sh

Next, paste the following code in the editor

#! /bin/sh

### BEGIN INIT INFO # Provides: run-fan.py # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 ### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system case "$1" in start) echo "Starting run-fan.py" /usr/local/bin/run-fan.py & ;; stop) echo "Stopping run-fan.py" pkill -f /usr/local/bin/run-fan.py ;; *) echo "Usage: /etc/init.d/run-fan.sh {start|stop}" exit 1 ;; esac

exit 0

Execute these commands to get the ball rolling !

sudo mv run-fan.sh /etc/init.d/

sudo chmod +x /etc/init.d/run-fan.sh

Now register the script to run on boot

sudo update-rc.d run-fan.sh defaults

Step 6: Yeah ! I Know the Fan Is Still Dead !

But, but pi told me that the script is not yet started.

Oh ! get is working by executing the following command

sudo /etc/init.d/run-fan.sh start

The fan would has started and now you can jump from your chairs !

You really did great if your fan has started !

If not, you are the one who might have messed it up somewhere ! Re-watch the steps carefully :P

By the way my pi is happy with me now ! I gave him all the comfort he needs and now i make him work 24 X 7 Haha !

You all can clap now !

No wait don't close the window ! comment if you have any doubt !

Watch the complete tutorial on this link and do subscribe to Technoww so that i can start making millions :)

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Big and Small Contest

      Big and Small Contest
    • For the Home Contest

      For the Home Contest



    Question 1 year ago on Introduction

    great instructable for cooling the pi, I am designing a similar but different project. (using the heat from pi and blowing it into an allsky camera enclosure to prevent dew on the lens/dome. I am band new to python and have been watching many video as well as purchased 2 books. I am trying to understand your script to see what works for me and what i would need to change. do you have any notes on the commands you used and why/what they do?