OC
OceanRemote
Low-code IoT platform
← Back to Course

Practical Soil Temperature

Practical Soil Temperature

🔌 Measuring Soil Temperature with DS18B20

🌡️ What You'll Learn:

  • 🔌 Wire a waterproof DS18B20 temperature sensor for soil monitoring
  • 📊 Read accurate soil temperature (Âą0.5°C) at root depth
  • 🌱 Use soil temperature data to optimize planting and irrigation
  • 💰 Prevent crop loss by avoiding planting in cold soil

🛠️ Why DS18B20 is Best for Soil

  • ✅ Waterproof probe
    Can be buried directly in soil — no special enclosure needed!
  • ✅ Digital output
    No calibration required — reads accurate temperature immediately
  • ✅ Accurate to Âą0.5°C
    Range: -55°C to +125°C (works in all farming climates)
  • ✅ Multiple sensors on one wire
    One GPIO pin can read 10+ sensors (OneWire protocol)
  • ✅ Very affordable
    Only $4-6 per sensor — monitor multiple zones cheaply
💡 DS18B20 vs Other Soil Temperature Sensors:
  • DS18B20 ($5): Waterproof, digital, Âą0.5°C accuracy — BEST FOR SOIL
  • LM35 ($3): Analog, NOT waterproof — needs enclosure, less accurate
  • Thermocouple ($10): Very wide range, but needs amplifier — overkill for soil
  • Our choice: DS18B20 is the perfect balance of price, accuracy, and durability

🔗 Wiring the DS18B20

DS18B20 Waterproof Sensor (colors may vary):
═══════════════════════════════════════════════════════════════
Red wire (VDD)     → ESP32 3.3V (or 5V for ESP8266)
Black wire (GND)   → ESP32 GND
Yellow/White (DATA)→ ESP32 GPIO4

⚠️ CRITICAL: 4.7kΩ pull-up resistor required!
   Connect between DATA (GPIO4) and 3.3V

Diagram:
       4.7kΊ
GPIO4 ──█/\/\/\█── 3.3V
   │
   └── DS18B20 DATA (yellow)

Multiple sensors: Connect all DATA wires to same GPIO4!
    
⚠️ CRITICAL: Pull-Up Resistor Required!

The DS18B20 WILL NOT WORK without a 4.7kΊ pull-up resistor between DATA and 3.3V. Some pre-built modules include this resistor, but the bare waterproof probe does NOT. Always add it!

📖 Reading Soil Temperature (Basic Code)

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
    Serial.begin(115200);
    sensors.begin();
    Serial.println("🌡️ DS18B20 Soil Temperature Sensor Ready");
    Serial.println("========================================");
}

void loop() {
    sensors.requestTemperatures();
    float soilTemp = sensors.getTempCByIndex(0);
    
    if (soilTemp == -127.0) {
        Serial.println("❌ Sensor error! Check wiring.");
    } else {
        Serial.printf("🌡️ Soil Temperature: %.1f°C\n", soilTemp);
        
        if (soilTemp < 10) {
            Serial.println("⚠️ TOO COLD! Do not plant sensitive crops.");
        } else if (soilTemp > 35) {
            Serial.println("⚠️ TOO HOT! Increase irrigation, provide shade.");
        } else {
            Serial.println("✅ Soil temperature is within good range.");
        }
    }
    
    delay(60000);
}
    

📖 Advanced Code (Multiple Sensors)

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int sensorCount = 0;

void setup() {
    Serial.begin(115200);
    sensors.begin();
    sensorCount = sensors.getDeviceCount();
    Serial.printf("🔍 Found %d DS18B20 sensor(s)\n", sensorCount);
}

void loop() {
    sensors.requestTemperatures();
    
    for (int i = 0; i < sensorCount; i++) {
        float temp = sensors.getTempCByIndex(i);
        
        if (temp != -127.0) {
            Serial.printf("🌡️ Sensor %d: %.1f°C\n", i + 1, temp);
            
            if (temp < 12) {
                Serial.printf("   Zone %d: Too cold for germination\n", i + 1);
            } else if (temp > 32) {
                Serial.printf("   Zone %d: Heat stress — water more\n", i + 1);
            } else {
                Serial.printf("   Zone %d: Optimal temperature\n", i + 1);
            }
        }
    }
    
    delay(300000);
}
    
