ESP32 MicroPython Control Fan
This tutorial instructs you how to control a 12V or 5V fan with a ESP32 and MicroPython. We will cover:
- How to connect a 12V/5V fan to a ESP32
- How to write MicroPython code for the ESP32 to control the fan on and off.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
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 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:
Wiring Diagram
- How to connect ESP32 and controls fan using breadboard (powered via USB cable)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls fan using breadboard (powered via Vin pin)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls fan using screw terminal block breakout board (powered via USB cable)
- How to connect ESP32 and controls fan using screw terminal block breakout board (powered via Vin pin)
ESP32 MicroPython Code
This code turns the fan ON for five seconds and then turns it OFF for another five seconds.
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.