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

Arduino Mega 2560 Based Weather Monitoring and Alarm System with I2C LCD, RTC, and Sensors

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Based Weather Monitoring and Alarm System with I2C LCD, RTC, and Sensors

Circuit Documentation

Summary

The circuit in question is a multifunctional system that incorporates various sensors, a display, a real-time clock (RTC), and a microcontroller to manage inputs and outputs. The primary controller is an Arduino Mega 2560, which interfaces with an LCD I2C display, RTC DS3231, a rain sensor, a DHT11 temperature and humidity sensor, a BMP180 pressure sensor, a relay module, and multiple pushbuttons. The circuit is designed to monitor environmental conditions and control external devices based on sensor readings and user inputs.

Component List

Arduino Mega 2560

LCD I2C Display

RTC DS3231

Rain Sensor

DHT11

Relay Module 1 Channel

Pushbuttons

BMP180

2cmx8cm PCBs

Comments

Wiring Details

Arduino Mega 2560

LCD I2C Display

RTC DS3231

Rain Sensor

DHT11

Relay Module 1 Channel

Pushbuttons

BMP180

Documented Code

The code for the Arduino Mega 2560 is written in C++ and utilizes several libraries for interfacing with the hardware components. The code manages sensor readings, user inputs, displays information on the LCD, and controls the relay based on the logic defined within the program.

#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <Countimer.h>
#include <SimpleDHT.h>

// Define pin connections & constants
#define bt_select A1
#define bt_data A2
#define bt_up A3
#define bt_down A4
#define bt_manual_start A5
#define bt_manual_stop A6
#define bt_pumpstartwithtimer A7
#define buzzer 8
#define EEPROM_ADDR_ALARM_HH 1
#define EEPROM_ADDR_ALARM_MM 2
#define EEPROM_ADDR_ALARM_SS 3
#define EEPROM_ADDR_MODE 4
#define EEPROM_ADDR_STOP 50
#define seconds 6
#define minutes 7
#define hours 8

// Initialize objects for sensors and display
Countimer tdown;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_BMP085 bmp;
RTC_DS3231 rtc;
DateTime now;

// Variables for time, date, and alarm settings
int hh = 0, mm = 0, ss = 0, dd = 0, bb = 0, yy = 0;
int set_day = 1;
String Day = "  ";
int AlarmHH = 21, AlarmMM = 22, AlarmSS = 23;
int setMode = 0, setAlarm = 0, alarmMode = 0, setrunTimer = 0, setselect = 0;
int stop = 0, mode = 0, flag = 0;
bool alarmActive = false;
unsigned long buzzerStartTime = 0;
bool timerWaitingToStart = true;
bool showTemperatureHumidity = false;
bool alarmWorking = false;
bool skipNextAlarm = false;
bool delayalarm = false;
bool startdelay = false;
const int SENSOR_PIN = A0;
const int SENSOR_THRESHOLD = 500;
const unsigned long ALARM_WORKING_TIMEOUT = 10000;
const unsigned long ALARM_NOT_WORKING_TIMEOUT = 20000;
unsigned long alarmStateChangeTime = 0;
unsigned long startTime = 0;
const unsigned long conditionDuration = 5000;
unsigned long delayStartTime = 0;
const unsigned long DELAY_DURATION = 7000;
int time_s = 0;
int time_m = 0;
int time_h = 0;
int pump = 0;
int flag1 = 0, flag2 = 0;
int pinDHT11 = 10;
SimpleDHT11 dht11(pinDHT11);

// Custom character for the LCD display
byte bell_symbol[8] = {
  B00100,
  B01110,
  B01110,
  B01110,
  B01110,
  B11111,
  B01000,
  B00100
};

// Function prototypes
void ReadEeprom();
void WriteEeprom();
void printTwoDigits(int number);
void printFourDigits(int number);
void setTimer();
void setupClock();
void blinking();

void print_time();
void tdownComplete();
void setup1();
void eeprom_read();
void runTimer();
void setrunTimerrun();
void humidity();

// Setup function runs once at startup
void setup() {
  // Initialize serial communication and I2C interface
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  // Setup pin modes for buttons and buzzer
  pinMode(bt_up, INPUT_PULLUP);
  pinMode(bt_down, INPUT_PULLUP);
  pinMode(bt_manual_start, INPUT_PULLUP);
  pinMode(bt_manual_stop, INPUT_PULLUP);
  pinMode(bt_data, INPUT_PULLUP);
  pinMode(bt_pumpstartwithtimer, INPUT_PULLUP);
  pinMode(bt_select, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  // Create custom LCD character
  lcd.createChar(2, bell_symbol);

  // Initialize the LCD and display startup message
  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print(" Real Time Clock ");
  lcd.setCursor(0, 1);
  lcd.print("   With Alarm ");
  tdown.setInterval(print_time, 999);
  eeprom_read();
  delay(2000);
  lcd.clear();

  // Check if the system was stopped and reset EEPROM if necessary
  stop = EEPROM.read(EEPROM_ADDR_STOP);
  if (stop != 0) {
    WriteEeprom();
  }
  EEPROM.write(EEPROM_ADDR_STOP, 0);

  // Read alarm settings and mode from EEPROM
  AlarmHH = EEPROM.read(EEPROM_ADDR_ALARM_HH);
  AlarmMM = EEPROM.read(EEPROM_ADDR_ALARM_MM);
  AlarmSS = EEPROM.read(EEPROM_ADDR_ALARM_SS);
  mode = EEPROM.read(EEPROM_ADDR_MODE);

  // Initialize the BMP180 sensor
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
}

// Placeholder for tdownComplete function
void tdownComplete() {
  Serial.print("ok");
}

// Main loop function runs repeatedly
void loop() {
  // Placeholder for main loop code
}

// Additional functions and logic for handling sensor readings, user inputs, and display updates