Introduction: Automated Cooling Fan for Pi

Description
A simple design to control a mini 5v fan with python, without the need of a breadboard, transistors etc. All you need are a few cables and a 1 channel relay. I had a 2 channel relay which i recommend, since its almost the same price plus you get an extra controller. A script executing every hour will check the pi's temperature and turn on/off a fan until the desired temperature is reached.

Introduction
Since summer is upon us my secondary pi is running a bit too hot for my taste so i wanted to cool it down a bit every now and then. Of course i could have a fan blowing at it 24/7 but a) that would require a breadboard, and some resistors etc because i cant just plug it in on the GPIO pins directly b) writing some code that interacts with the 'outside' world is more fun :) and c) its really cheap...all you need are some cables and relay.

Enjoy, and feel free to comment !

Step 1: Materials Needed

- crontab / python installed on your pi
- A raspberry pi with a case
- 5v mini fan (link)
- 2 channel relay (link)
- Couple of female to female cables (link). I only had 1, so i used an old DVD audio cable for the PC and that extra f2f cable for the relay controller pin.

Note:
The hole on top of my raspberry case was a bit smaller than usual, so i had to use a drill to make it a bit wider. Maybe you should get a case with a 5v fan already attached to it, if you are not sure about the size of it on your case.

Step 2: The Circuit - Assembly

As you can see from the image above i am using pins 2,6,12, which are all the same for any type of raspberry pi you might have, so you don't have to worry about the fact that i am using a Pi Model B rev2.

Plug in the cables the same way i am doing it.
- The 5v(pin2) goes to VCC
- The GND(pin6) to the GND
- The GPIO18(pin2) goes to the IN1
Make sure the jumper on your relay is set to : JD-VCC VCC.

Now to the relay...My setup is a bit weird, i know. I didnt want to use an external power source to start the fan, so i kinda of attached it to the pi as well. You will have to make a small cut to both the 5v and GND cable coming from the pi to attach the fan cables the same way i am doing it. You can use a soldering iron and some tape like i did.
I dont think there is a reason to worry about bricking your pi, because only the ground cable is directly connected to the pi. If you see carefully at the image, the relay switch is connected to the power cable, meaning when the fan is off, the direct connection with the pi is 'open' as well. So any electrical current the fan might produce when it goes off, will have no way of returning back to the pi.

The reason why i am using those gates on the relay, is because the relay has a tiny led on top of it. I wanted to see the red light when the fan in ON, so i know when the pi is cooling down.
If you want, you could use the other gates so that the reverse thing happens.
But you would probably need to reverse the GPIO commands in the python script (in functions fanON fan OFF shown in the next step) to make it work...You'll see what i mean when we get to it.

Step 3: The Script

Now don't freak out about scripting... Last week i didn't know python and yet i managed to write this script by reading and testing various examples out there. I know other programming languages though so dont worry, i am not gonna blow up your pi.
If i used (which i probably did) some functions that someone else wrote, i apologize for no credits given, but i've tweaked this script so much, its basically new.

Instructions

1) Download the attachement, or just copy/paste it from pastebin here

2) Place the fan.py script in /home/pi/ folder

3) Execute: sudo chmod +x /home/pi/fan.py and sudo chown pi:pi /home/pi/fan.py

4) Assuming you have crontab installed on your pi execute: crontab -e

5) At the bottom copy / paste: 5 * * * * sudo python /home/pi/fan.py and save the file. (Ctrl+X and Y)

This crontab will execute the script every 1h5m.
The script will work as a standalone script as well...meaning besides the automated crontab action you can manually turn on/off the fan yourself. You do this using parameters like so:

  • sudo python /home/pi/fan.py on or
  • sudo python /home/pi/fan.py off

I also wrote a failsafe function in case you press Ctrl+C while the script is running. If you do, the fan will shut down before the script exits.


So, how this works ?

Every hour the script executes and checks the pi's temperature.
If the temperature is above an X value the fan will turn on and stay on until it cools down to a Y value. When it does, the script will exit. If for some reason it never reaches that low temperature and an hour passes, the next time the script executes it will 'see' that the fan is still on and the second script will quit...so use realistic temperature values if you want the fan to actually turn on/off.
If not, just set some ridiculous values (like Y = 0 degrees Celsius) so that the fan will always stay on.)
If your pi is operating between those 2 values (X,Y), it means that its operating under 'accepted' temperatures so the script will just exit when it checks the temperature every hour.

Explaining some values

At the top of the script there are some global variables. There are the variables you have to change.

# Identify which pin controls the relay
FAN_PIN = 18 # the yellow box ex: GPIO18
# Temperature check. Start fan if temp > 49C
FAN_START = 49
# Temperature check. Shut down under 28C
FAN_END = 28

Its pretty obvious what to do here. If you used GPIO18 like i did, then leave 18 here, otherwise change that value to the gpio you used.
FAN_START and FAN_END are the high/low temperatures you want use. You can even put float numbers there if you want, like 49.2


