Variable Speed Cooling Fan for Raspberry Pi using PWM (video#138)
In video #131 we pimped a Raspberry Pi. One of the new features was a cooling fan which switched automatically on if CPU temperature was too high. Viewers suggested to make the speed of the fan variable. And this is, what we will do today. We also will learn a few things about Pulse Width Modulation, PID controllers, MOSFET transistors, and even a little bit of magic.
So, let’s start: in video #131 I used a simple concept: A MOSFET transistor to switch the fan on and off and a Python program which measures the CPU temperature and switched the MOSFET on, when the temperature was too high. Very simple. I used a beefy IRLZ44N transistor, because it is logic level. In video #137 I showed, that these FETs switch on at a voltage of below 2.5 volt. This is, why they are ideal for microprocessors with 3.3 volt logic.
Usually, you should add two resistors to drive a FET: R1 to discharge the gate capacitance of the MOSFET, and R2 to limit the current which is created if you change the signal at the gate. R1 is usually a few k Ohms, and R2 a few 100 ohms.
A diode should be connected in parallel to the fan, because it has an inductance and inductors create high “reverse” voltages when switched on and off fast. In our case, R1 and R2 are built into the GPIO of the Raspberry and can be omitted. The diode is not mandatory for a small fan, but it is better to include one.
The python script has two tasks. It has to be started at boot. I described how this can be done in a earlier blog and in video #131.
The python script is at the same place. You can get it with the following command:
Please rename it to fan_shutdown.py:
mv fanShutdownPWM.py fan_shutdown.py
Or adjust your startup file accordingly.
As a result, you should get a quite constant temperature of 45 degrees centigrade:
You can change this temperature on line 15 in the python file:
desiredTemp = 45 # The maximum temperature in Celsius after which we trigger the fan. If you use a different fan, it might be necessary to change the parameters of the PI controller: