I have recently purchased Raspberry Pi 4. It goes quite hot when working (even in idle), so I had to obtain a cooler. I have got one with big aluminum heat sink, and two small fans. They are meant to be connected to 5V and to work all the time. I didn't like that so I have decided to make a fan control.

The easiest way I could find was on this video:
https://www.youtube.com/watch?v=Pw1kSS_FIKk

The idea is quite simple: use one MOSFET to control the fan. I have connected the fan connector to the drain and GPIO pin to the gate and it looks like this:

The schematics of the circuit.

I have used IRF640N MOSFET, since I could not find the IRF530N, which was shown in the video. Here is the photo of the actual contraption:

MOSFET-based fan control

The software for the fan control is basic: turn on the fan if the temperature goes over the high threshold, and turn off the fan if the temperature goes lower than the low threshold.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import threading
import os
import sys

from subprocess import * 

# Fan control port (GPIO 23)
PORT_FAN = 23
HOT  = 55
COLD = 50
OFF = 0
ON  = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(PORT_FAN, GPIO.OUT)

GPIO.output(PORT_FAN, OFF)   # turn off the fan

def getTemp():
p = Popen("vcgencmd measure_temp|cut -c 6-7", shell=True, stdout=PIPE)
t = "" + p.communicate()[0]
t = t.strip()
t = int(t)
return t

while True:
temp = getTemp()
print temp
if temp > HOT:
GPIO.output(PORT_FAN, ON)   # turn on the fan
print "Turning the fan ON"
if temp < COLD:
GPIO.output(PORT_FAN, OFF)   # turn off the fan
print "Turning the fan OFF"
time.sleep(0.1)

GPIO.cleanup() 

 As you can see, the software is simple. If the temperature goes over 55 degrees of Celsius, the fans are turned on. When the CPU temperature goes below 50 degrees, the fans are turned off.

With this big aluminum heat sink, the idle CPU gets between 48 and 50 degrees.

One note: you can see on the photo above that I have connected +5V and GND of the power supply directly on the GPIO pins for +5V and GND, instead of using USB-C power cord. I have done that because I have quite decent 5V power supply which is not USB-C, so if I connect the USB-C cable to it, the additional voltage drop happens across that cable. Lower the quality of the cable, bigger voltage drop becomes.