DS18B20 Waterproof Temperature Sensor
π‘οΈ DS18B20 Waterproof Temperature Sensor - Complete Guide
π¬ What You'll Learn in This Lesson:
- π‘οΈ How to measure soil, water, and air temperature accurately
- π Wire the DS18B20 sensor correctly (OneWire protocol)
- π» Write complete Arduino/ESP32 code for temperature monitoring
- π Send temperature data to OceanRemote cloud
- πΎ Make farm decisions based on temperature readings
π Why Choose DS18B20?
| Feature | DS18B20 | DHT22 | NTC Thermistor |
|---|---|---|---|
| π‘οΈ Temperature Range | -55Β°C to +125Β°C | -40Β°C to +80Β°C | -40Β°C to +125Β°C |
| π― Accuracy | Β±0.5Β°C | Β±0.5Β°C | Β±1-2Β°C |
| π§ Waterproof | β Yes (probe version) | β No | β οΈ Depends on model |
| π Cable Length | Up to 100 meters | < 20 meters | < 10 meters |
| π Digital Interface | β OneWire (single pin) | β OneWire (single pin) | β Analog (needs ADC) |
The waterproof probe version can be buried in soil, submerged in water tanks, or placed in compost piles. Perfect for monitoring soil temperature for seed germination, water temperature for aquaculture, or compost temperature for organic farming!
π Complete Wiring Diagram
DS18B20 Waterproof Probe Connections:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β DS18B20 Probe ESP32/ESP8266 β
β βββββββββββββ ββββββββββββ β
β β
β Red Wire (VDD) ββββΊ 3.3V β
β Black Wire (GND) ββββΊ GND β
β Yellow Wire (DATA) ββββΊ GPIO4 β
β β
β IMPORTANT: Add a 4.7kΞ© resistor between DATA and 3.3V! β
β β
β βββββββ β
β β β β
β β 4.7kβ β
β β β β
β ββββ¬βββ β
β β β
β 3.3VβΌβββββββββββββββββββββββ¬ββββββββββββββΊ Red Wire (VDD) β
β β β β
β βββββββββββββββββββββββββΌββββββββββββββΊ Yellow (DATA) β
β β β with 4.7k pull-up β
β β β
β GNDβΌββββββββββββββββββββββββββββββββββββΊ Black Wire (GND) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Alternative: Many DS18B20 modules include the resistor already!
The 4.7kΞ© pull-up resistor between DATA and 3.3V is MANDATORY. Without it, you will get "CRC error" or "Device not found". If your module doesn't have one built-in, add it yourself!
π Required Libraries
Install these libraries in Arduino IDE:
1. OneWire by Jim Studt
2. DallasTemperature by Miles Burton
Install via: Sketch β Include Library β Manage Libraries β Search and Install
π» Basic Arduino Code (Single Sensor)
/* * DS18B20 Waterproof Temperature Sensor - Basic Reading * Perfect for soil, water, or air temperature monitoring * * Components: * - 1x DS18B20 waterproof probe * - 1x 4.7kΞ© resistor (if not on module) * - ESP32/ESP8266 board */ #include#include // GPIO pin connected to DS18B20 DATA wire #define ONE_WIRE_BUS 4 // Setup OneWire and DallasTemperature OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(115200); Serial.println("========================================"); Serial.println("π‘οΈ DS18B20 Temperature Monitor v1.0"); Serial.println(" Waterproof Soil/Water Temperature"); Serial.println("========================================"); // Initialize the sensor sensors.begin(); // Check if sensor is connected int deviceCount = sensors.getDeviceCount(); if (deviceCount == 0) { Serial.println("β ERROR: No DS18B20 sensor found!"); Serial.println(" Check wiring and pull-up resistor!"); } else { Serial.printf("β Found %d DS18B20 sensor(s)\n", deviceCount); } } void loop() { // Request temperature from all sensors sensors.requestTemperatures(); // Read temperature in Celsius float tempC = sensors.getTempCByIndex(0); // Check for valid reading if (tempC == -127.00) { Serial.println("β Sensor read error! Check connection."); } else { // Convert to Fahrenheit (optional) float tempF = tempC * 9.0 / 5.0 + 32.0; Serial.println("ββββββββββββββββββββββββββββββββββ"); Serial.printf("π‘οΈ Temperature: %.1fΒ°C | %.1fΒ°F\n", tempC, tempF); // Agricultural recommendations based on temperature if (tempC < 10) { Serial.println("β οΈ SOIL TOO COLD! Delay planting seeds."); } else if (tempC >= 10 && tempC < 18) { Serial.println("π± Cool season crops only (lettuce, spinach, peas)"); } else if (tempC >= 18 && tempC < 25) { Serial.println("β IDEAL temperature for most vegetables!"); } else if (tempC >= 25 && tempC < 35) { Serial.println("π Warm season crops thrive (tomatoes, peppers, corn)"); } else if (tempC >= 35) { Serial.println("β οΈ SOIL TOO HOT! Increase mulching and watering."); } Serial.println("ββββββββββββββββββββββββββββββββββ"); } delay(5000); // Read every 5 seconds }
π§ Multiple Sensors on One Wire (Advanced)
/* * Multiple DS18B20 Sensors - Monitor different zones! * You can connect up to 100 sensors on the same wire! * Perfect for: different soil depths, multiple garden beds, compost piles */ #include#include #define ONE_WIRE_BUS 4 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); // Store sensor addresses DeviceAddress sensor1, sensor2, sensor3; int sensorCount = 0; void setup() { Serial.begin(115200); sensors.begin(); Serial.println("========================================"); Serial.println("πΎ Multi-Zone Temperature Monitor"); Serial.println("========================================"); // Discover all sensors sensorCount = sensors.getDeviceCount(); Serial.printf("π Found %d DS18B20 sensor(s)\n", sensorCount); // Get addresses of first 3 sensors if (sensorCount >= 1) sensors.getAddress(sensor1, 0); if (sensorCount >= 2) sensors.getAddress(sensor2, 1); if (sensorCount >= 3) sensors.getAddress(sensor3, 2); } void printTemperature(DeviceAddress deviceAddress, String location) { float tempC = sensors.getTempC(deviceAddress); if (tempC != -127.00) { Serial.printf("%s: %.1fΒ°C | %.1fΒ°F\n", location.c_str(), tempC, tempC * 9.0/5.0 + 32.0); } else { Serial.printf("%s: π΄ ERROR - No sensor detected!\n", location.c_str()); } } void loop() { sensors.requestTemperatures(); Serial.println("\nββββββββββββββββββββββββββββββββββ"); Serial.println("π ZONE TEMPERATURES:"); if (sensorCount >= 1) printTemperature(sensor1, " π± Garden Bed A"); if (sensorCount >= 2) printTemperature(sensor2, " π§ Water Tank"); if (sensorCount >= 3) printTemperature(sensor3, " πͺ΄ Compost Pile"); Serial.println("ββββββββββββββββββββββββββββββββββ\n"); delay(30000); // Read every 30 seconds }
βοΈ Send Data to OceanRemote Cloud
/* * DS18B20 with OceanRemote Cloud Integration * Monitor soil temperature from anywhere in the world! */ #include#include #include #include #define ONE_WIRE_BUS 4 // WiFi Configuration const char* ssid = "YOUR_WIFI"; const char* password = "YOUR_PASSWORD"; // OceanRemote Configuration const char* token = "YOUR_DEVICE_TOKEN"; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(115200); sensors.begin(); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nβ WiFi connected!"); Serial.println("π‘οΈ DS18B20 Cloud Monitor Ready"); } void sendToCloud(float temperature) { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin("https://api.oceanremote.net/device/state"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String data = "token=" + String(token); data += "&temperature=" + String(temperature); data += "&sensor=ds18b20"; int httpCode = http.POST(data); if (httpCode > 0) { Serial.println("β Data sent to OceanRemote"); } http.end(); } } void loop() { sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); if (tempC != -127.00) { Serial.printf("π‘οΈ Soil Temperature: %.1fΒ°C\n", tempC); sendToCloud(tempC); } else { Serial.println("β Sensor error!"); } delay(60000); // Send every minute }
A vegetable farm in Kenya installed DS18B20 sensors at 10cm, 20cm, and 30cm depths:
- π‘οΈ Problem: Seeds failing to germinate in certain areas
- π¬ Solution: Buried 3 DS18B20 sensors at different depths
- π Discovery: Top 10cm was 12Β°C (too cold), 30cm was 22Β°C (ideal)
- β Action: Planted deeper seeds and added black plastic mulch
- π Result: 85% germination rate increased from 40%
"The DS18B20 sensors showed us exactly where to plant for better germination!" - Farmer, Kenya
π§ Troubleshooting Common Issues
| Problem | Solution |
|---|---|
| β "Device not found" | Check 4.7kΞ© pull-up resistor between DATA and 3.3V |
| β Reading -127Β°C | Bad connection - check yellow DATA wire |
| β "CRC error" | Cable too long (max 100m) or electrical interference |
| β Temperature jumps around | Add a 100Β΅F capacitor between VDD and GND near sensor |
You now know how to:
- β Wire DS18B20 sensors correctly with pull-up resistor
- β Read temperature from single or multiple sensors
- β Send data to OceanRemote cloud
- β Make farm decisions based on soil temperature
Next step: Add a soil moisture sensor to create a complete automated irrigation system!
sensors.begin(); // Initialize sensor sensors.requestTemperatures(); // Request temperature reading float temp = sensors.getTempCByIndex(0); // Read first sensor float temp = sensors.getTempC(deviceAddress); // Read specific sensor int count = sensors.getDeviceCount(); // Get number of sensors
- 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.