OC
OceanRemote
Low-code IoT platform
← Back to Course

Combining Multiple Sensors on One Board

Combining Multiple Sensors on One Board

šŸ”„ Combining Multiple Sensors - Complete Farm Monitor

🌾 What You'll Learn:

  • šŸ”Œ Connect soil moisture, temperature, humidity, and rain sensors together
  • šŸ“Š Make smart irrigation decisions based on multiple data points
  • šŸ’§ Skip irrigation when rain is detected automatically
  • šŸ“ˆ Create a complete farm monitoring dashboard

šŸ“‹ Complete Component List

Sensor Pin Measures
Soil Moisture (Capacitive) GPIO32 Water content 0-100%
DS18B20 (Soil Temp) GPIO4 (OneWire) Soil temperature -55 to +125°C
DHT22 (Air Temp/Humidity) GPIO16 Air temperature and humidity
Rain Sensor GPIO33 Rain detection (LOW = raining)

šŸ“– Complete Multi-Sensor Code

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

// ========== WIFI CONFIGURATION ==========
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";

// ========== PIN DEFINITIONS ==========
#define SOIL_PIN 32          // Capacitive soil moisture
#define ONE_WIRE_BUS 4       // DS18B20 soil temperature
#define DHTPIN 16            // DHT22 air temp/humidity
#define DHTTYPE DHT22
#define RAIN_PIN 33          // Rain sensor (LOW = rain)

// ========== SENSOR CALIBRATION ==========
const int DRY_VALUE = 3800;
const int WET_VALUE = 1500;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature soilTemp(&oneWire);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    soilTemp.begin();
    dht.begin();
    pinMode(RAIN_PIN, INPUT_PULLUP);
    
    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(500);
    Serial.println("āœ… Connected to WiFi");
}

void loop() {
    // ========== READ ALL SENSORS ==========
    
    // 1. Soil Moisture
    int soilRaw = analogRead(SOIL_PIN);
    int soilMoisture = map(soilRaw, DRY_VALUE, WET_VALUE, 0, 100);
    soilMoisture = constrain(soilMoisture, 0, 100);
    
    // 2. Soil Temperature (DS18B20)
    soilTemp.requestTemperatures();
    float soilTemperature = soilTemp.getTempCByIndex(0);
    
    // 3. Air Temperature & Humidity (DHT22)
    float airTemp = dht.readTemperature();
    float humidity = dht.readHumidity();
    
    // 4. Rain Detection
    bool isRaining = (digitalRead(RAIN_PIN) == LOW);
    
    // Validate readings
    if (isnan(airTemp) || isnan(humidity)) {
        Serial.println("āŒ DHT22 read error!");
        airTemp = -999;
        humidity = -999;
    }
    
    // ========== DISPLAY REPORT ==========
    Serial.println("\n╔════════════════════════════════════════════╗");
    Serial.println("ā•‘         🌾 COMPLETE FARM REPORT           ā•‘");
    Serial.println("ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•");
    
    Serial.printf("šŸ’§ Soil Moisture: %d%%\n", soilMoisture);
    Serial.printf("šŸŒ”ļø Soil Temperature: %.1f°C\n", soilTemperature);
    Serial.printf("šŸŒ¬ļø Air Temperature: %.1f°C\n", airTemp);
    Serial.printf("šŸ’Ø Humidity: %.1f%%\n", humidity);
    Serial.printf("ā˜” Raining: %s\n", isRaining ? "āŒ NO" : "āœ… YES");
    
    // ========== IRRIGATION DECISION ==========
    Serial.println("\nšŸŽÆ IRRIGATION RECOMMENDATION:");
    
    if (isRaining) {
        Serial.println("   ā˜” Rain detected - SKIP irrigation");
    } 
    else if (soilMoisture < 30) {
        Serial.println("   🚨 SOIL DRY - START irrigation NOW!");
    } 
    else if (soilMoisture < 45) {
        Serial.println("   āš ļø Soil drying - Plan irrigation soon");
    } 
    else if (soilMoisture > 80) {
        Serial.println("   āœ… SOIL WET - STOP irrigation");
    } 
    else {
        Serial.println("   āœ… Soil moisture adequate - No action needed");
    }
    
    // Temperature alerts
    if (airTemp > 35) {
        Serial.println("\nšŸ”„ HEAT ALERT: Consider shading crops");
    }
    if (airTemp < 5 && !isRaining) {
        Serial.println("\nā„ļø FROST ALERT: Protect sensitive crops");
    }
    
    Serial.println("\n════════════════════════════════════════════\n");
    
    delay(60000); // Read every minute
}
    
šŸ’” Smart Decision Logic:
  • Rain detected: Skip irrigation regardless of soil moisture
  • Soil < 30%: Start irrigation immediately
  • Soil > 80%: Stop irrigation to prevent root rot
  • Heat alert: Temperature > 35°C - crops stressed
āš ļø Troubleshooting Multi-Sensor Issues:
  • DHT22 failing: Add delay(2000) between readings
  • DS18B20 not detected: Check pull-up resistor (4.7kĪ©)
  • Rain sensor always HIGH: Adjust potentiometer on module
šŸŽ‰ Next Steps:
  • āœ… Add a relay to automate irrigation based on decisions
  • āœ… Send data to OceanRemote cloud dashboard
  • āœ… Add solar power for remote field deployment
  • āœ… Set up SMS/email alerts for critical conditions
šŸ’” 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.