💡 Multiple Sensors on One Wire:

You can connect 10+ DS18B20 sensors to the same GPIO pin! Each sensor has a unique 64-bit address. Perfect for monitoring different depths or zones across your farm.

📊 Interpreting Soil Temperature Data

  • ❄️ Below 10°C
    → Too cold for most crops — delay planting
    🌱 Seeds won't germinate. Roots stop growing below 8°C.
  • 🌿 10-15°C
    → Cold-tolerant crops only (wheat, onions, peas, lettuce)
    🌱 Germination is slow but possible for cold-season crops.
  • ✅ 15-25°C
    → Ideal for most vegetables (tomatoes, peppers, cucumbers)
    🌱 Optimal root growth, nutrient uptake, and microbial activity.
  • 🌞 25-35°C
    → Good for tropical crops (maize, sorghum, okra, eggplant)
    🌱 Warm-season crops thrive, but monitor moisture closely.
  • 🔥 Above 35°C
    → Heat stress — increase watering, provide shade, mulch heavily
    🌱 Root death above 40°C. Immediate action required!
🌟 Using Soil Temperature for Planting Decisions:
// Add to your monitoring code
float soilTemp = sensors.getTempCByIndex(0);

if (soilTemp < 12 && month == 3) {  // March planting season
    Serial.println("⚠️ Soil too cold! Delay planting by 2 weeks.");
} else if (soilTemp > 18 && month == 3) {
    Serial.println("✅ Soil ready for planting tomatoes and peppers.");
}

if (soilTemp > 35) {
    Serial.println("⚠️ CRITICAL: Soil overheating! Water immediately.");
}
📖 Case Study — Soil Temperature Monitoring Saves Crop, Nigeria:

A tomato farmer in Kaduna used DS18B20 sensors to monitor soil temperature:

  • 🌡️ Discovery: Soil temperature reached 42°C at 2pm (roots cooking!)
  • ✅ Action: Added mulch and increased irrigation during peak heat
  • 💧 Result: Saved entire crop from root death ($5,000 value)
  • 💰 ROI: $6 sensor saved $5,000 crop in first season

"The sensor alerted me before my plants died. Now I mulch heavily and water during peak heat." — Farmer, Kaduna State

⚠️ Common Problems & Solutions:
  • Reading -127°C: Sensor not detected — check pull-up resistor and wiring
  • Reading 85°C: Power-on default — wait 1 second after sensor request
  • Intermittent readings: Loose connection or wire too long (>10m)
  • No sensors found: Wrong pin assignment or missing pull-up resistor
  • ESP8266 note: Use GPIO4 or GPIO5 (avoid GPIO16 for OneWire)
💡 Pro Tips for Accurate Soil Temperature:
  • 📍 Bury at root depth: 10-15cm deep for most vegetables
  • 🌳 Multiple locations: Use 3-5 sensors per acre for accuracy
  • 🕐 Time of day: Check at sunrise (coolest) and 2pm (hottest)
  • 🌾 Under mulch vs bare soil: Mulch keeps soil 5-10°C cooler
  • 💧 Wet vs dry soil: Wet soil heats slower — water before heat waves
🎯 Key Takeaways:
  • ✅ DS18B20 is the best soil temperature sensor: waterproof, digital, Âą0.5°C, $4-6
  • ✅ A 4.7kΊ pull-up resistor between DATA and 3.3V is REQUIRED
  • ✅ One GPIO pin can read 10+ sensors — monitor multiple zones
  • ✅ Ideal soil temperature for most vegetables: 15-25°C
  • ✅ Below 10°C = too cold to plant. Above 35°C = heat stress risk
  • ✅ A single $6 sensor can save $5,000+ by preventing crop loss
💡 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.