ESP32 MicroPython Control Pump
In this guide, you will learn how to use a 12V pump with a ESP32 and MicroPython. In detail, we will learn:
- How to connect pump to ESP32
- How to write MicroPython code for ESP32 to turn the pump on and off
- How to change the water flow by driving the pump with a MOSFET module
This tutorial will help you create systems for watering plants, aquariums, or water refill stations.
You can switch the pump with a relay or with a MOSFET module. The relay only starts and stops the pump. The MOSFET module starts and stops it too, but it can also run the pump slower or faster with PWM, so you control how much water flows. And it makes no click. We will build both versions.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of 12V Pump
Pinout

A 12V pump usually has two pins.
- Connect the negative (-) pin (black) to the GND on the DC power supply.
- Connect the positive (+) pin (red) to the 12V on the DC power supply.
How to Control Pump
A 12V power supply works perfectly for a 12V pump. To control the pump with an ESP32, connect a relay between them. The ESP32 will use the relay to turn the pump on and off. If you're new to relay or MicroPython programming for the ESP32, I recommend checking out the below tutorial:
Never used a MOSFET module before? Read ESP32 MicroPython - MOSFET Module first. It explains the pins, the screw terminals and the PWM code.
Relay vs MOSFET Module for Pump
| Relay | MOSFET Module | |
|---|---|---|
| Pump control | run or stop | run or stop, plus pump speed and flow (PWM) |
| Best use | AC pumps, plain ON/OFF | 12V DC pumps, flow control, many starts and stops |
| Load type | AC or DC | DC only |
| Power supply for the pump | any voltage the relay is rated for | 6V or more (the gate is powered from it) |
| Switching sound | audible click | none |
| Switching time | slow, in milliseconds | very short |
| Wear | contacts wear over time | no moving parts |
| Isolation | yes, coil and contacts | yes, PC817 optocoupler |
For the 12V DC pump used here, choose the MOSFET module when you want to set the water flow, for example a gentle flow for plants. If your pump runs on AC mains, the relay is the only option of the two.
Wiring Diagram
- How to connect ESP32 and controls pump using breadboard (powered via USB cable)

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls pump using breadboard (powered via Vin pin)

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls pump using screw terminal block breakout board (powered via USB cable)

- How to connect ESP32 and controls pump using screw terminal block breakout board (powered via Vin pin)

ESP32 MicroPython Code
The code below turns the pump ON for five seconds and OFF for five cycles.
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 12V pump 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.
- Verify that the pump is working correctly.
Code Explanation
The comments in the ESP32 MicroPython code above explain everything.
How to Control Pump Flow with MOSFET Module
This section uses a MOSFET module in place of the relay. By changing the PWM duty cycle, the ESP32 sets the pump speed, and so the amount of water it moves.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| MOSFET Module Pin | Connect To |
|---|---|
| PWM | ESP32 GPIO18 |
| GND | ESP32 GND |
| + | 12V adapter + (via the DC power jack) and the pump red wire (+) |
| LOAD | pump black wire (−) |
| − | 12V adapter − (via the DC power jack) |
The pump motor is a coil, so add a flyback diode (1N4007 or 1N5819) between the two pump wires: the striped end (cathode) to the pump + wire, the other end to the pump − wire.
ESP32 MicroPython Code - Pump with MOSFET Module
The pump first runs at full power for 2 seconds, then stops for 2 seconds. Next, the power goes up in steps from 10% to 100%, and the water flow becomes stronger. Then the power goes down step by step, and the flow gets weaker until it stops.
Detailed Instructions
- Do the Thonny and ESP32 setup from the relay Quick Steps above.
- Put the pump in water. Never run it dry.
- Connect everything as in the diagram, but do not plug in the 12V adapter yet.
- Save the code to the ESP32 as main.py.
- Plug in the 12V adapter and click the green Run button (or press F5).
- Watch the water flow and the messages in the Shell.
At low power, a pump may only hum and move no water. That is normal. It starts pumping at a higher level.
Remember: the MOSFET module is for DC pumps only, with a supply of at least about 6V. Use the relay for an AC pump.