Arduino - Soil Moisture Sensor Pump

In this tutorial, We are going to learn how to use the Arduino and capacitive soil moisture sensor to control the pump.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Capacitive Soil Moisture Sensor
1×Relay
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

Buy Note: Many capacitive soil moisture sensors on the market are low-quality, regardless of the version. We highly recommend buying the sensor from the DIYables brand using the link above. We tested it, and it worked well.

About soil moisture sensor and Pump

If you do not know about pump and soil moisture sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:

How It Works

Arduino periodically reads the value from the capacitive soil moisture sensor. Based on the soil moisture value, it will take the following actions:

  • If the soil moisture value is below a threshold, Arduino automatically activates a relay to turn a pump on.
  • Otherwise, Arduino automatically deactivates a relay to turn a pump off.

Wiring Diagram

Arduino soil moisture sensor Pump Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-soil-moisture-sensor-pump */ #define RELAY_PIN 2 // Arduino pin that controls the pump via relay #define MOISTURE_PIN A0 // Arduino pin that connects to AOUT pin of moisture sensor #define THRESHOLD 530 // => CHANGE YOUR THRESHOLD HERE void setup() { Serial.begin(9600); pinMode(RELAY_PIN, OUTPUT); } void loop() { int value = analogRead(MOISTURE_PIN); // read the analog value from sensor if (value > THRESHOLD) { Serial.print("The soil moisture is DRY => activate pump"); digitalWrite(RELAY_PIN, HIGH); } else { Serial.print("The soil moisture is WET => deactivate the pump"); digitalWrite(RELAY_PIN, LOW); } Serial.print(" ("); Serial.print(value); Serial.println(")"); delay(1000); }

Quick Steps

  • Do calibration to determine the wet-dry THRESHOLD, see Arduino - Calibrates Soil Moisture Sensor
  • Update the calibrated value to THRESHOLD in the code
  • Open Serial Monitor on Arduino IDE
  • Upload the code to Arduino
  • Check out the result on Serial Monitor.
COM6
Send
The soil moisture is DRY => activate the pump The soil moisture is DRY => activate the pump The soil moisture is DRY => activate the pump The soil moisture is DRY => activate the pump The soil moisture is WET => deactivate the pump The soil moisture is WET => deactivate the pump The soil moisture is WET => deactivate the pump The soil moisture is WET => deactivate the pump
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in the comment lines of the source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES