In another blog, I have told how I used Raspberry Pi 3 as a media server using Plex. For that setup, I used a DC fan to cool down the Raspberry Pi, to maintain temperatures to allow overclocking. On Raspberry Pi, the system clock will be throttled if the temperature on ARM chip goes above 85 degrees Celsius. This is a measure to prevent permanent damage to the ARM chip. However, doing so will slow down the operation of the system and reduce throughput.
For a media server application, during playback, I observed this when I used the Raspberry Pi without any fan. Even with aluminum heat sinks sold along with the case, the temperatures were out of limits. In fact, I found in due course that, the heat sinks are completely useless. However, a simple 60 mm 5 V DC fan helped to maintain the temperatures. As fan was needed using only heavy usage, I decided to use a simple circuit and control software to control the fan using GPIO. The fan would be turned on only when there is heavy usage, thus reducing fan power consumption and noise. Also extending fan life by keeping it off when not needed.
The circuit is pretty simple. The power to the fan is cut off using an N-Channel MOSFET. The fan is chosen to be 5 V so that it can be directly powered using USB. Raspberry Pi has GPIO breakout, which is used to interface the fan.


Image reproduced from Raspberry Pi Foundation. https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/README.md
Above is the schematic, and Raspberry Pi 3 pinout. R1 is only required to limit the current through GPIO. Any N-Channel MOSFET with Drain-Source voltage (VDS) greater than 10 V can be used. Gate to Source threshold voltage Vgs(th) should be 2.5 V maximum and 1 V minimum for a guaranteed operation. Further details on pinout can be found on this link.
MOSFET acts like an electronic switch or solid state relay. When input at Pin 12 is high fan is ON, when it is low fan is off. A Python script monitors ARM temperature and controls the fan. I have used simple On-Off control with temperature hysteresis. When the temperature exceeds 55 deg C, the fan will be turned on. When it drops below 50 deg C, the fan will be turned off. I have arrived at these numbers empirically. The main aim was to keep the fan off most of the times and to not allow for extreme temperatures. The python script and instructions can be found on my GitHub page.
I did not use PWM based fan speed control and PID loop, as temperature set point accuracy was not important to me. More important was to have the fan run as least as possible. Using PID loop, will complicate the control algorithm and make the fan run for longer - at a lower speed.
Raspberry Pi and the logo is a trademark of the Raspberry Pi Foundation
Python and the logo is a trademark of the Python Software Foundation