Arduino Mega 2560 Environmental Monitoring System with Data Logging and Relay Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The 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, and a capacitive soil moisture sensor. The data is displayed on both an LCD and an OLED display. A rotary encoder allows the user to switch between different data pages on the OLED display. The circuit also includes a 4-channel relay module to control external devices and an RGB LED matrix for visual alerts. An SD card module is used for data logging purposes.
Component List
Arduino Mega 2560
- Microcontroller board based on the ATmega2560
- Provides numerous digital input/output pins, analog inputs, and various 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
OLED 128x64 I2C Monochrome Display
- Small display for showing graphical and textual information
- Uses I2C communication protocol
LCD I2C Display
- Alphanumeric liquid crystal display with I2C interface
- Displays characters and numbers
Rotary Encoder (x2)
- Input device that provides rotational feedback
- Includes pins for ground, power, switch, and two data outputs
MQ135 Gas Sensor (x2)
- Sensor for detecting a wide range of gases, including 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
RTC DS1307
- Real-time clock module for keeping track of the current time and date
- Uses I2C communication protocol
DHT22 Temperature and Humidity Sensor (x2)
- Sensor for measuring ambient temperature and humidity
- Provides a digital signal output
WS2812 RGB LED Matrix 1x8
- Strip of 8 individually addressable RGB LEDs
- Controlled via a single data line
2cm x 8cm PCB
- Generic printed circuit board for prototyping
SD Card Module
- Module for reading and writing to SD cards
- Uses SPI communication protocol
Wiring Details
Arduino Mega 2560
3V3to SD Card Module+3.3V5Vto all components requiring 5V supplyGNDto all components requiring ground connectionA0to Soil Moisture SensorAA1to MQ135 SensorA0A2to MQ135 SensorA0D21/SCLto I2C devicesSCL(LCD, OLED, RTC)D20/SDAto I2C devicesSDA(LCD, OLED, RTC)D17 PWM/RX2to Relay ModuleIN 4D16 PWM/TX2to Relay ModuleIN 3D15/RX3to Relay ModuleIN 2D14/TX3to Relay ModuleIN 1D5 PWMto Rotary EncoderCLKD6 PWMto Rotary EncoderDTD7 PWMto Rotary EncoderSWD9 PWMto Rotary EncoderCLKD10 PWMto Rotary EncoderDTD11 PWMto Rotary EncoderSWD12 PWMto RGB LED MatrixDIND22to DHT22 SensorOUTD24to DHT22 SensorOUTD46to SD Card ModuleCSD50to SD Card ModuleMISOD51to SD Card ModuleMOSID52to SD Card ModuleSCK
4 Channel Relay Module
VCC+to 5V supplyVCC- (GND)to groundIN 1toD14/TX3on ArduinoIN 2toD15/RX3on ArduinoIN 3toD16 PWM/TX2on ArduinoIN 4toD17 PWM/RX2on Arduino
OLED 128x64 I2C Monochrome Display
VDDto 5V supplyGNDto groundSCKtoD21/SCLon ArduinoSDAtoD20/SDAon Arduino
LCD I2C Display
VCCto 5V supplyGNDto groundSDAtoD20/SDAon ArduinoSCLtoD21/SCLon Arduino
Rotary Encoder (x2)
GNDto ground+to 5V supplySWtoD7 PWMorD11 PWMon ArduinoDTtoD6 PWMorD10 PWMon ArduinoCLKtoD5 PWMorD9 PWMon Arduino
MQ135 Gas Sensor (x2)
VCCto 5V supplyGNDto groundA0toA1orA2on Arduino
DFRobot Capacitive Soil Moisture Sensor (V1.0)
VCCto 5V supplyGNDto groundAtoA0on Arduino
RTC DS1307
VCCto 5V supplyGNDto groundSDAtoD20/SDAon ArduinoSCLtoD21/SCLon Arduino
DHT22 Temperature and Humidity Sensor (x2)
VCCto 5V supplyGNDto groundOUTtoD22orD24on Arduino
WS2812 RGB LED Matrix 1x8
VCCto 5V supplyGNDto groundDINtoD12 PWMon Arduino
SD Card Module
GNDto ground+3.3Vto3V3on Arduino+5Vto 5V supplyCStoD46on ArduinoMOSItoD51on ArduinoSCKtoD52on ArduinoMISOtoD50on Arduino
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