Arduino UNO-Based Smart Weather Station with Gas and Rain Detection
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor environmental conditions using various sensors and display the data on an LCD screen. It includes an Arduino UNO microcontroller, an MQ-2 gas sensor, a DHT22 temperature and humidity sensor, a rain sensor, an RTC DS3231 real-time clock, a buzzer for alerts, and a 16x2 I2C LCD screen. The Arduino UNO reads data from the sensors and displays it on the LCD screen. It also triggers the buzzer if certain conditions are met.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
MQ-2
- Description: A gas sensor that detects LPG, i-butane, propane, methane, alcohol, hydrogen, and smoke.
- Pins: GND, VCC, ANALOG, Digital
DHT22
- Description: A temperature and humidity sensor.
- Pins: +, Out, -
Buzzer
- Description: An electronic device that emits a sound when activated.
- Pins: GND, IN
RTC DS3231
- Description: A real-time clock module with a temperature-compensated crystal oscillator.
- Pins: 32K, SQW, SCL, SDA, VCC, GND
RAIN SENSOR
- Description: A sensor that detects the presence of rain.
- Pins: AO, DO, GRD, VCC
LCD screen 16x2 I2C
- Description: A 16x2 character LCD display with I2C interface.
- Pins: SCL, SDA, VCC, GND
Wiring Details
Arduino UNO
- 5V: Connected to VCC of DHT22, MQ-2, RAIN SENSOR, RTC DS3231, and LCD screen 16x2 I2C
- GND: Connected to GND of DHT22, MQ-2, Buzzer, RTC DS3231, RAIN SENSOR, and LCD screen 16x2 I2C
- D2: Connected to Out of DHT22
- A0: Connected to AO of RAIN SENSOR
- A1: Connected to ANALOG of MQ-2
- A4: Connected to SDA of LCD screen 16x2 I2C and RTC DS3231
- A5: Connected to SCL of LCD screen 16x2 I2C and RTC DS3231
- D8: Connected to IN of Buzzer
MQ-2
- GND: Connected to GND of Arduino UNO
- VCC: Connected to 5V of Arduino UNO
- ANALOG: Connected to A1 of Arduino UNO
DHT22
- +: Connected to 5V of Arduino UNO
- Out: Connected to D2 of Arduino UNO
- -: Connected to GND of Arduino UNO
Buzzer
- GND: Connected to GND of Arduino UNO
- IN: Connected to D8 of Arduino UNO
RTC DS3231
- GND: Connected to GND of Arduino UNO
- VCC: Connected to 5V of Arduino UNO
- SDA: Connected to A4 of Arduino UNO
- SCL: Connected to A5 of Arduino UNO
RAIN SENSOR
- GRD: Connected to GND of Arduino UNO
- VCC: Connected to 5V of Arduino UNO
- AO: Connected to A0 of Arduino UNO
LCD screen 16x2 I2C
- GND: Connected to GND of Arduino UNO
- VCC: Connected to 5V of Arduino UNO
- SDA: Connected to A4 of Arduino UNO
- SCL: Connected to A5 of Arduino UNO
Documented Code
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(2, DHT22);
const int hujanpin = A0;
const int mq2Pin = A1;
char HariDalamMinggu[7][12] = {"Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"};
unsigned long lastSwitchTime = 0;
unsigned long lastSerialTime = 0;
int displayState = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.backlight();
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("RTC Error!");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
dht.begin();
DDRB |= (1 << DDB0);
PORTB &= ~(1 << PORTB0);
pinMode(hujanpin, INPUT);
lcd.setCursor(0, 0);
lcd.print("Welcome Kelompok");
lcd.setCursor(0, 1);
lcd.print("4 Mikro");
delay(2000);
lcd.clear();
}
void loop() {
if (millis() - lastSwitchTime > 5000) {
lastSwitchTime = millis();
displayState = (displayState + 1) % 3;
lcd.clear();
}
int nilaiSensor = analogRead(hujanpin);
int mq2Value = analogRead(mq2Pin);
if (nilaiSensor >= 500) {
if (displayState == 0) {
displayTimeAndDate();
} else if (displayState == 1) {
displayWeatherCondition();
} else if (displayState == 2) {
displayMQ2Data();
}
} else {
displayhujan();
}
checkAlert();
// Serial Monitor Update
if (millis() - lastSerialTime >= 1000) {
lastSerialTime = millis();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" C, Hum: ");
Serial.print(humidity);
Serial.print(" %, Gas: ");
Serial.print(mq2Value);
Serial.print(", Rain Sensor: ");
Serial.println(nilaiSensor);
}
}
void displayTimeAndDate() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(HariDalamMinggu[now.dayOfTheWeek()]);
lcd.print(", ");
lcd.print(now.day());
lcd.print("/");
lcd.print(now.month());
lcd.print("/");
lcd.print(now.year());
lcd.setCursor(0, 1);
if (now.hour() < 10) lcd.print("0");
lcd.print(now.hour());
lcd.print(":");
if (now.minute() < 10) lcd.print("0");
lcd.print(now.minute());
lcd.print(":");
if (now.second() < 10) lcd.print("0");
lcd.print(now.second());
}
void displayWeatherCondition() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 0);
if (isnan(temperature) || isnan(humidity)) {
lcd.print("T:Err H:Err");
return;
}
lcd.print("T:" + String(temperature) + "C ");
lcd.print("H:" + String(humidity) + "%");
lcd.setCursor(0, 1);
// Determine temperature category
String tempCategory;
if (temperature < 20) {
tempCategory = "Dingin";
} else if (temperature >= 20 && temperature < 30) {
tempCategory = "Hangat";
} else {
tempCategory = "Panas";
}
// Determine humidity category
String humidityCategory;
if (humidity > 60) {
humidityCategory = "Lembab";
} else {
humidityCategory = "Kering";
}
// Display combined condition
lcd.print(tempCategory + " & " + humidityCategory);
}
void displayMQ2Data() {
int mq2Value = analogRead(mq2Pin);
lcd.setCursor(0, 0);
lcd.print("Gas Level: ");
lcd.print(mq2Value);
lcd.setCursor(0, 1);
if (mq2Value > 600) {
lcd.print("Buruk");
} else if (mq2Value > 300) {
lcd