In this guide, we will explore how you can use an Arduino to control a pump. Whether you're creating a cooling system, designing a DIY water fountain, or constructing an automatic plant-watering setup, this tutorial will provide all the know-how you need to get started. We'll cover two commonly used pump types you can integrate into your projects, how to wire these pumps using a motor driver, and how to write Arduino code to effectively control the pump via the motor driver.
We also have a version of this guide for the Raspberry Pi Pico and MicroPython if you want to check that out. |
One quick note before we dive in: We'll be working with liquids (including conductive ones) close to electronic components, so please stay extra cautious, arrange your workspace carefully to minimize any potential spills, and have a clear plan ready in case spills do happen. Let's jump in!
Should I use a Peristaltic or Submersible Pump?
There are a lot of different types of pumps out there, but for most maker projects you'll typically be looking at two main contenders – the peristaltic and the submersible pump. Each has their own strengths and weaknesses that make them suitable for different applications, so let's dive into both so you can decide which is right for your project.
Submersible Pumps
As the name suggests, submersible pumps are designed to be placed directly inside the liquid you want to pump. Once submerged and powered, they start moving water immediately. If you've ever looked at an aquarium filter or a water fountain, chances are it's using something similar to this - typically with an impeller inside that is spun by a DC motor that pushes water through.
These pumps are incredibly popular for a few good reasons:
- They're relatively inexpensive
- Simpler to use
- Often compact and light-weight
- High flow rate (they can move a lot of liquid quickly
Some numbers for comparison, the submersible pump we will be using in this guide is on the smaller side with a flow rate of about 1.5 litres (0.4 gallons) per minute. However, you can find much larger ones that can pump more water with more pressure. Just note that the bigger pump will require more power and possibly a bigger motor driver.
However, submersible pumps do come with some notable downsides
- They must be submerged to function properly - Even if the inlet is placed into water, the pump will not be able to pull that water upwards as the pump itself needs to be full of water to operate. This also means that if any air gets into your pump, it may stop it from working and you may need to give it a little shake to get the air out.
- They're designed to be cooled by the surrounding water - This could be a positive or negative depending on how you look at it, but the liquid that flows through the pump actively cools down the motor and prevents it from overheating. However, if you run the pump out of water (or not in some other sort of liquid), or air gets into the pump, it won't be able to cool itself and will burn out if it runs for too long. Personally I once had a pond pump die on me because the water level got too low and it ran on air for a whole day.
- Unidirectional flow - Most submersible pumps can only pump water in 1 direction. Even if you reverse the polarity of the power being supplied to the motor, the water will still pump in the same direction
- Designed for low-viscosity liquids - These pumps are often designed for low-viscosity (runny or not very thick) fluids like water. If you tried to pump thicker oils or something like maple syrup, you might get no flow.
Peristaltic Pumps
Peristaltic pumps work on a really unique principle. Imagine that had a soft tube full of liquid. If you pinched the tube with your fingers and pulled along it, you would force the water inside to flow - like how you would squeeze out a freeze pop, or pop-ice, or whatever you call them in your part of the world (or as we call them in Australia, a Zooper Dooper!). This is essentially how the peristaltic pump works, except it uses rollers spinning continuously around the tube, usually being driven by a DC motor - Wikipedia has a great GIF of it.
Let's start with the downsides of peristaltic pumps:
- They are more expensive
- Typically not water-proof
- Have a much lower flow rate
Some numbers for comparison on that last one, while our submersible pump can move 1.5 litres (0.4 gallons) a minute, our peristaltic can only pump 50 millilitres (1.7 oz) a minute.
Remember though, both of these pumps have their own unique applications and uses so let's look at what the peristaltic pump gives you for these downsides:
- Accurate and consistent pumping - Every rotation of the motor squeezes out nearly the same amount of liquid. This means that if you ran the motor for 10 seconds, and then ran it for another 10 seconds, you'll get almost the same amount of liquid each time. This makes them great for applications requiring a measured and precise amount of liquid to be pumped.
- Non-contacting operation - The mechanisms of the motor don't actually touch the liquid being pumped through. The fluid enters the motor in a tube, it is squeezed through that tube and exits out that tube. Unlike a submersible, the liquid doesn't touch any metal blades or unknown plastics that could react with your liquid. This makes them more ideal for food-safe applications or working with reactive chemicals.
- Self-priming - The peristaltic mechanism allows it to not only pump liquids, but air as well (although not efficiently, it would not make for a great air pump). This means that the inlet hose of the pump can be dipped into water (even if the hose is full of air), and when turned on, the pump will suck up water as if it were drinking out of a straw. This is known as self-priming.
- Reversible flow - You run the motor in one direction and it pumps one way, you reverse the motor's direction, and it pumps the other way - really straightforward.
So, if you're looking for a more affordable pump with a high flow rate for applications like water fountains, the submersible is probably your best bet.
On the other hand, if your project needs precise liquid measurement, food-safe operation, or the ability to pump in both directions, then a peristaltic pump would be the way to go.
Now that you have a better idea of which pump might suit your project best, let's move on to using them!
What You Will Need
To follow along with this guide, you'll need a few things. The components listed here are all suitable for each other but feel free to change it up and use a bigger or smaller motor or whatever your project calls for. The process will largely be the same but just keep in mind that you might need to adjust your setup depending on what you choose.
- Pump - This guide will be using these peristaltic and submersible pumps. The wiring and code used for each of these will be the same.
- Motor Driver - We will be using the Makerverse Motor Driver as it is suitable for our motors. The submersible pump uses up to 220 mA at 3-6 volts and the peristaltic uses up to 1.5 Amps at up to 5 volts - both of these are happy run off the motor driver which can supply up to 1.6 amps at 3-16 volts. If you use a more powerful hungry motor, you may need to look for a motor driver that can handle its voltage and current.
- Arduino: We are using the Uno R4, but this will work with the older Uno R3 or any Arduino Uno. With a bit of effort, this can also be adapted to work with an ESP32.
- Tubing - You will also need some tubing to connect to your pumps. The submersible pump we have linked comes with 1 meter (3 ft) of tubing (it is 6 mm internal diameter if you need more). The peristaltic doesn't come with any and will need some 3 mm internal diameter tubing.
- Power Supply - You will need a power supply capable of supplying the current and voltage required for your motor. The peristaltic we will be using in this guide will be able to run off the Arduino's 5 Volt pin as it can supply up to 500 mA. The peristaltic pump will need more power than this pin can provide so you will need an external supply so we will be using this USB C breakout board.
- Jumper Wires
Connecting the Motor Driver and Pump
Before we start making our connections, we will first need to solder a few components. Chances are your motor driver will need its terminals to be soldered on, and your pump will need some wires attached to its terminals. If you are using a submersible pump it should already have waterproofed wires attached to it.
If this is your first time soldering, it is a lot easier than it looks and we have a guide to get you started! Just a quick tip, when soldering the wires to your pump's motor try to avoid holding the iron to it for any longer than necessary. The motor has magnets on the inside which will be damaged by heat. Don't fret about this too much, it will take a lot of heat to damage them, just don't hold the iron down on it for several minutes. As always, ensure that the wires you connect to your motor are rated to handle its maximum current. We used some 22AWG silicon wire which is good for a few amps.
Once you have your motor driver soldered up and some wires soldered to your pump's terminals (ideally wrapped with heat shrink), we can get started connecting everything.
To control the motor driver with the Arduino we will need two pins; one to control the direction of the motor and another to control the speed. The speed pin will need to be a PWM-capable pin of the Arduino. You can use any combination of pins that you like but for this guide, we will be using pins 2 and 3.
Connect pin 2 of the Arduino to the DIR B pin of the motor driver. Then connect pin 3 of the Arduino to the PWM B pin of the driver. This motor driver is capable of controlling 2 motors at once if your project calls for it. To add another motor connect another 2 pins to the A-channel of the driver, and ensure your power supply can handle the extra current of a second motor.
We will also connect the ground pin of the Arduino to the ground pin of the motor driver. This ensures they can both agree on what 0 Volts/ground is.
Now let's connect our motor to the driver board. Using a flat-head screwdriver, secure the wires of the motor to the screw terminals of the motor driver. These do not need to be extremely tight, just nip them up tight enough to hold the wire in place. Extensive overtightening can break the terminals. Also, ensure you match the polarity of the motor to that on the driver board. Don't worry if you get this wrong as the motor will just move in reverse. You can swap back the terminals to fix this, or just change it in code.
For this first example, we will be using the submersible pump and powering it off the Arduino. In the next section, we will demonstrate how to use the peristaltic with an external power supply. The peristaltic pump uses just under 500 mA of current in normal operation so we may be able to power it off the Arduino. However, if we pump anything thicker than water or if anything blocks the pump, it will draw more than the 500 mA the Arduino can supply and we may damage something. This is why we will be using an external supply.
To source power from our Arduino, connect the 5 Volt power pin to the VM terminal on the driver, and ground to ground of the driver. To connect the jumper wire to the screw terminal, we cut one end off the wire and stripped the protective layer off to give us something to screw into.
One more quick tip here. The Makerverse Motor Driver comes with current limiting capabilities which can be controlled by the little screw potentiometer on the top of the board. If you look closely at the potentiometer you will see it has a flat side on the rotating part. With a screwdriver, carefully rotate that so it is facing downwards, towards the MKRVRS letters. The image on the left shows the correct position. This position should set the maximum current of the motor to about 0.55 Amps - just some nice peace of mind that you won't draw too much with your motor.
If you have a multimeter you can precisely set this to whatever maximum current you like. Measure the voltage from ground to the test point labelled 1.1 A/V. Divide that voltage by 1.1 and you will get the maximum current the board will allow.
E.g. if you rotated the potentiometer and read 1.43 volts, 1.43 / 1.1 = 1.3. The maximum current will be set to 1.3 amps. You can set this as high or low as your project needs.
Coding the Arduino and Running the Motor
Now that we have it wired up, let's jump into the Arduino IDE and code this up. Plug your Arduino into your computer, select the board and COM port, create a new sketch, and paste in the following demo code:
// Set up pin constants const int DIR_PIN = 2; // Direction control pin const int PWM_PIN = 3; // PWM pin for speed control (must be PWM capable like pin 3) void setup() { // Initialize pins pinMode(DIR_PIN, OUTPUT); } void loop() { // Set direction forward and speed to 100% digitalWrite(DIR_PIN, HIGH); analogWrite(PWM_PIN, 255); delay(5000); // 5 seconds // Set motor speed to 0% analogWrite(PWM_PIN, 0); delay(2000); // 2 seconds // Set direction reverse and speed to 100% digitalWrite(DIR_PIN, LOW); analogWrite(PWM_PIN, 255); delay(2000); // 2 seconds // Set motor speed to 0% analogWrite(PWM_PIN, 0); delay(2000); // 2 seconds }
Lets take a quick look at what this code is actually doing by breaking it down. First of all we define the pin numbers as names that we can more easily use:
// Set up pin constants const int DIR_PIN = 2; // Direction control pin const int PWM_PIN = 3; // PWM pin for speed control (must be PWM capable like pin 3)
Then we set up Pin 2 as a regular digital output in the void setup. The direction pin that this is plugged into is simply looking for a high/low signal to set the direction of the motor. We don't need to set up pin 3 in this section as our Arduino will automatically set it up and assign the correct frequencies to it when we use it later in the code. If you want to do everything by the book, you can set it up here if needed. The motor driver pin this is connected to will use the PWM we set to control the speed of the motor with 255 being the maximum speed at 0 being no speed.
void setup() { // Initialize pins pinMode(DIR_PIN, OUTPUT); }
Then we enter our forever looping while void loop. Here we start by setting pin 2 to high (or on / 5 volts, however you want to describe it), and we set the maximum possible PWM on Pin 3. This is going to drive our pump forward as fast as we can. We then delay the Arduino for 5 seconds which will run the pump for 5 seconds.
void loop() { // Set direction forward and speed to 100% digitalWrite(DIR_PIN, HIGH); analogWrite(PWM_PIN, 255); delay(5000); // 5 seconds
Then we set the PWM to 0 for 2 seconds - this will stop the pump. After that we set pin 2 to low/off, and the PWM back to maximum - this will run the motor in reverse for 2 seconds. Then we stop the motor again for a few seconds.
// Set motor speed to 0% analogWrite(PWM_PIN, 0); delay(2000); // 2 seconds // Set direction reverse and speed to 100% digitalWrite(DIR_PIN, LOW); analogWrite(PWM_PIN, 255); delay(2000); // 2 seconds // Set motor speed to 0% analogWrite(PWM_PIN, 0); delay(2000); // 2 seconds }
Run this code and your pump should start! To test ours we connected some tubing and set up some cups of water. We placed one tube into each cup, ran the code and saw that the pump was moving water from one to the other.
If you run the same code you may find a weird quirk of this pump type. As the motor runs forward for 5 seconds it will pump into the other cup. But as the motor runs in reverse for 2 seconds, it will also pump water forward into the other cup. This is due to how this pump operates, but ensure that you are pumping it forward as it will likely be less powerful running the pump in reverse.
From here you can modify this code and apply it however you need in your project.
Wiring an External Power Supply
Let's now look at using the peristaltic pump and the external power supply that it requires. You can use whatever power supply is available to you as long as it outputs 5 Volts and at least 800 mA of current. We will be using this USB C breakout board which will provide us with 5 volts when plugged into a computer or phone charger.
Connect the VBUS pin to the VM terminal of the motor driver and also the ground pin to the ground terminal of the driver.
Now we can also remove our submersible pump and attach our peristaltic. Plug in the USB C breakout board as well as the Arduino and you should be able to pump water with the exact same code as before!
This code also highlights the reversible nature of the peristaltic pump as it pumps water forward for 5 seconds, then in reverse for 2. Neither direction is faster than the other and the water will eventually make its way into 1 cup. When you initially place the hose into the cup of water, you will also see it's self-priming capabilities as it is able to suck the water up through the pump even when the lines are full of air.
One handy thing before we finish up. The Arduino can be powered off our USB C breakout board as well. To do so you will need to connect VBUS to the Vin pin on the arduino and ground to a ground pin. This can be a litttle fiddly to do but we had the best luck using the screw terminal of the motor driver like in the image on the left.
If you do make this connect, only the USB C of the breakout board needs to be powered.
And with that, we can now add a pump to our Arduino project to pump any sort of liquid needed. If you use any of this to make something cool, or you need a hand with anything we covered here, feel free to drop a comment in the forum topic for this guide below. We are all makers at Core and happy to help.
Till next time, happy making!