Raspberry Pi - Fan
This tutorial instructs you how to use Raspberry Pi to control a fan.
There are two ways to do it: with a relay or with a MOSFET module. A relay can only switch the fan ON and OFF. A MOSFET module can do that too, and it can also change how fast the fan spins, without any clicking sound. We will learn both ways in this tutorial.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of DC Fan
Pinout

A DC fan usually has two pins:
- Negative (-) pin (black): needs to be connected to the negative wire of DC power supply
- Positive (+) pin (red): needs to be connected to the positive wire of DC power supply
The voltage of the DC power supply should be equal to the voltage specified by the fan. In this tutorial, we will use 12VDC and 5VDC fans.
How to Control Fan
- If DC fan is powered by 12V/5V power supply, it run with full speed.
- If DC fan is powered by 12V/5V PWM signal, The fan's speed can be controlled.
This tutorial instructs you how to use Raspberry Pi to turn a 12V/5V fan on or off. To control the fan's speed, see the MOSFET module part near the end of this tutorial.
To control the fan on off, we need to use a relay between the Raspberry Pi and the fan. This relay can be used by Raspberry Pi to switch the fan on or off.
If you are unfamiliar with relays (pinout, operation, programming, etc.), please refer to the Raspberry Pi - Relay tutorial for more information.
The MOSFET module is new to you? The Raspberry Pi - MOSFET Module tutorial explains its pins and how it works.
Relay vs MOSFET Module for a Fan
| Relay | MOSFET Module | |
|---|---|---|
| What you can do | turn the fan ON or OFF | turn it ON or OFF, and set the fan speed with PWM |
| Sound | a click each time it switches | no sound |
| Moving parts | yes, a metal contact that wears out | none, so it lasts a long time |
| Switching speed | slow (a few milliseconds) | very fast |
| Fan type | AC or DC fan | DC fan only |
| Fan supply voltage | any voltage the relay is rated for | about 6V or more (12V fans are fine, 5V fans are not) |
| Isolation | yes, between coil and contacts | yes, by a PC817 optocoupler |
| Good choice for | AC fans, or a simple ON/OFF fan | 12V DC fans that need speed control |
For a 12V DC fan, the MOSFET module is usually the better pick, because you can run the fan slowly and quietly when full speed is not needed. If your fan is 5V or runs on AC, keep the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Please note that if you use 5V fan, you need to use 5V power adapter.
Raspberry Pi Code
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file fan.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the fan's state.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
How to Control Fan Speed with MOSFET Module
Now let's replace the relay with a MOSFET module. The Raspberry Pi sends a PWM signal to the module, and the module gives the fan more or less power. This way, the fan can spin fast, slow, or stop.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

- Raspberry Pi GPIO16 (pin 36) to the PWM pin of the MOSFET module
- Raspberry Pi GND (pin 34) to the GND pin of the MOSFET module
- 12V adapter positive to the + terminal
- Fan red wire (+) to the + terminal too (it shares the terminal with the adapter positive)
- Fan black wire (−) to the LOAD terminal
- 12V adapter negative to the − terminal
- A flyback diode (1N4007 or 1N5819) across the fan: the side with the stripe (cathode) goes to the fan red wire, the other side goes to the fan black wire
Raspberry Pi Code
The script below is the same one used in the MOSFET module tutorial. First, the fan runs at full speed for 2 seconds and stops for 2 seconds. Then it speeds up step by step from 10% to 100%, and slows down again to 0%.
Detailed Instructions
- Prepare the Raspberry Pi, SSH and the RPi.GPIO library as in the Quick Steps of the relay part above.
- Wire the fan and the MOSFET module as shown in the wiring diagram above.
- Create a Python script file MosfetModule.py and add the following code:
- Plug in the 12V adapter, then run the script:
- Listen and watch the fan: it goes full speed, stops, then slowly speeds up and slows down.
- Check the output on the terminal:
Many fans do not start at 10% or 20%. This is normal. They begin to turn at about 30%.
※ NOTE THAT:
The MOSFET module works with DC only, and its load supply must be at least about 6V. For an AC fan or a 5V fan, use the relay way instead.