โ Back to Course
Practical Soil Moisture Sensors
๐ Connecting a Soil Moisture Sensor
๐ง What You'll Learn:
- ๐ Wire a soil moisture sensor to ESP32/ESP8266
- ๐ Read raw analog values (0-4095 on ESP32, 0-1023 on ESP8266)
- ๐ Convert raw readings to moisture percentage (0-100%)
- ๐ฑ Calibrate sensor for your specific soil type
โ ๏ธ Capacitive vs Resistive Sensors:
Capacitive sensors (recommended): Last 3-5 years, no corrosion, $8-10
Resistive sensors (avoid): Corrode in 2-4 weeks, $2-3 โ false economy!
Always buy capacitive soil moisture sensors for long-term farm use.
๐ ๏ธ What You Need
-
๐น Capacitive Soil Moisture Sensor
$8-10 ยท Recommended (lasts 3-5 years, no corrosion) -
๐น Resistive Sensor (not recommended)
$2-3 ยท Avoid (corrodes in 2-4 weeks) -
๐น ESP32 or ESP8266 board
$8-12 ยท Any ADC pin works -
๐น 3 jumper wires (female-to-female)
$2 ยท For connections
๐ Wiring Connection
Soil Moisture Sensor โ ESP32/ESP8266
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
VCC (Red wire) โ 3.3V or 5V pin
GND (Black wire) โ GND pin
AO (Analog Out) โ ADC pin (GPIO34 on ESP32, A0 on ESP8266)
ESP32 Analog Pins: GPIO32, GPIO33, GPIO34, GPIO35, GPIO36, GPIO39
ESP8266 Analog: Only A0 pin!
๐ก ESP32 vs ESP8266 ADC Notes:
- ESP32: 12-bit ADC โ values 0 to 4095 (more precise)
- ESP8266: 10-bit ADC โ values 0 to 1023 (only A0 pin)
- ESP32 advantage: Better resolution and multiple ADC pins
๐ Reading the Raw Value
#include <WiFi.h>
#define SOIL_SENSOR_PIN 34
void setup() {
Serial.begin(115200);
pinMode(SOIL_SENSOR_PIN, INPUT);
Serial.println("๐ฑ Soil Moisture Sensor Ready");
Serial.println("=================================");
}
void loop() {
int rawValue = analogRead(SOIL_SENSOR_PIN);
Serial.printf("๐ Raw sensor value: %d\n", rawValue);
// ESP32: 0-4095 range
if (rawValue < 500) {
Serial.println("๐ง VERY WET - Soil is saturated");
} else if (rawValue < 1500) {
Serial.println("๐ WET - Good moisture level");
} else if (rawValue < 2500) {
Serial.println("๐ฟ MOIST - Getting dry");
} else if (rawValue < 3500) {
Serial.println("โ ๏ธ DRY - Water soon!");
} else {
Serial.println("๐ฅ VERY DRY - Water immediately!");
}
delay(5000); // Read every 5 seconds
}
๐ Understanding Raw Values (ESP32):
- Raw value 4095 = Very dry (sensor in air) โ HIGHEST reading
- Raw value 3000-3500 = Dry soil โ needs water soon
- Raw value 2000-2500 = Moderate moisture โ good for most crops
- Raw value 1000-1500 = Wet soil โ ideal after watering
- Raw value 0-500 = Very wet (sensor in water) โ LOWEST reading
โ ๏ธ Note: Higher value = DRYER soil. Lower value = WETTER soil. This is opposite of what most people expect!
๐ Calibration Formula (Convert to Percentage)
// Complete calibration code
#define SOIL_SENSOR_PIN 34
int dryValue = 4095; // Measure in DRY air (sensor not in soil)
int wetValue = 1500; // Measure in WET soil (after watering)
int getMoisturePercent() {
int raw = analogRead(SOIL_SENSOR_PIN);
// Formula: Convert raw to percentage
// Higher raw = drier, so we invert the formula
int percent = map(raw, dryValue, wetValue, 0, 100);
percent = constrain(percent, 0, 100);
return percent;
}
void setup() {
Serial.begin(115200);
Serial.println("๐ฑ Calibrated Soil Moisture Sensor");
Serial.println("=================================");
}
void loop() {
int moisture = getMoisturePercent();
Serial.printf("๐ง Soil Moisture: %d%%\n", moisture);
if (moisture < 30) {
Serial.println("โ ๏ธ CRITICAL - Water immediately!");
} else if (moisture < 50) {
Serial.println("๐ฟ Dry - Plan to water soon");
} else if (moisture < 70) {
Serial.println("โ
Good moisture level");
} else {
Serial.println("๐ Very wet - Reduce watering");
}
delay(60000); // Read every minute
}
๐ How to Calibrate Your Sensor:
- Step 1 - Find DRY value: Hold sensor in AIR (not touching anything) โ record reading (~4095)
- Step 2 - Find WET value: Submerge sensor in a glass of WATER โ record reading (~1500)
- Step 3 - Update code: Replace dryValue and wetValue with YOUR readings
- Step 4 - Test in soil: Take a reading in moist soil โ should be ~50-70%
โ ๏ธ Important Notes:
- The sensor measures electrical conductivity, not actual water content
- Different soil types (sandy vs clay) give different readings โ calibrate for YOUR soil!
- Salt/fertilizer in soil affects readings โ calibrate after fertilizing
- Resistive sensors corrode โ buy capacitive sensors for long-term use
- Insert sensor at 5-10cm depth (active root zone)
๐ Case Study โ Capacitive Sensor Lasts 3+ Years, Kenya:
A farmer initially bought cheap resistive sensors ($3 each):
- โ Result: Sensors failed after 3 weeks (corrosion)
- ๐ฐ Cost: Bought 4 replacements in 3 months ($12 total)
- โ Switched to capacitive ($10): Still working after 3 years!
- ๐ง Water savings: 45% reduction with reliable data
- ๐ ROI: $10 sensor paid for itself in 2 months
"I learned the hard way. Cheap resistive sensors cost more in the long run. Buy capacitive once and forget about it." โ Farmer, Nakuru County
๐ Quick Reference โ Sensor Values to Percentage:
- Formula:
moisture% = map(raw, dryValue, wetValue, 0, 100) - ESP32 example: Raw 4095 (dry air) โ 0%, Raw 1500 (wet) โ 100%
- ESP8266 example: Raw 1023 (dry air) โ 0%, Raw 400 (wet) โ 100%
- Adjust thresholds: Sandy soil needs lower thresholds (drains faster)
๐ฏ Key Takeaways:
- โ Always buy capacitive sensors โ they last 3-5 years, resistive corrodes in weeks
- โ ESP32 gives 0-4095 range (12-bit), ESP8266 gives 0-1023 (10-bit) on A0 only
- โ Higher raw value = DRIER soil. Lower raw value = WETTER soil (opposite of intuition!)
- โ Calibrate for YOUR soil: measure dry air and wet water, then use map() formula
- โ Different soil types (sandy/clay) need different moisture thresholds
- โ Insert sensor at 5-10cm depth (active root zone) for accurate readings
- โ A $10 capacitive sensor pays for itself in 1-2 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.
×