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

Arduino Mega 2560 Environmental Monitoring System with Relay Control and Data Logging

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Environmental Monitoring System with Relay Control and Data Logging

Circuit Documentation

Summary

This circuit is designed to monitor environmental parameters such as temperature, humidity, CO2 levels, and soil moisture. It utilizes an Arduino Mega 2560 as the central microcontroller to read data from DHT22 sensors, MQ135 sensors, a capacitive soil moisture sensor, and to control a 4-channel relay module and an RGB LED matrix. The data is displayed on both an LCD and an OLED display. A rotary encoder allows the user to navigate through different data pages on the OLED display. The circuit also includes an RTC module for timekeeping and an SD card module for data logging.

Component List

Arduino Mega 2560

4 Channel Relay Module

OLED 128x64 I2C Monochrome Display

LCD I2C Display

Rotary Encoder (x2)

MQ135 Gas Sensor (x2)

DFRobot Capacitive Soil Moisture Sensor (V1.0)

RTC DS1307

DHT22 Temperature and Humidity Sensor (x2)

WS2812 RGB LED Matrix 1x8

2cm x 8cm PCB

SD Card Module

Wiring Details

Arduino Mega 2560

4 Channel Relay Module

OLED 128x64 I2C Monochrome Display

LCD I2C Display

Rotary Encoder (x2)

MQ135 Gas Sensor (x2)

DFRobot Capacitive Soil Moisture Sensor (V1.0)

RTC DS1307

DHT22 Temperature and Humidity Sensor (x2)

WS2812 RGB LED Matrix 1x8

SD Card Module

Documented Code

/*
 * This Arduino Sketch reads data from DHT22 sensors for temperature and
 * humidity, MQ135 sensors for CO2 levels, and a soil moisture sensor. It
 * displays the data on an LCD and OLED screen. The LCD shows the date and
 * time, while the OLED cycles through pages displaying temperature, humidity,
 * CO2 levels, and soil moisture. A rotary encoder is used to switch between
 * the pages on the OLED display. The code also controls a relay module and an
 * RGB LED matrix based on specific conditions.
 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <DHT.h>
#include <RTClib.h>
#include <Adafruit_NeoPixel.h>
#include <SD.h>

#define DHTPIN0 22
#define DHTPIN1 24
#define DHTTYPE DHT22
#define MQ135PIN0 A2
#define MQ135PIN1 A1
#define SOILMOISTUREPIN A0
#define OLED_RESET -1
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define RELAY1 14
#define RELAY2 15
#define RGB_PIN 12
#define NUMPIXELS 8
#define SD_CS 46

DHT dht0(DHTPIN0, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;
Adafruit_NeoPixel pixels(NUMPIXELS, RGB_PIN, NEO_GRB + NEO_KHZ800);
File dataFile;

int page = 0;
const int totalPages = 4;
unsigned long previousMillis = 0;
const long interval = 20000;
bool relay2State = false;

void setup() {
  Wire.begin();
  lcd.begin();
  lcd.backlight();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
  dht0.begin();
  dht1.begin();
  rtc.begin();
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pixels.begin();
  if (!SD.begin(SD_CS)) {
    lcd.print("SD init failed!");
    return;
  }
  dataFile = SD.open("datalog.txt", FILE_WRITE);
}

void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');