The Easy Way to Cool Your Raspberry Pi 4

Learn how to connect a fan to your Raspberry Pi 4 to keep it cool and running efficiently. This step-by-step guide will show you how to wire the fan, control it using Python code, and monitor its perf …


Updated August 1, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to connect a fan to your Raspberry Pi 4 to keep it cool and running efficiently. This step-by-step guide will show you how to wire the fan, control it using Python code, and monitor its performance.

Raspberry Pi is a popular single board computer used for various projects that require computation power. As powerful as they are, Raspberry Pi 4 can generate a lot of heat during operation, especially when running multiple tasks or playing high-performance games. To prevent overheating and damage to the device, it’s essential to keep your Raspberry Pi cool. One simple way to do this is by connecting an external fan to the board. In this article, we will show you how to connect a fan to your Raspberry Pi 4 and use Python code to control its speed based on the CPU temperature.

Materials Required:

  1. Raspberry Pi 4
  2. Micro USB power supply
  3. Breadboard
  4. Jumper wires (male-to-male)
  5. Fan with 3-pin connector (Positive, Negative, and Switch)
  6. Resistor (1kΩ or higher)
  7. Raspberry Pi OS installed on an SD card
  8. Monitor, Keyboard, and Mouse (for setup and testing)
  9. Power cable for the fan

Step 1: Prepare Your Raspberry Pi 4

Before connecting any components to your Raspberry Pi 4, make sure it is powered off and all external devices are disconnected.

Step 2: Connect the Fan

Now, let’s connect the fan to your Raspberry Pi 4 using a breadboard and jumper wires. Follow these steps:

  1. Cut off the end of a jumper wire and strip about 1mm of insulation from one end.
  2. Solder the positive terminal of the fan (red wire) to the exposed copper wire.
  3. Strip about 1mm of insulation from another jumper wire and solder it to the negative terminal of the fan (black wire).
  4. Insert the jumper wires into the breadboard.
  5. Connect one end of the positive wire to any GPIO pin on the Raspberry Pi 4 (e.g., Pin 2 or 4) and the other end to the positive power rail (+3V3).
  6. Connect the negative wire to any GND pin on the Raspberry Pi 4.
  7. Connect a resistor between the GPIO pin and the fan’s switch terminal (white wire). The value of the resistor should be 1kΩ or higher.
  8. Connect the power cable for the fan to the positive power rail (+5V) and negative power rail (GND).

Your fan is now connected to your Raspberry Pi 4. The GPIO pin controls the speed of the fan, while the resistor prevents the fan from drawing too much current and damaging the board.

Step 3: Control the Fan with Python

To control the fan speed based on the CPU temperature, we will use a Python script that reads the temperature and adjusts the GPIO pin accordingly. Follow these steps to create and run the script:

  1. Open the terminal on your Raspberry Pi 4 and navigate to the home directory by typing cd ~.
  2. Create a new file called fan_control.py using the command nano fan_control.py.
  3. Copy and paste the following code into the file:
import time
import RPi.GPIO as GPIO
import os

# Set up GPIO pin for fan control
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT) # Use Pin 4 (GPIO 4)
fan = GPIO.PWM(4, 50) # Set frequency to 50Hz
fan.start(0) # Start with duty cycle of 0 (off)

# Function to read CPU temperature
def get_cpu_temp():
    temp = os.popen("vcgencmd measure_temp").readline()
    return float(temp.replace("temp=", "").replace("'C\n", ""))

try:
    while True:
        # Read the CPU temperature
        cpu_temp = get_cpu_temp()
        print(f"CPU Temperature: {cpu_temp}°C")
        
        # Set the fan speed based on the temperature
        if cpu_temp > 60.0:
            fan.ChangeDutyCycle(100) # Full speed
        elif cpu_temp > 55.0:
            fan.ChangeDutyCycle(75) # Medium speed
        else:
            fan.ChangeDutyCycle(0) # Off
        
        time.sleep(10) # Check temperature every 10 seconds
except KeyboardInterrupt:
    fan.stop()
    GPIO.cleanup()
  1. Save and exit the file by pressing CTRL + X, then Y and Enter.
  2. Run the script with sudo python3 fan_control.py. The fan will now turn on or off based on the CPU temperature.

Step 4: Monitor Fan Performance

To check if your fan is working correctly, you can use a tool called “htop” to monitor the CPU temperature and fan speed. Follow these steps:

  1. Install htop using sudo apt-get install htop.
  2. Run htop with htop in the terminal.
  3. Press F5 to display temperatures. You should see the CPU temperature and the frequency of your fan under “PWM”.
  4. Perform some resource-intensive tasks on your Raspberry Pi 4 (e.g., running a game or running a stress test) to see how the fan responds.

Your Raspberry Pi 4 is now equipped with an external fan that can help keep it cool and running efficiently. With the Python script, you can monitor and control the fan speed based on CPU temperature for optimal performance and longevity of your device.