Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Environmental Monitoring System with DHT11, LDR, and Rain Sensor

View and Edit Project In Cirkit DesignerImage of ESP32-Based Environmental Monitoring System with DHT11, LDR, and Rain Sensor

Circuit Documentation

Summary

The circuit in question is designed to interface an ESP32 Devkit V1 microcontroller with a set of sensors including a KY-015 DHT11 temperature and humidity sensor, a KY-018 LDR photoresistor for light detection, and a generic rain sensor. The ESP32 Devkit V1 is responsible for reading data from these sensors and processing it. The DHT11 sensor provides ambient temperature and humidity readings, the LDR photoresistor measures light intensity, and the rain sensor detects the presence of water. The ESP32 is programmed to read and interpret signals from these sensors, providing a serial output of the environmental conditions.

Component List

ESP32 Devkit V1

KY-015 DHT11 Temperature and Humidity Sensor

KY-018 LDR Photo Resistor

Rain Sensor

Wiring Details

ESP32 Devkit V1

KY-015 DHT11 Temperature and Humidity Sensor

KY-018 LDR Photo Resistor

Rain Sensor

Documented Code

#include <DHT.h>

// Pin assignments for ESP32
#define DHTPIN 13             // DHT11 connected to GPIO13
#define DHTTYPE DHT11         // Define the type of DHT sensor (DHT11)
#define RAIN_SENSOR_PIN 34    // Rain sensor connected to GPIO34 (ADC1_CH6)
#define PHOTORESISTOR_PIN 35  // Photoresistor connected to GPIO35 (ADC1_CH7)

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);          // Initialize serial communication
  dht.begin();                   // Start the DHT sensor
  pinMode(RAIN_SENSOR_PIN, INPUT); // Set rain sensor pin as input
  pinMode(PHOTORESISTOR_PIN, INPUT); // Set photoresistor pin as input

  Serial.println("ESP32 Sensors: Rain, DHT11, Photoresistor");
}

void loop() {
  // DHT11 readings
  float temperature = dht.readTemperature();  // Read temperature in Celsius
  float humidity = dht.readHumidity();        // Read humidity
  
  // Rain sensor reading (analog)
  int rainValue = analogRead(RAIN_SENSOR_PIN);  // Read rain sensor value

  // Define a threshold for rain detection
  int rainThreshold = 200;  // Adjust this value based on your sensor's readings

  // Check if it is raining based on the threshold
  String rainStatus = rainValue < rainThreshold ? "Rain detected" : "No rain";
  Serial.print("Rain Status: ");
  Serial.println(rainStatus);
  
  // Read light level from the photoresistor
  int lightValue = analogRead(PHOTORESISTOR_PIN); // Read light level from photoresistor

  // Normalize lightValue to a percentage (1-100)
  int normalizedLightValue = map(lightValue, 0, 4095, 0, 100); // ESP32 has 12-bit ADC (0-4095)

  // Print DHT and light sensor data
  if (!isnan(temperature) && !isnan(humidity)) {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println("°C");
    
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println("%");
  } else {
    Serial.println("Failed to read from DHT sensor!");
  }

  // Print photoresistor data with status
  Serial.print("Photoresistor Light Level: ");
  Serial.print(normalizedLightValue);
  Serial.println("%");

  // Delay between readings
  delay(2000);
}

This code is designed to run on the ESP32 Devkit V1 microcontroller. It initializes the DHT11 sensor and configures the pins for the rain sensor and photoresistor. The loop function reads the temperature and humidity from the DHT11, the analog value from the rain sensor, and the light level from the photoresistor. It then prints these values to the serial monitor, along with a message indicating whether rain is detected. The light level is normalized to a percentage for easier interpretation. The code includes error handling for failed DHT11 readings and a delay between each set of readings.