This circuit is designed to monitor environmental conditions such as humidity, temperature, and air quality. It also controls a water pump based on the state of a rocker switch. The main components include an Arduino UNO, a DHT11 humidity and temperature sensor, an MQ-2 gas sensor, a 16x2 I2C LCD, a two-channel relay, a water pump, a rocker switch, and a 18650 Li-Ion battery.
Arduino UNO
DHT11 Humidity and Temperature Sensor
MQ-2 Gas Sensor
Two Channel Relay 5V
18650 Li-Ion Battery
Water Pump
Rocker Switch
16x2 I2C LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin definitions
#define DHTPIN 2
#define DHTTYPE DHT11
#define MQ2PIN A0
#define RELAYPIN 3
#define SWITCHPIN 4
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set pin modes
pinMode(MQ2PIN, INPUT);
pinMode(RELAYPIN, OUTPUT);
pinMode(SWITCHPIN, INPUT);
// Initial state of the relay
digitalWrite(RELAYPIN, LOW);
}
void loop() {
// Read humidity and temperature from DHT11
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read air quality from MQ-2
int airQuality = analogRead(MQ2PIN);
// Display data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C AQ: ");
lcd.print(airQuality);
// Check the state of the switch
if (digitalRead(SWITCHPIN) == HIGH) {
// Turn on the water pump
digitalWrite(RELAYPIN, HIGH);
} else {
// Turn off the water pump
digitalWrite(RELAYPIN, LOW);
}
// Wait for a second before the next loop
delay(1000);
}
This code initializes the sensors and LCD, reads data from the DHT11 and MQ-2 sensors, displays the data on the LCD, and controls the water pump based on the state of the rocker switch.