Raspberry Pi Cooling Fan Control with Bash Scripting

June 30, 2020 Jeremy S. Cook, Freelance Tech Writer

In a previous post, I did a very brief introduction to the world of Bash scripting in the context of Raspberry Pi single-board computers. It’s an amazingly powerful tool, capable of administrative tasks like batch file renaming, making decisions, and more. While this scripting interface is available for any Linux system, the Raspberry Pi’s GPIO pins make it even more powerful, allowing it to control physical devices, like an LED directly, or even motors and other higher current devices indirectly via a transistor.

As it just so happens, the Raspberry Pi doesn’t come with any sort of active or even passive, cooling solution, and it’s pretty common to simply hook up a fan to run at all times to its 5V power supply. This seems to work fine, but when I noticed the Pi that runs my 3D printer (in a hot Florida garage) was overheating, running it all the time seemed a little silly. After all, power is applied to the Pi constantly, but it’s actually used on a very intermittent basis when I’m printing something.

(Script and PCB design available on GitHub)

To make the fan function run intermittently as well, I used the bash script shown above, set up to run via cron once every minute. When triggered, it uses vcgencmd measure_temp to get the temperature of the Raspberry Pi’s processor. It then compares this temperature using an if/then/else statement to either turn the fan on with the line gpio -g write 3 1 or off with gpio -g write 3 0. It’s not the most responsive solution, able to turn on or off only once per minute, but it’s much simpler than anything else I’ve seen so far program-wise.

To directly power the fan itself I used a 2N2222 NPN transistor through one of the Pi’s 5v pins. Here GPIO pin 3 is fed to the transistor’s base, allowing current to flow through the fan, then through the transistor’s collector and emitter, and finally to ground. A resistor is used between the GPIO and base to limit the current output. I also added a flyback diode to the design to account for voltage spikes when the fan is switched off, though it’s optional, and isn’t actually used in my current setup.

A Simple PCB

(1K is shown, but 500-ohm resistor was actually used)

If using a cooling fan, along with massive a radiator assembly, wasn’t overkill enough (a small passive aluminum cooler would have probably been fine) I decided to have a PCB made to house the transistor, resistor, and diode. The design was made in KiCad, and manufactured by OSH Park. It looks good, and works well, and the PCBs were much smaller than I even anticipated. At less than a dollar per batch of 3 in this particular case, I would definitely recommend this sort of thing if you can deal with waiting a week or two to try it out.

The video below shows everything was implemented, including  commentary on the software setup. The video goes over how the transistor/fan design can be implemented with components and heat shrink, on a PCB, or on a breadboard. It’s a fun project, and as an extra bonus, the fan assembly has an RGB light on it that tells you in colorful fashion when it is actually running.

Part of the power of Bash, and using Raspberry Pi’s shell in general, is that you can log on remotely and do whatever you need to via SSH. To help, Iwrote a guide to some of the finer points of doing so in this post. This could be quite helpful if you’re relatively new in your Pi journey, or if there’s perhaps a technique or two that you’ve missed so far!

About the Author

Jeremy S. Cook is a freelance tech journalist and engineering consultant with over 10 years of factory automation experience. An avid maker and experimenter, you can follow him on Twitter [https://twitter.com/JeremySCook], or see his electromechanical exploits on the Jeremy S. Cook YouTube Channel! [https://www.youtube.com/c/jeremyscook]

Previous Article
Securing the Automobile in a Connected World
Securing the Automobile in a Connected World

Who would have thought that someone would want to hack an automobile? About five years ago, someone hacked ...

Next Article
How IoT is Transforming the Healthcare Industry
How IoT is Transforming the Healthcare Industry

The adoption of the IoT has revolutionized the way the healthcare industry works now, as it has great poten...