Control Your Raspberry Pi’s Temperature With a DIY Cooling System

Learn how to connect a fan to your Raspberry Pi 4 and monitor its temperature using Python. Keep your Pi running smoothly at all times! …


Updated August 21, 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 and monitor its temperature using Python. Keep your Pi running smoothly at all times! Are you tired of hearing your Raspberry Pi’s cooling fan work overtime or struggling with overheating issues? In this article, we will show you how to connect a fan to your Raspberry Pi 4 and monitor its temperature using Python. By doing so, you can keep your Pi running smoothly at all times!

Step 1: Gather the Required Components

To begin, you’ll need the following components:

  • Raspberry Pi 4 (Model B)
  • Fan (5V, 3-Pin, with a connector that matches your Pi)
  • Jumper wires
  • Soldering iron and solder (optional)
  • Heat shrink tubes or heat-shrinkable tubing (optional)

Step 2: Connect the Fan to the Raspberry Pi

  1. Cut off a section of the jumper wires to expose about an inch of each wire. Strip about half an inch of insulation from each end using your teeth or a utility knife.
  2. Solder the red wire from the fan’s connector to the 5V pin on the Raspberry Pi (Pin 2).
  3. Solder the black wire from the fan’s connector to one of the ground pins on the Raspberry Pi (Pin 6 or Pin 9).
  4. Optional: Use heat shrink tubes or heat-shrinkable tubing to cover and protect the solder joints.

Step 3: Write a Python Script to Control the Fan

  1. Open your favorite code editor on your Raspberry Pi, such as nano or Visual Studio Code.
  2. Create a new Python file named fan_control.py using the following command in the terminal:
touch fan_control.py
  1. Open fan_control.py and add the following code to control your fan:
import RPi.GPIO as GPIO
import time

# Set up GPIO mode
GPIO.setmode(GPIO.BCM)

# Choose the pin connected to the fan (change this if needed)
FAN_PIN = 18

# Set up the pin as an output
GPIO.setup(FAN_PIN, GPIO.OUT)

def turn_on():
    GPIO.output(FAN_PIN, GPIO.HIGH)

def turn_off():
    GPIO.output(FAN_PIN, GPIO.LOW)

while True:
    # Check the CPU temperature of your Raspberry Pi
    cpu_temp = float(open('/sys/class/thermal/thermal_zone0/temp').read()) / 10000
    
    # Turn on the fan if the temperature is above 50 degrees Celsius
    if cpu_temp > 50:
        turn_on()
    else:
        turn_off()
        
    # Wait for a second before checking again
    time.sleep(1)
  1. Save and exit the file.

Step 4: Run the Python Script on Startup

To make sure your fan script runs automatically whenever you boot up your Raspberry Pi, add it to the list of services that startup on boot.

  1. Open the rc.local file with root privileges using nano or any other text editor:
sudo nano /etc/rc.local
  1. Add the following line above the line exit 0:
sudo python3 /path/to/fan_control.py &

Replace /path/to/ with the actual path to your script, for example:

sudo python3 /home/pi/fan_control.py &
  1. Save and exit the file.

Step 5: Test Your Setup

  1. Reboot your Raspberry Pi using the following command in the terminal:
sudo reboot
  1. Once it’s back up, check the temperature of your Raspberry Pi using the vcgencmd tool:
vcgencmd measure_temp
  1. Load a resource-intensive application or activity on your Raspberry Pi to see if the fan turns on automatically when the temperature rises above 50 degrees Celsius.

Now you have a DIY cooling system for your Raspberry Pi! You can adjust the temperature threshold in the Python script to suit your needs and even add more fans or sensors if necessary. Remember, keeping your Raspberry Pi cool will ensure its longevity and proper functioning!