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.
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.
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.
1) Download the attachement, or just copy/paste it from pastebin here and name it fan.py
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
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:
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.