Keep Your Raspberry Pi 4 Cool and Running Smoothly with a Simple Fan Setup

Learn how to connect a cooling fan to your Raspberry Pi 4 for better performance, stability, and a longer lifespan of your device. This guide will show you the steps needed to set up a basic fan contr …


Updated August 26, 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 cooling fan to your Raspberry Pi 4 for better performance, stability, and a longer lifespan of your device. This guide will show you the steps needed to set up a basic fan control script using Python and GPIO pins.

Raspberry Pi 4 is an incredible piece of hardware that has changed the game in the world of single-board computers. However, one of its downsides is its tendency to overheat when running intensive tasks or programs for extended periods of time. Overheating can lead to system crashes and even permanent damage to your device. In this article, we’ll show you how to connect a cooling fan to your Raspberry Pi 4 in just a few easy steps.

Requirements

  • Raspberry Pi 4 (any model)
  • Micro USB power supply for the Pi
  • 5V DC cooling fan with a 3-pin connector
  • Breadboard and jumper wires
  • Raspberry Pi GPIO breakout board

Step 1: Connect the Fan to the Raspberry Pi

First, you’ll need to solder the pins of the fan onto the breadboard. Make sure to use a separate power supply for the fan and connect it to the 5V pin on the GPIO breakout board. Then, connect the ground pin to another pin on the breakout board.

Next, attach the negative lead of the fan to the ground pin (GND) on the breakout board and the positive lead to a GPIO pin. Any GPIO pin will work, but for this example we’ll use GPIO 18. Make sure to note which pin you choose as you’ll need it later in the script.

Step 2: Set Up Python and RPi.GPIO

Open a terminal on your Raspberry Pi and install RPi.GPIO library by running:

sudo apt-get update
sudo apt-get install python3-rpi.gpio

Once the installation is complete, you can start writing the fan control script in Python. Open your favorite text editor (e.g., nano) and create a new file called fan_control.py.

Step 3: Write the Fan Control Script

Here’s an example of a basic fan control script using RPi.GPIO library:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) # Set the pin numbering mode to BCM (Broadcom)
fan_pin = 18 # Choose the same pin you connected the fan to in Step 1
GPIO.setup(fan_pin, GPIO.OUT) # Set up the pin as an output

try:
    while True:
        temp = float(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000 # Read the CPU temperature
        print("Current temperature: {}°C".format(temp))
        
        if temp > 50.0: # Adjust this threshold according to your preference
            GPIO.output(fan_pin, GPIO.HIGH) # Turn on the fan
        else:
            GPIO.output(fan_pin, GPIO.LOW) # Turn off the fan
            
        time.sleep(5) # Check the temperature every 5 seconds
        
except KeyboardInterrupt:
    pass
    
finally:
    GPIO.cleanup() # Clean up the GPIO pins on exit

Save this file and make it executable with:

chmod +x fan_control.py

Now you can run the script by executing ./fan_control.py in the terminal. The fan will turn on when the CPU temperature exceeds 50°C and turn off when it drops below that value. You can adjust this threshold to suit your needs.

Step 4: Run the Fan Control Script at Startup (Optional)

If you want the fan to automatically start when your Raspberry Pi boots up, add the script to the /etc/rc.local file before the exit 0 line:

sudo nano /etc/rc.local

Add the following lines before exit 0:

sudo python3 /path/to/fan_control.py &
exit 0

Replace /path/to/ with the actual path where you saved the fan_control.py file. Save and exit the file. Now your Raspberry Pi will automatically start the fan control script at boot.

Conclusion

By following these steps, you can connect a cooling fan to your Raspberry Pi 4 and keep it running smoothly and efficiently. The fan will turn on when the CPU temperature rises above a certain threshold and turn off when it cools down, ensuring optimal performance and longevity for your device. Remember to adjust the temperature threshold in the script according to your needs and the environment where your Raspberry Pi is running.