Arduino Nano ESP32 - Control Pump

This tutorial provides instructions on how to use Arduino Nano ESP32 to control a pump. You can extend this tutorial to build the DIY aquarium, cocktail machine, coffee machine, or irrigation system...

You can drive the pump in two ways. One is a relay: it only starts and stops the pump. The other is a MOSFET module: it starts and stops the pump too, but it can also change the pump speed with PWM, so you can give more or less water flow. It works quietly. You will learn both ways here.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of 12V Pump

Pump Pinout

Arduino Nano ESP32 Pump Pinout
image source: diyables.io

A 12V Pump has two wires:

  • Negative (-) wire (black): connect this wire to the negative wire of 12V DC power supply
  • Positive (+) wire (red): connect this wire to the Positive wire of 12V DC power supply

How to Control Pump using Arduino Nano ESP32

If we connect a 12V power supply to a pump, the pump will work. We can programmatically control a pump by using Arduino Nano ESP32. To do so, we need a relay in between the pump Arduino Nano ESP32 board. We have a specific tutorial about relay, which contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn about relay in the Arduino Nano ESP32 - Relay tutorial

A MOSFET module can take the place of the relay. The Arduino Nano ESP32 - MOSFET Module tutorial explains this module from the start, if you have not used it before.

Which One to Use with a Pump: Relay or MOSFET Module

Relay MOSFET Module
Pump control start and stop only start, stop, and set the flow (PWM)
Switching sound audible click none
Speed of switching slow (a few ms) very fast
Life contacts wear out no moving parts
Pump type AC or DC pump DC pump only
Pump supply as the relay allows 6V or more
Isolation yes, between coil and contacts yes, by a PC817 optocoupler
Best use AC pumps, plain ON/OFF DC pumps, flow control, many ON/OFF cycles

With a 12V DC pump, choose the MOSFET module when you want to give less water, for example a slow drip for plants. If your pump runs on AC, or ON/OFF is all you need, the relay is fine.

Wiring Diagram between ESP32, Relay and Pump

The wiring diagram between Arduino Nano ESP32 and Pump

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 - Pump Code

The below code periodically turns the pump ON/OFF in every 4 seconds.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-pump */ #define RELAY_PIN D2 // The Arduino Nano ESP32 pin connected to the pump the via the relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on pump 4 seconds delay(4000); digitalWrite(RELAY_PIN, LOW); // turn off pump 4 seconds delay(4000); }

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • See the pump's state

Line-by-line Code Explanation

The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!

How to Control Pump Flow with MOSFET Module

Now let's run the same pump with a MOSFET module, so the Nano ESP32 can make the flow stronger or weaker.

Wiring Diagram between ESP32, MOSFET Module and Pump

The wiring diagram between Arduino Nano ESP32 and pump MOSFET module

This image is created using Fritzing. Click to enlarge image

Connections:

  • D9 of the Nano ESP32 goes to the module's PWM pin, and GND of the Nano ESP32 goes to the module's GND pin.
  • The 12V adapter + and the pump red wire (+) both go into the + terminal.
  • The pump black wire (−) goes into the LOAD terminal.
  • The 12V adapter − goes into the − terminal.
  • Add a flyback diode (1N4007 or 1N5819) across the pump: stripe (cathode) to pump +, the other end to pump −. The pump motor is a coil, and the module has no diode on board.

Arduino Nano ESP32 - Pump Flow Code

We use the code from the MOSFET module tutorial without any change. The pump runs at full flow for 2 seconds and stops for 2 seconds. After that, the flow goes up from 10% to 100% and then down to 0%.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-pump */ const int MOSFET_PIN = D9; // Arduino Nano ESP32 pin connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

Detailed Instructions

  • If needed, set up the Arduino IDE first, as shown in the relay Quick Steps.
  • Put the pump in water. Do not run it dry.
  • Wire the parts as in the diagram above, with the 12V adapter unplugged.
  • Upload the code to the Arduino Nano ESP32.
  • Plug in the 12V adapter and open the Serial Monitor at 9600 baud.
  • Look at the water from the tube: it flows hard, stops, then grows and gets weak again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
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%
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Remember: the MOSFET module is for DC pumps only, with a pump supply of about 6V or higher. For an AC pump, keep using 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!