Learn How to Control a Fan Using the GPIO Pins on Your Raspberry Pi 4

A step-by-step guide for connecting a fan to your Raspberry Pi 4 and controlling it using Python and GPIO pins. …


Updated September 14, 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 for connecting a fan to your Raspberry Pi 4 and controlling it using Python and GPIO pins.

Before you begin, make sure you have the following items:

  1. A Raspberry Pi 4 Model B with Raspbian OS installed.
  2. A fan that can be controlled by a 5V power source and has a three-pin connector (positive, negative, and signal).
  3. A breadboard or a solderless breadboard.
  4. Jumper wires (male to male, female to male, and female to female).
  5. A 1kΩ resistor.
  6. Python installed on your Raspberry Pi.

Here’s how you can connect the fan:

  1. Prepare the hardware components:
    • Connect one end of the jumper wire to the positive (+) terminal of the fan and the other end to a GPIO pin on the Raspberry Pi (e.g., GPIO 23).
    • Connect another jumper wire from the negative (-) terminal of the fan to the ground pin (GND) on the Raspberry Pi.
    • Insert the 1kΩ resistor between the signal pin of the fan and the GPIO pin connected in step 1.
    • Connect the third pin of the fan (signal) to another GPIO pin on the Raspberry Pi (e.g., GPIO 24). This is used for speed control.

Note: Make sure that you do not connect the jumper wires directly to the GPIO pins, but rather use the breadboard or solderless breadboard as an intermediate connection point.

  1. Set up Python and GPIO module:

    • Open a terminal window on your Raspberry Pi and update the package list by running sudo apt-get update.
    • Install the required packages for using the GPIO pins with Python by running sudo apt-get install python3-gpiozero.
  2. Write the Python script:

    • Create a new file named “fan_control.py” and open it in your preferred text editor (e.g., nano, vim).
    • Add the following code to control the fan speed:
from gpiozero import PWMOutputDevice
import time

# Connect the fan signal pin to GPIO 24
fan_speed = PWMOutputDevice(24)

# Control the fan speed using a loop
while True:
    # Set the fan speed to maximum (1.0)
    fan_speed.value = 1.0
    time.sleep(5)
    
    # Turn off the fan (0.0)
    fan_speed.value = 0.0
    time.sleep(2)
    
    # Set the fan speed to half maximum (0.5)
    fan_speed.value = 0.5
    time.sleep(5)
  1. Run the script:
    • Save and close the file.
    • In the terminal, navigate to the directory where you saved the “fan_control.py” file by running cd /path/to/file.
    • Execute the script with sudo python3 fan_control.py

Your Raspberry Pi should now be able to control the speed of the connected fan. The fan will run at maximum speed for 5 seconds, then turn off for 2 seconds, and finally run at half speed for another 5 seconds. You can adjust the sleep times to change the speed or create more complex speed control logic based on your needs.

Remember to always disconnect the power from your Raspberry Pi before making any hardware changes to prevent damage.