Keep Your Raspberry Pi 4 Cool with a Simple DIY Fan

Learn how to install a fan on your Raspberry Pi 4 to keep it cool and running efficiently. This step-by-step guide will walk you through the process of choosing the right fan, connecting it to your Ra …


Updated September 27, 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 install a fan on your Raspberry Pi 4 to keep it cool and running efficiently. This step-by-step guide will walk you through the process of choosing the right fan, connecting it to your Raspberry Pi, and writing a Python script to control its speed based on temperature.

Step 1: Choosing the Right Fan for Your Raspberry Pi 4

Choose a fan that is compatible with your Raspberry Pi 4’s GPIO pins and has enough power to cool your device effectively. A popular choice is the Noctua NF-A4x10 5V PWM fan, which can be purchased from various online retailers such as Amazon or directly from the manufacturer’s website. Make sure to buy a fan with a 3-pin or 4-pin connector for accurate speed control using Pulse Width Modulation (PWM).

Step 2: Connecting the Fan to Your Raspberry Pi 4

  1. Remove the plastic cover from your Raspberry Pi 4 to expose the GPIO pins.
  2. Cut off the end of a fan cable and strip the insulation from two wires, leaving about 1 inch exposed.
  3. Connect one wire to pin 6 (GND) and the other to pin 2 (5V). These pins provide power to the fan.
  4. Strip the insulation from another pair of wires and connect one wire to pin 12 (GPIO 18) for PWM control. The other wire can be left unconnected.
  5. Twist the wires together securely and tape them in place to prevent accidental disconnection.

Step 3: Writing a Python Script to Control Fan Speed Based on Temperature

  1. Open a text editor and create a new file called fan_control.py.
  2. Import the necessary libraries by adding the following lines at the top of the file:
import RPi.GPIO as GPIO
import time
  1. Set up the PWM pin and initialize it with a duty cycle of 0 (off) by adding the following code:
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50) # frequency is set to 50 Hz
pwm.start(0)
  1. Define a function that takes the current temperature as input and returns the desired fan speed as a duty cycle:
def get_fan_speed(temp):
    if temp < 40:
        return 0 # turn off the fan if temperature is below 40 degrees Celsius
    elif temp < 50:
        return 25 # set fan speed to 25% if temperature is between 40 and 50 degrees Celsius
    else:
        return 100 # set fan speed to 100% above 50 degrees Celsius
  1. Add a loop that reads the temperature every second, calculates the desired fan speed, and updates the PWM duty cycle accordingly:
try:
    while True:
        temp = float(open('/sys/class/thermal/thermal_zone0/temp').read()) / 10000 # read CPU temperature in Celsius
        fan_speed = get_fan_speed(temp)
        pwm.ChangeDutyCycle(fan_speed)
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    pwm.stop()
    GPIO.cleanup()
  1. Save the file and run it on your Raspberry Pi 4 using sudo python3 fan_control.py. The script will continuously monitor the temperature and adjust the fan speed accordingly.

Conclusion

By following these steps, you should now have a working fan on your Raspberry Pi 4 that automatically adjusts its speed based on the device’s temperature. This will help to keep your Raspberry Pi cool and running efficiently, ensuring optimal performance and longevity of the hardware. If you encounter any issues or have questions, feel free to ask in the comments section below!