How to install a fan onto a Raspberry Pi and make it run dynamically
In my previous article I wrote about what a Raspberry Pi was and why you should consider buying one. One of the main gripes with the current fourth generation Raspberry Pi is that it does become quite hot under heavy load. The fourth generation Raspberry Pi does “throttle” the CPU which means that it slows the processing down so to prevent the device from over-heating. This is good but you really want the Pi to perform well and be cooled accordingly. So what you really need a good cooling solution for your Raspberry Pi. Add to this the fact that the Raspberry Pi does not ship with a case so we could do with this too.
There are two cooling solutions for the Raspberry Pi; active cooling and passive cooling. Active cooling involves the use of a physical rotating fan to cool the central processing unit (CPU) whereas passive cooling involves the use of heat sinks; pieces of jagged metal to dissipate the heat. Active cooling is far more effective than passive cooling at reducing the temperature of the CPU which was the reason I opted for a fan to cool my Raspberry Pi. (If you don’t believe me check out YouTuber Chris Barnett’s video of comparing cooling solutions on a Raspberry Pi 4).
So a case with a mounted fan would be ideal, hence I purchased the ‘GeeekPi Raspberry Pi 4 Acrylic Case, Raspberry Pi 4 Case with Fan(Blue Light) and Raspberry Pi 4 Heatsinks For Raspberry Pi 4 Model B, Compatible with Raspberry Pi ICE Tower Fan(Black and Clear)’ from Amazon for £6.49 ($8.96).
When it arrived, I connected it up to my Pi and voila, it started up and the fan spun up and did its job cooling the Pi constantly. But I wanted to take things a little further and thought about dynamically controlling the fan according to the CPU temperature. After a bit of Googling I came across the following site: https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python. “Bees knees” I thought until I got to part 3 — build the test circuit. This was where hit an impasse. The problem I faced was not having a breadboard and circuit also I’m no electrical engineer so knowing how to make this was a little too complex for my liking.
After some more Googling I came across this site: https://www.tindie.com/products/jeremycook/ez-fan2-tiny-raspberry-pi-fan-controller/ An engineer by the name of Jeremy Cook has created a minute printed circuit board (PCB) that sits between the fan connectors and the GPIO connectors of the Raspberry Pi. So I purchase it, with the following add-ons: Clear Heat Shrink, Female ~8cm connector wires which came to $9.95 plus shipping to the UK which cost $15 (more than the item, but I thought it would be worth it), which brings the total cost to $23.99 USD. (Note if you are in the UK check out the Fan Controller for Raspberry Pi sold on thePiHut for only £4.20)
Check out Jeremy Cook’s YouTube video on how he made it and installed it; https://www.youtube.com/watch?v=AdjU_AVcZTA. At 5:59 Jeremy shows a GPIO schematic of how to connect the wires to the EZ Fan 2.
Here’s my installation of the EZ Fan 2 connector:
Notice, green wire is on the top, yellow in the middle and blue at the bottom.
GPIO ports 2, 6 and 8 were used as per schematic:
Blue wire — port 2; 5V power
Connector spacer — port 4; 5V power
Green wire — port 6; Ground
Yellow wire — port 8; GPIO 14 (TXD)
Now for the shell script to make it work dynamically
Access the following link: https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python and scroll down to part 4 — Use the install script and follow the steps verbatim (Pre-requisites: give your Raspberry Pi a static IP address, connect it to your router either via ethernet (preferred) or WiFi. You’ll also need to enable secure shell (SSH) on the Raspberry Pi and SSH into it).
That’s it! All done. Since we’ve used the installer script in step 4 of https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python it has already been registered to run on boot.
Reboot the Raspberry Pi with the following command:
sudo reboot
This will reboot your Pi and your fan should be running dynamically.
Time to test it…
- Access the check_fan directory and create a file called measure_temp.py in nano
- Copy/paste this into the file:
#!/bin/bashwhile [ true ]domyTemp=`/opt/vc/bin/vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'`myDate=`date '+%Y/%m/%d %H:%M:%S'`echo $myTemp $myDatesleep 3done
3. Save the file and exit using the ctrl+z keys
4. Give the file executable attribute by typing:
chmod +x measure_temp.py
5. The run above script:
./measure_temp.py
The moment the temperature rises to above 51 degrees the fan is activated and the temperature is bought down to 40 degrees after which the fan switches off.
So there you have an inexpensive fan and a script to dynamically control the fan of your Raspberry Pi. Please leave a comment if you have any questions, concerns or comments. If you’ve managed to replicate this and it’s worked for you please leave a clap… :-)