Relay and water pump with Arduino
A small water pump is a real “actuator”: when we switch it on, something actually happens – water flows. The Arduino cannot run it directly, because its pin gives only 5 V and far too little current. That is why we put a relay between the Arduino and the pump – a switch controlled by the Arduino, through which the pump current flows. This lesson prepares you for automatic garden watering.
Why the pump cannot go directly on a pin
- A digital pin of the Arduino Uno gives 5 V and safely about 20 mA (the absolute maximum is 40 mA).
- A small 3–6 V submersible pump draws about 100–200 mA, and even more at startup.
- A pump is a motor – when it switches off, it creates a short reverse voltage that can damage electronics.
A relay solves all three problems: the Arduino only “presses the switch”, and the pump gets current from its own power supply (batteries or an adapter).
How a relay module works
A relay is an electromechanical switch: when current flows through its coil (an electromagnet), the coil pulls a metal plate that connects the contacts. You hear a quiet “click”. A relay module for Arduino already has everything needed: a transistor, a protection diode for the coil and an LED that shows the state.
| Terminal | Side | Purpose |
|---|---|---|
| VCC | control | module supply (5 V from the Arduino) |
| GND | control | ground, shared with the Arduino |
| IN | control | signal from a digital pin (D7 in the example) |
| COM | pump | common (moving) contact |
| NO | pump | normally open – connects to COM when the relay is on |
| NC | pump | normally closed – connected to COM when the relay is off |
LOW – this is called LOW-trigger. Others switch on with HIGH. That is why the program keeps the “on” and “off” states in two constants, so you can easily swap them if your module works the other way round. If the pump runs when it should be idle – swap them.Wiring diagram
The pump is connected to the NO contact, so it is off while the relay is idle (and when the Arduino is not powered). We switch the plus (+) line of the pump supply.
ARDUINO RELAY MODULE
5V ---------------> VCC
GND ---------------> GND
D7 ---------------> IN
BATTERY 4×AA (6 V) RELAY PUMP
+ ----------------> COM
NO ---------> pump +
- -----------------------------> pump -
- The pump has its own power supply (4 AA batteries or a 5–6 V adapter). Do not power it from the Arduino 5V pin – the motor creates noise and the Arduino may reset.
- The relay contacts are electrically separated from the control side, so the minus of the pump battery does not have to be connected to the Arduino GND.
- Recommended: a 1N4007 diode in parallel with the pump (the diode ring towards pump +) absorbs the voltage the motor creates when it switches off and protects the relay contacts.
Program 1: the pump from the Serial Monitor
Open the Serial Monitor (115200 baud) and send 1 or 0. Test the pump in a container of water – many small pumps must not run dry.
// Controlling a pump through a relay – commands from the Serial Monitor
const int RELAY_PIN = 7; // digital pin connected to the IN pin of the relay module
// Many cheap relay modules switch on with LOW (LOW-trigger).
// If your module switches on with HIGH, swap HIGH and LOW in these two lines.
const int RELAY_ON = LOW;
const int RELAY_OFF = HIGH;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, RELAY_OFF); // the pump is off at the start
Serial.println("Send 1 to switch the pump on, 0 to switch it off.");
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == '1') {
digitalWrite(RELAY_PIN, RELAY_ON);
Serial.println("Pump ON");
} else if (command == '0') {
digitalWrite(RELAY_PIN, RELAY_OFF);
Serial.println("Pump OFF");
}
}
}
Serial.read() reads one character at a time. The newline character the Serial Monitor sends after the number is neither '1' nor '0', so the program simply skips it.
Program 2: a button switches the pump on and off
The button is connected between pin D2 and GND. With INPUT_PULLUP the pin is HIGH while the button is not pressed and LOW when it is – no external resistor is needed.
// A button switches the pump on and off (each press changes the state)
const int RELAY_PIN = 7;
const int BUTTON_PIN = 2; // button between pin 2 and GND
const int RELAY_ON = LOW; // for a HIGH-trigger module swap LOW and HIGH
const int RELAY_OFF = HIGH;
bool pumpOn = false;
int previousState = HIGH; // with INPUT_PULLUP a released button reads HIGH
void setup() {
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // internal resistor, no external one needed
digitalWrite(RELAY_PIN, RELAY_OFF);
}
void loop() {
int state = digitalRead(BUTTON_PIN);
// react only at the moment of pressing (change from HIGH to LOW)
if (previousState == HIGH && state == LOW) {
pumpOn = !pumpOn;
digitalWrite(RELAY_PIN, pumpOn ? RELAY_ON : RELAY_OFF);
delay(50); // short pause against button contact bounce
}
previousState = state;
}
The program remembers the previous button state and reacts only to the change from HIGH to LOW. If we only checked “is it pressed”, the pump would switch on and off many times while we hold the button.
Safety
- Water and electronics do not go together: keep the Arduino, the relay and the batteries above and away from the water container, and fix the tube so it does not spray.
- In these exercises we use only batteries and adapters up to 12 V. Never connect the relay to 230 V mains – that is a job for an electrician.
- The pump must not run without water for long – it overheats.
Instead of a relay you can also use a MOSFET transistor (e.g. IRLZ44N): it is silent, has no moving parts and allows PWM speed control of the pump. A relay is still simpler to start with and clearly separates the two circuits.
Practice tasks
- Add an LED on pin 13 that is on while the pump runs.
- Change program 1 so that the command
5switches the pump on for exactly 5 seconds and then switches it off by itself. - With a stopwatch, measure how many milliliters of water the pump moves in 10 seconds. How many seconds must it run to water 100 ml?
Check your knowledge
Open the quiz
Quiz: relay and pump
1. Why is the pump not connected directly to an Arduino pin?
2. Which relay contact is the pump connected to so that it is off while the relay is idle?
3. What does it mean that a relay module is “LOW-trigger”?
4. What is INPUT_PULLUP for with a button?
