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:

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.

ESP32 MicroPython pump

Overview of 12V Pump

Pinout

ESP32 MicroPython Pump Pinout
image source: diyables.io

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)
The wiring diagram between ESP32 MicroPython Pump

This image is created using Fritzing. Click to enlarge image

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

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and controls pump
How to wire ESP32 and controls pump

ESP32 MicroPython Code

The code below turns the pump ON for five seconds and OFF for five cycles.

""" 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-pump """ from machine import Pin import time RELAY_PIN = 16 # The ESP32 pin GPIO16 connected to the pump 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 pump for 5 seconds time.sleep(5) relay.value(0) # Turn off pump 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 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

The wiring diagram between ESP32 MicroPython pump MOSFET module

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.

""" 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-pump """ 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

  • 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.
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 ≡

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.

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!