Arduino Project for Automatic Plant Watering System
An automatic plant watering system with
Arduino uses a soil moisture sensor to detect dryness and controls a water pump to water the plant when needed. The Arduino reads sensor data and switches the pump on or off based on moisture levels.Syntax
The basic syntax involves reading the soil moisture sensor value using analogRead(), comparing it to a threshold, and controlling a water pump using digitalWrite() on a relay or transistor pin.
analogRead(pin): Reads sensor value (0-1023).digitalWrite(pin, HIGH/LOW): Turns pump ON or OFF.pinMode(pin, INPUT/OUTPUT): Sets pin mode.delay(ms): Pauses program for given milliseconds.
arduino
const int sensorPin = A0; // Soil moisture sensor connected to analog pin A0 const int pumpPin = 7; // Pump control pin connected to digital pin 7 const int threshold = 500; // Moisture threshold value void setup() { pinMode(pumpPin, OUTPUT); // Set pump pin as output Serial.begin(9600); // Start serial communication } void loop() { int sensorValue = analogRead(sensorPin); // Read moisture level Serial.println(sensorValue); // Print value for monitoring if (sensorValue < threshold) { // If soil is dry digitalWrite(pumpPin, HIGH); // Turn pump ON } else { digitalWrite(pumpPin, LOW); // Turn pump OFF } delay(1000); // Wait 1 second before next reading }
Output
Serial Monitor shows sensor values like:
450
460
470
... Pump turns ON when value < 500, OFF otherwise.
Example
This example shows a complete working code for an automatic plant watering system using an Arduino, soil moisture sensor, and a water pump controlled via a relay module.
arduino
const int soilMoisturePin = A0; // Analog pin for soil moisture sensor const int pumpPin = 7; // Digital pin to control water pump const int drySoilThreshold = 500; // Threshold for dry soil void setup() { pinMode(pumpPin, OUTPUT); // Set pump pin as output Serial.begin(9600); // Initialize serial communication } void loop() { int moistureLevel = analogRead(soilMoisturePin); // Read moisture sensor Serial.print("Soil Moisture Level: "); Serial.println(moistureLevel); if (moistureLevel < drySoilThreshold) { digitalWrite(pumpPin, HIGH); // Turn pump ON if soil is dry Serial.println("Pump ON - Watering plant"); } else { digitalWrite(pumpPin, LOW); // Turn pump OFF if soil is wet enough Serial.println("Pump OFF - Soil is moist"); } delay(2000); // Wait 2 seconds before next reading }
Output
Soil Moisture Level: 480
Pump ON - Watering plant
Soil Moisture Level: 520
Pump OFF - Soil is moist
Soil Moisture Level: 495
Pump ON - Watering plant
Common Pitfalls
Common mistakes include:
- Using incorrect sensor pins or pump control pins.
- Not setting pin modes properly with
pinMode(). - Using a pump directly without a relay or transistor, which can damage the Arduino.
- Setting the wrong moisture threshold causing overwatering or underwatering.
- Not adding delays, causing rapid switching of the pump.
arduino
/* Wrong way: Pump connected directly to Arduino pin without relay */ const int pumpPin = 7; void setup() { pinMode(pumpPin, OUTPUT); } void loop() { digitalWrite(pumpPin, HIGH); // This can damage Arduino delay(1000); digitalWrite(pumpPin, LOW); delay(1000); } /* Right way: Use a relay module or transistor to control pump safely */ const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); } void loop() { digitalWrite(relayPin, LOW); // Relay switches pump ON safely (assuming active LOW relay) delay(1000); digitalWrite(relayPin, HIGH); delay(1000); }
Quick Reference
- Sensor Pin: Connect soil moisture sensor analog output to Arduino analog pin (e.g., A0).
- Pump Control: Use a relay or transistor connected to a digital pin (e.g., 7) to switch the water pump.
- Threshold: Adjust moisture threshold based on your sensor readings to detect dry soil.
- Serial Monitor: Use
Serial.println()to monitor sensor values for calibration. - Delay: Add delay between readings to avoid rapid pump switching.
Key Takeaways
Use a soil moisture sensor to detect when the plant needs water.
Control the water pump safely with a relay or transistor, never directly from Arduino pins.
Set a moisture threshold to decide when to turn the pump on or off.
Use serial output to monitor sensor readings and adjust thresholds.
Add delays to prevent the pump from switching on and off too quickly.