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

Arduino Mega 2560 and ESP32 Based Environmental Monitoring and Control System

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 and ESP32 Based Environmental Monitoring and Control System

Circuit Documentation

Summary

The circuit in question is designed to monitor environmental conditions and control a water pump and fans based on temperature and water level readings. It utilizes an Arduino Mega 2560 as the main microcontroller to interface with a DHT22 temperature and humidity sensor, a water level sensor, and a 2-channel relay module. The relay module controls the power to a water pump and two 120mm 12V fans. An ESP32 microcontroller is also included for potential WiFi capabilities. The system is powered by a 12V battery.

Component List

Arduino Mega 2560

DHT22

Water Level Sensor

2-Channel Relay Module

12V Battery (Small Size)

Water Pump

120mm Fan 12V

ESP32 (30 Pin)

Wiring Details

Arduino Mega 2560

DHT22

Water Level Sensor

2-Channel Relay Module

12V Battery (Small Size)

Water Pump

120mm Fans 12V

ESP32 (30 Pin)

Documented Code

Arduino Mega 2560 Code (SYSTEM.ino)

#include <DHT.h>
#include <Wire.h>

#define DHTPIN 2
#define DHTTYPE DHT22
#define WATER_LEVEL_PIN A0
#define RELAY_PUMP_PIN 3
#define RELAY_FAN_PIN 4

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200); // Communication with ESP32
  dht.begin();
  
  pinMode(WATER_LEVEL_PIN, INPUT);
  pinMode(RELAY_PUMP_PIN, OUTPUT);
  pinMode(RELAY_FAN_PIN, OUTPUT);
  
  digitalWrite(RELAY_PUMP_PIN, LOW);
  digitalWrite(RELAY_FAN_PIN, LOW);
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int waterLevel = analogRead(WATER_LEVEL_PIN);
  
  // Send data to ESP32
  Serial1.print("Temperature: ");
  Serial1.print(temperature);
  Serial1.print(" C, Humidity: ");
  Serial1.print(humidity);
  Serial1.print(" %, Water Level: ");
  Serial1.print(waterLevel);
  Serial1.println(" mm");
  
  // Check if the temperature is below 15 degrees Celsius
  if (temperature < 15.0 && waterLevel > 3) {
    digitalWrite(RELAY_PUMP_PIN, HIGH); // Activate water pump
    digitalWrite(RELAY_FAN_PIN, HIGH);  // Activate fans
  } else {
    digitalWrite(RELAY_PUMP_PIN, LOW);  // Deactivate water pump
    digitalWrite(RELAY_FAN_PIN, LOW);   // Deactivate fans
  }
  
  delay(2000); // Wait for 2 seconds before the next reading
}

ESP32 Code (sketch.ino)

#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

WebServer server(80);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  Serial.println("Connected to WiFi");
  
  server.on("/", handleRoot);
  server.begin();
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  if (Serial.available()) {
    String data = Serial.readStringUntil('\n');
    server.send(200, "text/plain", data);
  } else {
    server.send(200, "text/plain", "No data available");
  }
}

(Note: The second code snippet provided for the Arduino Mega 2560 appears to be intended for the ESP32, as it includes WiFi library calls and is written in C++ which is typical for ESP32 sketches. The code snippet is similar to the ESP32 code provided, suggesting a possible duplication or mislabeling in the input data.)