ESP32 MicroPython Control Fan

This tutorial instructs you how to control a 12V or 5V fan with a ESP32 and MicroPython. We will cover:

There are two ways to drive the fan: a relay or a MOSFET module. A relay is a simple switch, so the fan is either fully ON or fully OFF. A MOSFET module can do that too, and it can also make the fan spin faster or slower with PWM, without any clicking sound. This page shows both ways.

ESP32 MicroPython fan

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.
Fan Pinout
image source: diyables.io

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 rotates 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 ESP32. We will show you how to control the fan speed in another guide. To control the fan, we connect a relay between the ESP32 and the fan. The ESP32 controls the relay to indirectly control the fan. If you're new to relay or MicroPython programming for the ESP32, I recommend checking out the below tutorial:

Want speed control as well? Then the MOSFET module is the part to use. Its pins and how it works are explained in the ESP32 MicroPython - MOSFET Module tutorial.

Relay vs MOSFET Module for Fan

Relay MOSFET Module
What you can control fan ON or OFF fan ON or OFF, plus fan speed (PWM)
Noise a click each time it switches no sound at all
How fast it switches slow, a few milliseconds very fast
Life the metal contacts wear out after many switches nothing moves, so nothing wears out
Fan type AC or DC fans DC fans only
Fan supply voltage any voltage within the relay rating about 6V or more (a 5V fan will not work)
Isolation yes, between coil and contacts yes, through the PC817 optocoupler
Good choice for AC fans, plain ON/OFF 12V DC fans when you want speed control

For the 12V DC fan in this guide, the MOSFET module is the better pick if you want a quiet fan that can run at half speed. Keep the relay for an AC fan, or for the 5V fan, because the MOSFET module needs a higher supply voltage.

Wiring Diagram

  • How to connect ESP32 and controls fan using breadboard (powered via USB cable)
The wiring diagram between ESP32 MicroPython Fan

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and controls fan using breadboard (powered via Vin pin)
The wiring diagram between ESP32 MicroPython Fan

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and controls fan
How to wire ESP32 and controls fan

ESP32 MicroPython Code

This code turns the fan ON for five seconds and then turns it OFF for another five seconds.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-controls-fan """ from machine import Pin import time RELAY_PIN = 16 # The ESP32 pin GPIO16 connected to the fan via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # Turn on fan for 5 seconds time.sleep(5) relay.value(0) # Turn off fan for 5 seconds time.sleep(5)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the fan according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Check how the fan is working.

Code Explanation

The comments in the ESP32 MicroPython code above explain the details.

How to Control Fan Speed with MOSFET Module

In this part, the relay is replaced by a MOSFET module. The ESP32 now sends a PWM signal, so the fan does not only start and stop. It can also turn slowly, fast, or anywhere in between.

Wiring Diagram

The wiring diagram between ESP32 MicroPython cooling fan MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
ESP32 GPIO18 PWM pin of the MOSFET module
ESP32 GND GND pin of the MOSFET module
12V adapter + (via DC power jack) + terminal
Fan red wire (+) + terminal (shared with the adapter +)
Fan black wire (−) LOAD terminal
12V adapter − (via DC power jack) − terminal

A fan has a motor coil inside. Put a flyback diode (1N4007 or 1N5819) across the two fan wires. The end with the stripe (cathode) goes to the fan + wire, and the other end goes to the fan − wire. This protects the MOSFET from voltage spikes.

ESP32 MicroPython Code - Fan Speed with MOSFET Module

The fan runs at full speed for 2 seconds and stops for 2 seconds. Then the script raises the power from 10% to 100%, so you hear the fan speed up. After that it lowers the power again, and the fan slows down to a stop.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-controls-fan """ from machine import Pin, PWM import time MOSFET_PIN = 18 # ESP32 GPIO18 connected to the PWM pin of the MOSFET module pwm = PWM(Pin(MOSFET_PIN), freq=500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.duty_u16(0) # start with the load OFF def set_load(percent): pwm.duty_u16(percent * 65535 // 100) # convert 0-100% to 0-65535 print("Load power: {}%".format(percent)) while True: # turn the load fully ON, then fully OFF print("Load: ON") set_load(100) time.sleep(2) print("Load: OFF") set_load(0) time.sleep(2) # increase the power step by step (PWM) for percent in range(10, 101, 10): set_load(percent) time.sleep(0.5) # decrease the power step by step (PWM) for percent in range(90, -1, -10): set_load(percent) time.sleep(0.5) time.sleep(1)

Detailed Instructions

  • Set up Thonny and the ESP32 in the same way as in the relay Quick Steps above.
  • Wire the fan, the MOSFET module and the DC power jack as shown in the diagram. Keep the 12V adapter unplugged.
  • Paste the code into Thonny and save it to the ESP32 as main.py.
  • Plug in the 12V adapter.
  • Click the green Run button (or press F5).
  • Listen to the fan and look at the Shell in Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Some fans do not start at 10% or 20%. This is normal. They begin to turn at a higher power level.

※ NOTE THAT:

The MOSFET module works with DC only, and it needs a fan supply of about 6V or more. For an AC fan, use the relay.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!