Raspberry Pi Pico - Control Fan
This tutorial instructs you how to manage a 12V or 5V fan with a Raspberry Pi Pico. We will cover:
- How to connect a 12V/5V fan to a Raspberry Pi Pico
- How to code the Raspberry Pi Pico to switch the fan on and off.
- How to use a MOSFET module in place of the relay, so the Pico can also set the fan speed.
A relay can only start or stop the fan. A MOSFET module can do that too, and it can also make the fan spin faster or slower with PWM, without any click. This tutorial shows both ways.

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 comes with two pins.
- Connect the black pin, labeled as negative (-), to the negative wire of the DC power supply.
- Connect the red pinned labeled as positive (+), to the positive wire of the DC power supply.

Make sure that the voltage of the DC power supply is the same as the voltage needed by the fan. In this guide, we use fans that require 12VDC and 5VDC.
How to Control Fan
- When a DC fan uses a 12V/5V power supply, it operates at maximum speed.
- When a DC fan uses a 12V/5V PWM signal, you can adjust the speed of the fan.
In this guide, we will teach you how to turn a fan on and off using a Raspberry Pi Pico. We will show you how to control the fan speed in another guide. To control the fan, we connect a relay between the Raspberry Pi Pico and the fan. The Raspberry Pi Pico uses the relay to operate the fan. If you're new to relay or MicroPython programming for the Raspberry Pi Pico, I recommend checking out the below tutorial:
New to the MOSFET module? Its pins, versions and how it works are in the Raspberry Pi Pico - MOSFET Module tutorial.
Relay vs MOSFET Module for a Fan
| Relay | MOSFET Module | |
|---|---|---|
| What you can do | start and stop the fan | start, stop and change the fan speed (PWM) |
| Noise | a click each time it switches | no sound |
| How fast it switches | slow, a few milliseconds | very fast |
| How long it lasts | the contacts wear out after many switches | nothing moves inside, so it lasts long |
| Kind of power | AC or DC | DC only |
| Fan supply voltage | any, within the relay rating | about 6V or more |
| Pico is protected | yes, coil and contacts are apart | yes, by a PC817 optocoupler |
| Good choice for | an AC fan, or a DC fan that only needs on and off | a 12V DC fan when you want speed control |
For a 12V DC fan, the MOSFET module is the better pick, because you get speed control and quiet work. For a fan that runs on AC mains, stay with the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
This code turns the fan ON for five seconds and then turns it OFF for another five seconds.
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Connect the Raspberry Pi Pico to the fan according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Check how the fan is working.
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.
Code Explanation
The comments in the Raspberry Pi Pico code above explain the details.
How to Control Fan with MOSFET Module
Here, the MOSFET module takes the place of the relay. The Pico sends a PWM signal on GP18, and the fan speed follows it.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Make these connections:
- Pico GP18 to the PWM pin of the module.
- Pico GND to the GND pin of the module.
- 12V adapter positive wire to the + terminal.
- Fan red wire (+) to the same + terminal.
- Fan black wire (−) to the LOAD terminal.
- 12V adapter negative wire to the − terminal.
- A flyback diode (1N4007 or 1N5819) across the two fan wires. The end with the stripe (cathode) goes to the fan +.
Raspberry Pi Pico Code - Fan Speed
First, the fan runs at full speed for 2 seconds and stops for 2 seconds. Next, the power goes up from 10% to 100%, so the fan speeds up step by step, and then it goes back down, so the fan slows down. A fan may not start at a very low power, so it can stay still in the first steps.
Detailed Instructions
- Do the Thonny and MicroPython setup as in the Quick Steps of the relay part above.
- Wire the fan and the MOSFET module as shown in the diagram. Keep the 12V adapter unplugged.
- Copy the code above into Thonny and save it to the Raspberry Pi Pico as main.py.
- Plug in the 12V adapter.
- Click the green Run button.
- Listen to the fan. It goes up and down in speed.
- Look at the Shell at the bottom of Thonny.
Tip: the MOSFET module works with DC only, and the fan supply must be about 6V or higher. A 5V fan is too low for it, so use the relay for a 5V fan or an AC fan.