This circuit is designed for a home automation system that integrates various sensors and an I2C LCD display to monitor environmental conditions and detect motion. The core of the system is an Arduino UNO microcontroller, which processes data from a DHT11 temperature and humidity sensor, an MQ-4 gas sensor, an HC-SR04 ultrasonic sensor, and an HC-SR501 motion sensor. The system is powered by a 9V battery and displays sensor readings on an I2C LCD 16x2 screen. The Arduino UNO also manages the communication with the I2C LCD and reads the sensor data to perform actions based on the sensor inputs.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin Definitions
#define DHTPIN 2
#define DHTTYPE DHT11
#define PIRPIN 7
#define MQ4PIN A0
#define TRIGPIN 8
#define ECHOPIN 9
// Initialize DHT11
DHT dht(DHTPIN, DHTTYPE);
// Initialize LCD (Assuming I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
// Initialize sensors
dht.begin();
pinMode(PIRPIN, INPUT);
pinMode(MQ4PIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
// Display welcome message
lcd.setCursor(0, 0);
lcd.print("Home Automation");
lcd.setCursor(0, 1);
lcd.print("System Starting...");
delay(2000);
lcd.clear();
}
void loop() {
// Read DHT11 sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
// Read PIR sensor
int pirState = digitalRead(PIRPIN);
// Read MQ4 Gas Sensor
int gasValue = analogRead(MQ4PIN);
// Read HC-SR04 Ultrasonic Sensor
long duration, distance;
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = (duration / 2) / 29.1; // Convert to cm
// Display sensor data on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("C Hum: ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Gas: ");
lcd.print(gasValue);
lcd.print(" PIR: ");
lcd.print(pirState ? "Motion" : "No Motion");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
delay(2000);
lcd.clear();
// Add any other logic for handling detected events
// such as sending alerts or triggering alarms
}
This code initializes and reads from the connected sensors, displaying their data on the I2C LCD screen. It includes setup for the DHT11 sensor, the PIR motion sensor, the MQ-4 gas sensor, and the HC-SR04 ultrasonic sensor. The LCD displays temperature, humidity, gas levels, motion detection, and distance measurements in a rotating fashion.