Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Mega 2560 and ESP32 Based Environmental Monitoring and Control System

Image 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

  • Microcontroller board based on the ATmega2560
  • Provides numerous digital input/output pins, analog inputs, and PWM outputs
  • Includes communication interfaces such as UART, SPI, and I2C

DHT22

  • Digital temperature and humidity sensor
  • Provides calibrated digital output

Water Level Sensor

  • Sensor for measuring the level of water
  • Outputs an analog signal corresponding to the water level

2-Channel Relay Module

  • Module with two relays capable of controlling high power devices
  • Each relay has Normally Open (NO) and Normally Closed (NC) connections

12V Battery (Small Size)

  • Provides power to the circuit
  • 12V output suitable for the fans and water pump

Water Pump

  • Electric pump for moving water
  • Operates at 12V

120mm Fan 12V

  • Two 120mm fans for air circulation
  • Operates at 12V

ESP32 (30 Pin)

  • Microcontroller with WiFi capability
  • Provides a variety of digital and analog I/O pins

Wiring Details

Arduino Mega 2560

  • 5V pin connected to the VCC pins of the DHT22, Water Level Sensor, and 2ch relay
  • GND pin connected to the GND pins of the DHT22, Water Level Sensor, and 2ch relay
  • D2 PWM pin connected to the DAT pin of the DHT22
  • A0 pin connected to the SIG pin of the Water Level Sensor
  • D3 PWM pin connected to the IN1 pin of the 2ch relay (controls the water pump)
  • D4 PWM pin connected to the IN2 pin of the 2ch relay (controls the fans)
  • 3V3 pin connected to the 3V3 pin of the ESP32
  • GND pin connected to the GND pin of the ESP32
  • D19/RX1 pin connected to the TX0 pin of the ESP32 (for serial communication)
  • D18/TX1 pin connected to the RX0 pin of the ESP32 (for serial communication)

DHT22

  • VCC pin connected to the 5V of the Arduino Mega 2560
  • GND pin connected to the GND of the Arduino Mega 2560
  • DAT pin connected to the D2 PWM of the Arduino Mega 2560

Water Level Sensor

  • VCC pin connected to the 5V of the Arduino Mega 2560
  • GND pin connected to the GND of the Arduino Mega 2560
  • SIG pin connected to the A0 of the Arduino Mega 2560

2-Channel Relay Module

  • VCC pin connected to the 5V of the Arduino Mega 2560
  • GND pin connected to the GND of the Arduino Mega 2560
  • IN1 pin connected to the D3 PWM of the Arduino Mega 2560
  • IN2 pin connected to the D4 PWM of the Arduino Mega 2560
  • COM pins connected to the VCC of the 12V Battery
  • NO pins connected to the VCC of the Water Pump and one of the 120mm fans

12V Battery (Small Size)

  • VCC pin connected to the COM pins of the 2ch relay
  • GND pin connected to the GND pins of the Water Pump and the 120mm fans

Water Pump

  • VCC pin connected to the NO pin of the 2ch relay
  • GND pin connected to the GND of the 12V Battery

120mm Fans 12V

  • One fan's 12V+ pin connected to the NO pin of the 2ch relay, and the other fan's 12V+ pin connected to the 12V+ of the first fan
  • Both fans' GND pins connected to the GND of the 12V Battery

ESP32 (30 Pin)

  • 3V3 pin connected to the 3V3 of the Arduino Mega 2560
  • GND pin connected to the GND of the Arduino Mega 2560
  • TX0 pin connected to the D19/RX1 of the Arduino Mega 2560
  • RX0 pin connected to the D18/TX1 of the Arduino Mega 2560

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.)