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

ESP32-Based Smart Weather Station with Light Sensing and Humidity Alert

View and Edit Project In Cirkit DesignerImage of ESP32-Based Smart Weather Station with Light Sensing and Humidity Alert

Circuit Documentation

Summary

This circuit is designed to function as a weather station, featuring an ESP32 microcontroller as the central processing unit. It includes an LDR (Light Dependent Resistor) module for light sensing, a DHT11 sensor for measuring temperature and humidity, a buzzer for audible alerts, an LED for visual indication, and an I2C LCD 16x2 screen for displaying sensor data. The ESP32 microcontroller is responsible for reading sensor data, controlling the output devices (buzzer and LED), and displaying the information on the LCD screen.

Component List

LDR Module 3 pin

Buzzer

LED: Two Pin (red)

I2C LCD 16x2 Screen

Esp32 on Baseboard

DHT11

Wiring Details

LDR Module 3 pin

Buzzer

LED: Two Pin (red)

I2C LCD 16x2 Screen

DHT11

Documented Code

#include <DHT.h>
#include <LiquidCrystal_I2C.h>

// DHT sensor configuration
#define DHT11_PIN   18 // ESP32 pin GPIO18 connected to DHT11 sensor
#define BUZZER_PIN  27 // GPIO pin for buzzer
#define LED_PIN     26 // GPIO pin for LED
#define LIGHT_SENSOR_PIN 19 // GPIO pin for LDR sensor
#define HMDT_THRESHOLD 67.0 // Humidity threshold

// Initialize DHT11 sensor and LCD display
DHT dht11(DHT11_PIN, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address, 16 columns, 2 rows

void setup() {
  Serial.begin(9600);

  // Initialize DHT11 and LCD
  dht11.begin();
  lcd.init();
  lcd.backlight();

  // Initialize pin modes
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(LIGHT_SENSOR_PIN, INPUT);

  digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off
  digitalWrite(LED_PIN, LOW); // Ensure LED is off
}

void loop() {
  // Read humidity and temperature
  float humi  = dht11.readHumidity();
  float tempC = dht11.readTemperature();
  float tempF = dht11.readTemperature(true);

  // Read light sensor value
  int digitalValue = digitalRead(LIGHT_SENSOR_PIN);
  String lightStatus = (digitalValue == LOW) ? "Bright" : "Dark";
  if (digitalValue == HIGH) {
    Serial.println(" => Bright");
    Serial.println();
    digitalWrite(LED_PIN, HIGH); // Turn off LED if bright
  } else {
    Serial.println(" => Dark");
    Serial.println();
    digitalWrite(LED_PIN, LOW); // Turn on LED if dark
  }

  // Check if the readings are valid
  if (isnan(tempC) || isnan(tempF) || isnan(humi)) {
    Serial.println("Failed to read from DHT11 sensor!");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Sensor Error!");
  } else {
    // Display data on Serial Monitor
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("% | Temp: ");
    Serial.print(tempC);
    Serial.print("C / ");
    Serial.print(tempF);
    Serial.print("F | Light: ");
    Serial.println(lightStatus);

    // Display data on LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("H:");
    lcd.print(humi);
    lcd.print("% T:");
    lcd.print(tempC);
    lcd.print("C");

    lcd.setCursor(0, 1);
    lcd.print("L:");
    lcd.print(lightStatus);
    lcd.print(" F:");
    lcd.print(tempF);
    lcd.print("F");

    // Check humidity for alert
    if (humi < HMDT_THRESHOLD) {
      digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
      digitalWrite(LED_PIN, HIGH); // Turn on LED

      // Override LCD display with alert
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Alert! Low Humid!");
      lcd.setCursor(0, 1);
      lcd.print("H:");
      lcd.print(humi);
      lcd.print("% - Air Dry!");
      
      Serial.println("Alert! Air Too Dry!");
    } else {
      digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
      digitalWrite(LED_PIN, LOW); // Turn off LED
    }
  }

  // Wait 500ms seconds between readings
  delay(500);
}

This code is designed to run on the ESP32 microcontroller. It initializes the DHT11 sensor and the I2C LCD display, reads temperature, humidity, and light sensor data, and displays the information on the LCD screen. It also controls the buzzer and LED based on the humidity level and light sensor input.