Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this post, we will learn how to make an automatic plant watering system using a capacitive soil moisture sensor.
The capacitive soil moisture sensor is widely used for measuring the volumetric water content of soil. Unlike traditional resistive sensors, which rely on electrical resistance and are prone to corrosion over time, the capacitive sensor operates on the principle of capacitance, offering better durability, stability, and accuracy in long-term soil monitoring applications.
For more information, visit Getting to Know Your Arduino Uno: A Beginner’s Guide to Its Components.
Visit Tutorial | How to Make your own 5v USB power supply CABLE.
A capacitive soil moisture sensor measures the volumetric water content of soil using the dielectric properties of the soil.
The sensor circuit is typically built around a 555 timer IC, which is used to monitor how fast a capacitor charges through a resistor. The sensing element comprises two PCB traces acting as capacitor plates, with their capacitance varying according to the moisture content in the surrounding soil.
When the sensor is inserted into the soil, the moisture content alters the dielectric constant, which in turn changes the capacitance. This affects the oscillation frequency generated by the 555 timer. The frequency shift is then converted into an analog voltage, typically ranging from 1 to 3 volts, which can be read by a microcontroller such as an Arduino. The drier the soil, the higher the output voltage, and vice versa.
The Arduino’s analog input pin is connected to its built-in ADC. The ADC takes the continuous analog voltage from the sensor and converts it into a digital integer value.
Relays are used to control high-power devices with low-power signals, often from a microcontroller like an Arduino or Raspberry Pi. Trigger level refers to the logic voltage level (HIGH or LOW) required to activate the relay. You can determine the trigger type by checking the relay module’s datasheet or documentation.
For further information, visit An Essential Introduction to DC Relays in Arduino Projects: What They Are and How They Work.
In this project, we use a LOW-level trigger relay in normally open (NO) mode. By default, the circuit remains open. When the control pin receives a LOW signal (0V), the relay is activated, closing the NO contact and turning on the water pump.
1. Sensor Reads Moisture
The soil moisture sensor is placed in the soil near the plant roots. It measures the analog voltage that corresponds to how dry or wet the soil is.
2. Arduino Processes Data
The Arduino reads the analog signal from the sensor. The ADC takes the continuous analog voltage from the sensor and converts it into a digital integer value. If the value is above the threshold (dry), it means the plant needs water.
3. Pump is Activated
If the soil is dry, Arduino sends a signal to the relay. In this project, a LOW signal is sent to the relay. The relay turns on the water pump. The pump draws water from the reservoir and delivers it to the plant.
4. Pump is Turned Off
When moisture reaches the desired level, Arduino turns off the pump.
| Moisture Sensor | Arduino | Breadboard |
| GND | – | |
| VCC | + | |
| AUOT | A0 |
| LCD | Arduino | Breadboard |
| GND | – | |
| VCC | + | |
| SDA | A4 | |
| SCL | A5 |
A4/SDA and A5/SCL are Arduino I2C Pins. The I2C protocol uses two lines to send and receive data: a serial data (SDA) line and a serial clock (SCL) line. With I2C, we can connect multiple devices only using two wires. SCL is used to synchronously clock data. SDA is used to transmit data to or from target devices. The clock signal acts as a metronome. It synchronizes the transfer of serial data, indicating to the devices the correct time to capture each bit. Visit Inter-Integrated Circuit (I2C) Protocol for more information.
Connect the tube to the water pump.
The USB adapter supplies power to the water pump. The ground wire (black) from the USB cable and the negative wire (black) from the water pump are connected together. The pump’s positive wire (red) is connected to the NO (Normally Open) terminal of the relay, while the power wire (red) from the USB cable is connected to the COM (Common) terminal.
| Relay | Arduino | Breadboard |
| GND | – | |
| VCC | + | |
| IN | D2 |
| Arduino | Breadboard |
| GND | – |
| 5V | + |
Connect the Arduino to your computer using a USB cable. Upload the program to the Arduino through the Arduino IDE software. Visit Getting Started with Arduino IDE: A Beginner’s Guide to Coding and Creating for more information.
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
//Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define relayPin 2 //Digital pin connected to the relay
#define sensorPin A0 //Analog pin connected to the moisture sensor
int value;
int pct;
void setup() {
Serial.begin(9600);
//Initialize the LCD
lcd.init();
// Turn on the backlight
lcd.backlight();
pinMode(relayPin, OUTPUT); //Relay is an output device
pinMode(sensorPin, INPUT); //Moisture sensor is an input device
delay(2000);
}
void loop() {
value = analogRead(sensorPin); //Read value from moisture sensor
pct = map(value, 275, 590, 100, 0); //Map value to percentage (0-100%)
Serial.print("value:");
Serial.print(value);
Serial.print("\t | ");
if(value>380){
//Relay is low level trigger relay so we need to write LOW to turn the pump on.
digitalWrite(relayPin, LOW);
Serial.println("No moisture, Soil is dry");
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(pct);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Soil is dry.");
}
else{
digitalWrite(relayPin, HIGH);
Serial.println("Soil is wet");
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(pct);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Soil is wet.");
}
delay(1000);
}