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

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

Image 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

  • Microcontroller board based on the ATmega2560
  • Provides numerous digital and analog I/O pins
  • Features I2C, SPI, and UART communication interfaces

4 Channel Relay Module

  • Module with 4 electromechanical relays
  • Each relay has normally open (N.O.), common (COM), and normally closed (N.C.) terminals
  • Controlled by digital outputs from the Arduino

OLED 128x64 I2C Monochrome Display

  • Small display for showing text and graphics
  • Uses I2C communication protocol
  • Typically used for displaying sensor data or menus

LCD I2C Display

  • Alphanumeric liquid crystal display with I2C interface
  • Displays time, date, and other status information

Rotary Encoder (x2)

  • Input device that provides rotational position feedback
  • Includes push-button functionality
  • Used for navigating the OLED display interface

MQ135 Gas Sensor (x2)

  • Air quality sensor for detecting a wide range of gases, including NH3, NOx, alcohol, benzene, smoke, and CO2
  • Provides both analog and digital outputs

DFRobot Capacitive Soil Moisture Sensor (V1.0)

  • Sensor for measuring the moisture content in soil
  • Outputs an analog signal that varies with the moisture level

RTC DS1307

  • Real-time clock module for timekeeping
  • Uses I2C communication protocol
  • Maintains time with a coin cell battery when power is lost

DHT22 Temperature and Humidity Sensor (x2)

  • Digital sensor for measuring ambient temperature and humidity
  • Provides calibrated digital output

WS2812 RGB LED Matrix 1x8

  • Strip of 8 individually addressable RGB LEDs
  • Controlled via a single digital pin

2cm x 8cm PCB

  • Small prototyping printed circuit board
  • Used for mounting and interconnecting components

SD Card Module

  • Module for reading and writing to SD cards
  • Uses SPI communication protocol
  • Utilized for data logging purposes

Wiring Details

Arduino Mega 2560

  • 3V3 connected to SD card module +3.3V
  • 5V connected to various components' VCC pins
  • GND connected to various components' GND pins
  • A0 to A2 connected to analog pins of sensors
  • D21/SCL and D20/SDA connected to I2C devices (LCD, OLED, RTC)
  • D17 PWM/RX2 to D14/TX3 connected to relay module inputs IN 1 to IN 4
  • D5 PWM to D11 PWM connected to rotary encoder pins CLK, DT, SW
  • D12 PWM connected to WS2812 RGB LED matrix DIN
  • D52, D50, D51, D46 connected to SD card module SCK, MISO, MOSI, CS
  • D24, D22 connected to DHT22 sensors OUT pins

4 Channel Relay Module

  • VCC+ and VCC- (GND) connected to 5V and GND respectively
  • IN 1 to IN 4 controlled by Arduino pins D14/TX3 to D17 PWM/RX2

OLED 128x64 I2C Monochrome Display

  • VDD and GND connected to 5V and GND respectively
  • SCK and SDA connected to Arduino D21/SCL and D20/SDA

LCD I2C Display

  • VCC and GND connected to 5V and GND respectively
  • SDA and SCL connected to Arduino D20/SDA and D21/SCL

Rotary Encoder (x2)

  • + and GND connected to 5V and GND respectively
  • SW, DT, CLK connected to Arduino pins D7 PWM, D6 PWM, D5 PWM for one encoder and D11 PWM, D10 PWM, D9 PWM for the other

MQ135 Gas Sensor (x2)

  • VCC and GND connected to 5V and GND respectively
  • A0 connected to Arduino A1 and A2

DFRobot Capacitive Soil Moisture Sensor (V1.0)

  • VCC and GND connected to 5V and GND respectively
  • A connected to Arduino A0

RTC DS1307

  • VCC and GND connected to 5V and GND respectively
  • SCL and SDA connected to Arduino D21/SCL and D20/SDA

DHT22 Temperature and Humidity Sensor (x2)

  • VCC and GND connected to 5V and GND respectively
  • OUT connected to Arduino D22 and D24

WS2812 RGB LED Matrix 1x8

  • VCC and GND connected to 5V and GND respectively
  • DIN connected to Arduino D12 PWM

SD Card Module

  • +3.3V, GND, +5V connected to Arduino 3V3, GND, 5V respectively
  • CS, MOSI, SCK, MISO connected to Arduino D46, D51, D52, D50

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('/');