Raspberry Pi OS Fan Control

By Jeremy S. Cook

Freelance Tech Journalist / Technical Writer, Engineering Consultant

Jeremy Cook Consulting

March 15, 2021

Blog

Raspberry Pi OS Fan Control

As you probably know, Raspberry Pi single-board computers, especially Pi 4 models, can get quite hot. While passive cooling options are often good enough to avoid overheating and thermal throttling, at some point you’ll need to think about using a cooling fan. The Raspberry Pi’s GPIO pins don’t supply enough current to power even a small fan, but there are several ways to power and control a fan with the Pi.

To run a simple 2-wire fan continuously, connect it to the 5V and ground pins, or you can instead hook it up to 3.3V for lower speed and noise. Either is slightly wasteful power-wise, and potentially annoying.

Raspberry Pi OS has functionality built-in to switch the fan on and off based on temperature. 2-wire fans, or more advanced coolers like the ICE Tower, will need a transistor for control, but PWM fans, such as this 5V Noctua NF-A8, have the necessary components built-in. This article covers simple on/off fan control, and variable speed fan PWM fan control is outlined in this separate post.

Raspberry Pi Fan Control Hardware Setup

Image Credit: Jeremy Cook

As outlined in the video below, if you’re using a PWM fan, all you have to do for control is connect its power lead to one of the Pi’s 5V pins, ground to ground, and the PWM lead to the appropriate GPIO pin. For simpler fans with only a power and ground input, you’ll need to add a transistor to control current from the 5V source. The EZ Fan2 board, that I designed, works for this purpose as a plug-and-play transistor unit, including a flyback diode and current-limiting resistor.

Raspberry Pi Fan Control Software Setup

From the Raspberry Pi OS GUI, click on the Raspberry button, then select Preferences > Raspberry Pi Configuration. Click on the Performance tab, then select the Fan Enable radio button. Below this you can select the GPIO pin that you’d like to use, as well as the temperature at which it should come on. Restart and you’re good to go!

Image Credit: Screencap

Alternatively, you can set things up via the terminal. Input sudo raspi-config. Select Performance Options > Fan, and follow the prompts to enable temperature control. Select the GPIO pin, and select at which temperature it should come on (between 60 and 120ºC, which is the same on the GUI interface). It then asks you if you want to reboot, and once that is done, the fan is temperature activated.

If you’d like to test things out, you could go “brute force” with a hairdryer or similar, but a better and more convenient way would be to perform a processor stress test. Core Electronics has a nice guide on how to do this, which I’ve used to verify my setup.

Turn Fan on Below 60 Degrees

Image Credit: Screencap

If you’d like to set the on temperature below 60ºC, this is possible too, if slightly less convenient. First enable fan control as outlined above. Open up the config.txt file (sudo nano /boot/config.txt in the terminal) and find the line: dtoverlay=gpio-fan,gpiopin=18,temp=60000 noting that the two numbers may be different depending on your configuration. Change the temp value to your preference (noting it’s the on temperature x 1000), save, reboot, and voila the fan comes on at 45ºC or wherever you like.

Note that the fan doesn’t switch off until its 10ºC below your set point, so if it’s set too low it may come on and never go off again. Also note that you can simply add the fan control line in manually, though the raspi-config option formats everything for you nicely.

PWM Variable Speed Control

Turning the fan on and off is good enough to cool your Pi in most instances. However it is also possible to implement a script to turn the fan on slowly when the temperature is only moderately high, and ramp it up to full blast when needed. Doing so is sightly more complicated, but still approachable, laid out in the post linked earlier.

That Was Easy!

Simple Raspberry Pi on/off temperature fan control is easy to implement, especially if using an appropriate 5V PWM fan or an EZ Fan2 device. No installing or writing scripts required, just a simple set of options, and potentially modification of a text file. This built-in functionality on the Pi was only added in late 2020, but everything, of course, has to start somewhere.

Alternatively, I came up with a method to do this in mid-2020 using Bash scripting, before it was available in the Raspberry Pi OS. While largely unnecessary today, you may find the code, comments, and links on that repository to be of use. If nothing else, it’s neat to see how powerful Bash can be on the Raspberry Pi.

Special thanks to Zackary Delgado for pointing out the cmdline.txt modification trick!

Jeremy 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, or see his electromechanical exploits on the Jeremy Cook YouTube Channel!

Jeremy 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, or see his electromechanical exploits on the Jeremy S. Cook YouTube Channel!

More from Jeremy