â Back to Course
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.