Temperature Controlled Raspberry Pi Fan (Keep Your Pi Within a Fixed Temperature Limits)
by TechNoww in Circuits > Raspberry Pi
2570 Views, 4 Favorites, 0 Comments
Temperature Controlled Raspberry Pi Fan (Keep Your Pi Within a Fixed Temperature Limits)
![How to reduce the temperature of pi - [ Heat Sinks ] & [ Fan ]](/proxy/?url=https://content.instructables.com/FL1/EYIY/K9K8NJOZ/FL1EYIYK9K8NJOZ.jpg&filename=How to reduce the temperature of pi - [ Heat Sinks ] & [ Fan ])
.png)
.png)
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 :)
https://www.instructables.com/id/Setting-Up-Raspbe...
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
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 !
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 !
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.
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)
return()
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>
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
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 :)