Control your Raspberry Pi’s Cooling System with Python!

A step-by-step guide on how to connect and control a fan using a Raspberry Pi 4 and Python. …


Updated August 30, 2023

Need help with your Raspberry Pi?
Contact Me!

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


A step-by-step guide on how to connect and control a fan using a Raspberry Pi 4 and Python. In this article, we will learn how to connect a fan to a Raspberry Pi 4 and use Python to turn it on and off based on the temperature of the device. By doing so, you can ensure your Raspberry Pi stays cool and runs efficiently. Here are the steps to follow:

  1. Components needed:
  • Raspberry Pi 4
  • Fan (with 3-pin connector)
  • Breadboard
  • Jumper wires
  • Resistor (1kΩ)
  • Transistor (NPN type)
  1. Connecting the fan to the Raspberry Pi:

    • Remove the back cover of the Raspberry Pi and locate the GPIO pins on the board.
    • Strip 1 cm of insulation from the ends of each jumper wire and expose about 1 mm of copper wire. This will help prevent short circuits.
    • Solder one end of the fan’s positive wire to the center pin (+) of the 3-pin connector.
    • Solder one end of the fan’s ground wire to any of the outer pins (-) of the 3-pin connector.
    • Connect the third pin on the 3-pin connector to a GPIO pin (e.g., GPIO18) using a jumper wire. This pin will be used as the control signal for the fan.
    • Connect the other end of the resistor to the same GPIO pin, and solder the free end of the resistor to the collector pin of the transistor (C).
    • Connect the emitter pin (E) of the transistor to a ground pin on the Raspberry Pi.
    • Connect the base pin (B) of the transistor to another GPIO pin (e.g., GPIO17) using another jumper wire. This will be used as the control input for the fan.
  2. Installing Python and necessary libraries:

    • Open a terminal on your Raspberry Pi and update the package list with sudo apt-get update.
    • Install python-gpiozero library by running sudo apt-get install python3-gpiozero. This library makes it easy to control GPIO pins with Python.
  3. Writing the Python script:

    • Create a new Python file named “fan_control.py” using your favorite text editor (e.g., nano).
    • Import the required libraries by adding the following lines at the beginning of the file:
      from gpiozero import LED, Button
      from time import sleep
      import os
      
    • Set up the fan and button objects using GPIO pins:
      fan = LED(18) # GPIO pin for controlling the fan
      button = Button(17) # GPIO pin for reading temperature input
      
    • Define a function to read the current CPU temperature:
      def get_cpu_temp():
          temp = os.popen("vcgencmd measure_temp").readline()
          return float(temp.replace('temp=', '').replace("'C\n", ''))
      
    • Use a while loop to continuously monitor the temperature and turn on/off the fan based on a threshold value:
      while True:
          temp = get_cpu_temp()
      
          if temp > 60.0: # Threshold temperature in Celsius
              fan.on() # Turn on the fan
          else:
              fan.off() # Turn off the fan
      
          sleep(1) # Wait for 1 second before checking again
      
    • Save and exit the file.
  4. Running the script:

    • Run the Python script by typing python3 fan_control.py in the terminal. The fan should turn on when the temperature exceeds 60°C and turn off when it drops below that value. Press Ctrl+C to stop the script.

Now you have successfully connected a fan to your Raspberry Pi 4 and can control its speed based on the device’s temperature using Python! This simple setup can help protect your device from overheating and ensure reliable performance. Happy tinkering!