Note:

Remember the way i set up my relay ? If you see functions fanON and fanOFF you'll notice that i am setting the gpio output to False to turn the fan on and True to turn it off. If you chose another gate configuration on the relay, you'll probably need to reverse those values.

Step 4: Final Notes

I apologize for the detailed instructions. I wanted to cover all types of users.
There is probably a better way to do all this but i did the best i could with the materials in hand and with my noobness in circuitry and python.

A small bonus code

If you want a fast way to check the temperature of your pi, create a file called temp in /usr/local/bin folder and then copy paste this script inside:

http://pastebin.com/rUYqGjV5

execute: chmod +x /usr/local/bin/temp to make it executable.

Then just type temp from whichever folder you are to see your pi's temperature.

Comments

author
FranciscoP94 (author)2017-03-08

@Sanbird - I'm running your script but I'm having an issue, the script is turning ON the fan at the specific temp but for some reason its not turning it off when the shutdown temp is met. Any idea?

author
Sandbird (author)FranciscoP942017-03-09

Have you tried debugging it by executing the commands manually ? Open up a console and try these commands (just change the path location to the location you put the script at) :
sudo python /home/pi/fan.py on
or
sudo python /home/pi/fan.py off

If it goes off, then i think you should raise a bit the shutdown temperature in the script.
I kinda had the same problem one time. Some nights the fan was always on, because the shutdown temp could not be reached since that night was a hotter night. Raising the FAN_END by 2-3 degrees fixed my prob.

author
ramaputradwip (author)2016-12-15

Hey, i cant download temp.py, i guess the link is not work or invalid. Please update the link soon.

Thx.

author
Sandbird (author)ramaputradwip2016-12-15

Fixed, i put the script in pastebin. Thanks

author
ThiagoS69 (author)2016-09-10

Hey, I follow your instructions. Worked well, except the script. Actually, if I start it by command line it work, but if it is executed by cron at, lets say, reboot, the fan will start no matter the temp, also, will never stop unless I reboot the Pi. Any thoughts?

author
Sandbird (author)ThiagoS692016-09-10

hmm, any errors when you start the script manually ?
Also are you running the cron with sudo ? I think it needs root permissions to run properly.
Also, to stop the fan, did you try the: 'sudo python /home/pi/fan.py off'
That should stop the fan, if not then probably you didnt select the proper slots on the relay. There are 3 slots. If i remember correctly depending on which slots you used, the ON functions in the script would actually do the opposite (turn the fan off) if you used the wrong setting. And then the OFF functions would turn it on instead...Basically the logic gate on the fan is reversed, if you put the cables in the wrong slots.
One last thing i could think of....when you ssh to your pi (the normal way) and type: vcgencmd measure_temp
Does it give the temperature in Celsius? (ex: temp=48.7'C)
If not then you need to change something in the code.
In the function 'get_temp_from_system' , i am 'reading' that value, but i am expecting to find the 'C part. If yours gives fahrenheit, then you need to change that 'C\n to 'F\n.
And probably recalculate the temperature values, for switching on/off the fan.

author
en_rov (author)2015-06-10

I think the temperature should be checked more often, like every 30 or 15 minutes, and that a transistor could have worked fine for powering the fan. Apart from that, great 'ible!

author
Sandbird (author)en_rov2015-06-11

During the 'hot days', it hardly stays off :) but yeah depending where you live you can tweak the crontab accordingly.
I have ordered a bunch of stuff from the net now, i didnt even have a breadboard let alone a transistor when i made this :)

author
voblak (author)2015-06-03

Don't you think its a bit overkill with relay? It would be much more simple to use logic level N mosfet or NPN

author
Sandbird (author)voblak2015-06-03

yeah i know...when i first realized that i need to do something about the heat....i checked on the internet to see what i need exactly to make this happen, and ordering the parts online would take 2-3 weeks until they arrive...so i had to figure out a way to do this 'now' since for example today we had 29C here :P.
I had the relay and thought maybe i can do this with just 1 relay and 3 cables.. :)

author
braytonlarson (author)Sandbird2015-06-03

An NPN Transitor (2N3904 is what I use) would be perfect for this. It can handle up to 40V so it is plenty. Much smaller and more cost effective.

author
vinz3nt (author)2015-06-03

Very nice solution but be aware that the RAM is on top of the Qualcomm CPU so in fact you are cooling the RAM, not the CPU

author
Sandbird (author)vinz3nt2015-06-03

yeah i know, but the 'air' inside the box gets cooler as well.
It can go down to 22-25 C in 2 minutes (on a normal day..if its not too hot outside).

author
tomatoskins (author)2015-06-03

This is awesome! Thanks for sharing and welcome to the community!

author
Sandbird (author)tomatoskins2015-06-03

Thank you so much :) Its great to be here!