โ Back to Course
Automation with Relays - Part 2
๐ ๏ธ Automation with Relays - Part 2: Practical Implementation
๐ฏ What You'll Learn:
- โ๏ธ Build an automatic irrigation system that waters only when needed
- ๐ Wire relay modules safely to control pumps and valves
- ๐ Set custom soil moisture thresholds for different crops
- ๐ง Save 40-60% water compared to timer-based irrigation
๐ Components You Need
- ๐น ESP32 microcontroller โ $8 ยท Brain of the system
- ๐น 1-Channel Relay Module โ $5 ยท Controls pump on/off
- ๐น Soil Moisture Sensor โ $8 ยท Measures soil water content
- ๐น 12V Water Pump โ $10 ยท Pumps water to crops
- ๐น Water tubing & fittings โ $5 ยท Distribute water to plants
- ๐น 12V Power Supply โ $8 ยท Powers pump and ESP32
๐ก Total System Cost: ~$44
Pays for itself in 2-3 months through water savings! A typical 1-acre farm saves $120-180/year on water bills.
๐ Wiring Diagram
ESP32 โ Relay Module
GPIO26 โ IN pin (control signal)
3.3V โ VCC (relay power)
GND โ GND
Relay โ Water Pump (12V circuit)
COM โ Pump positive (+)
NO โ 12V power supply positive (+)
Pump negative โ 12V power supply negative (-)
ESP32 โ Soil Moisture Sensor
GPIO34 (ADC) โ AO (analog out)
3.3V โ VCC
GND โ GND
โ ๏ธ CRITICAL SAFETY WARNING:
Never connect the pump directly to ESP32 pins! The pump draws 200-500mA, which will destroy your ESP32. ALWAYS use a relay module to isolate high-power circuits.
๐ Complete Automation Code
#include <WiFi.h>
#define RELAY_PIN 26
#define SOIL_PIN 34
const int DRY_THRESHOLD = 30; // Water when below 30%
const int WET_THRESHOLD = 70; // Stop watering at 70%
const unsigned long PUMP_RUN_TIME = 300000; // 5 minutes
const unsigned long CHECK_INTERVAL = 3600000; // Check every hour
unsigned long lastCheck = 0;
bool pumpRunning = false;
unsigned long pumpStartTime = 0;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay HIGH = Pump OFF (active low)
Serial.println("๐ฑ Auto Irrigation System Started");
}
int getSoilMoisturePercent() {
int raw = analogRead(SOIL_PIN);
// Sensor dry: 4095, Sensor wet: 1500 (adjust based on your soil)
int percent = map(raw, 4095, 1500, 0, 100);
percent = constrain(percent, 0, 100);
return percent;
}
void startPump() {
digitalWrite(RELAY_PIN, LOW); // Pump ON
pumpRunning = true;
pumpStartTime = millis();
Serial.println("๐ง PUMP ON - Watering crops");
}
void stopPump() {
digitalWrite(RELAY_PIN, HIGH); // Pump OFF
pumpRunning = false;
Serial.println("๐ง PUMP OFF - Watering complete");
}
void loop() {
unsigned long now = millis();
// Handle active pump timer
if (pumpRunning && (now - pumpStartTime >= PUMP_RUN_TIME)) {
stopPump();
}
// Check soil moisture every hour
if (!pumpRunning && (now - lastCheck >= CHECK_INTERVAL)) {
lastCheck = now;
int moisture = getSoilMoisturePercent();
Serial.printf("๐ Soil moisture: %d%%\n", moisture);
if (moisture < DRY_THRESHOLD) {
Serial.println("โ ๏ธ Soil too dry! Starting pump...");
startPump();
} else if (moisture > WET_THRESHOLD) {
Serial.println("โ
Soil moisture is good. No watering needed.");
} else {
Serial.printf("๐ฟ Soil moisture optimal (%d%%). No action needed.\n", moisture);
}
}
}
๐ก Adjusting Moisture Thresholds by Crop:
- Tomatoes: Water at 25% dry, stop at 65%
- Maize/Corn: Water at 20% dry, stop at 60%
- Leafy greens: Water at 35% dry, stop at 75%
- Succulents/Drought crops: Water at 15% dry, stop at 50%
๐ Case Study โ Smart Irrigation Saves 55% Water, Kenya:
A tomato farmer replaced timer-based irrigation with soil moisture sensors:
- ๐ง Water savings: 55% reduction (from 8,000L to 3,600L/week)
- ๐ฐ Cost savings: $65/month on water bills
- ๐ Yield increase: 28% better tomato production (no over/under watering)
- โก Power: Solar-powered ESP32 runs 6+ months
"My tomatoes never looked better. The system paid for itself in 6 weeks!" โ Farmer, Kiambu County
๐ Advanced Features to Add:
- ๐ฑ Remote monitoring: Add WiFi to see moisture from your phone
- ๐ง๏ธ Rain delay: Add a rain sensor to skip watering
- โฐ Time restrictions: Water only in morning/evening (prevents evaporation)
- ๐ Data logging: Track moisture trends over weeks
๐ฏ Key Takeaways:
- โ Relay modules protect your ESP32 from high-power devices (pumps, valves, lights)
- โ Soil moisture sensors save 40-60% water compared to timers
- โ Different crops need different moisture thresholds โ adjust for your farm
- โ Always add a max run time (fail-safe) to prevent flooding
- โ This $44 system pays for itself in 2-3 months through water savings
๐ก Key Takeaways:
- Apply these concepts directly to your farm or project.
- Take notes on important details for the quiz.
- Use the button below to track your progress.
×