Cool Down Your Raspberry Pi 4 with This Easy DIY Fan Tutorial

Learn how to install a fan in your Raspberry Pi 4 and keep it running at optimal performance levels. Follow this step-by-step guide for a cool and efficient setup. …


Updated September 28, 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 in your Raspberry Pi 4 and keep it running at optimal performance levels. Follow this step-by-step guide for a cool and efficient setup.

Materials Needed:

  1. Raspberry Pi 4 Model B (with power supply)
  2. Micro USB cable (5V/3A or higher)
  3. 40mm or 50mm fan with 4-pin PWM connector
  4. Screwdriver
  5. Hot glue gun
  6. Wire cutters
  7. Electrical tape
  8. Wire strippers
  9. Small Phillips head screwdriver
  10. Raspberry Pi 4 case (optional)

Step 1: Prepare the Fan

First, you need to prepare your fan for installation. Remove the plastic cover and any protective film from the fan blades. This will allow better airflow. You can also use a hot glue gun to secure the fan blades if necessary.

Step 2: Identify the Fan Pins

The 4-pin PWM connector on your fan has four pins labeled 1, 2, 3, and 4. Pin 1 is positive (+), pin 2 is ground (-), and pins 3 and 4 are used for controlling the fan speed. We will be using GPIO (General-Purpose Input/Output) pins on the Raspberry Pi to control the fan speed.

Step 3: Power Up the Raspberry Pi

Connect your Raspberry Pi to a power source and make sure it is running properly. You can use the included power supply or an external one, depending on your setup.

Step 4: Open the Raspberry Pi Case (Optional)

If you are using a case for your Raspberry Pi, open it and locate the area where you want to install the fan. This should be in a well-ventilated area near the CPU heatsink. If you don’t have a case, you can mount the fan directly on the heatsink or use double-sided tape to secure it in place.

Step 5: Strip and Cut Wires

Using wire strippers, strip about 1 cm of insulation from the positive (+) and ground (-) wires on your fan’s connector. Then, cut these wires about 2-3 cm long.

Step 6: Connect the Power Wires

Use electrical tape to securely attach the positive (+) wire to the Raspberry Pi’s 5V power pin (pin 4) and the ground (-) wire to one of the ground pins (either 6 or 9). Make sure to use a short, secure connection.

Step 7: Connect the PWM Wires

Using your wire strippers, strip about 1 cm of insulation from pins 3 and 4 on the fan’s connector. Then, cut these wires about 5-6 cm long.

Next, attach one end of each wire to GPIO pin 18 (PWM0) using electrical tape. The other ends will be connected to a resistor and then to ground.

Step 8: Add a Resistor

Fan speed is controlled by adjusting the voltage at the PWM pins. To do this, you need to add a resistor between the PWM pin (GPIO 18) and ground. There are many different resistor values that can be used for controlling fan speed, but a common value is 220K ohms.

To add the resistor, strip about 1 cm of insulation from one end of a wire and solder it to one leg of a 20K ohm resistor. Then, securely attach the other end of the resistor to ground (either pin 6 or 9). Finally, use electrical tape to secure the connection.

Step 9: Mount the Fan

Using your screwdriver, secure the fan blades to the heatsink or case with hot glue or small screws. Make sure it is in a well-ventilated area and can rotate freely.

Step 10: Test Your Fan

To test your fan, you can use the command line to control its speed. Open a terminal window on your Raspberry Pi and run the following commands:

sudo apt update && sudo apt install pigpio python3-pigpio
python3

Next, type the following code into the Python interpreter:

import pigpio
pi = pigpio.pi()
pin = 18 # Use GPIO pin 18 for PWM control
pi.set_PWM_frequency(pin, 25) # Set frequency to 25Hz (adjust as needed)
pi.set_PWM_range(pin, 100) # Set PWM range from 0-100 (adjust as needed)
pi.set_PWM_dutycycle(pin, 50) # Set initial speed to 50%

You should now hear the fan start spinning at a medium speed. You can adjust the duty cycle (last number in the last command) to increase or decrease the fan speed. To stop the fan, run pi.set_PWM_dutycycle(pin, 0).

Congratulations! You have successfully installed a fan in your Raspberry Pi 4 and are now ready to cool it down and keep it running at optimal performance levels. Enjoy your newfound computing power and efficiency.