The circuit is designed to monitor environmental parameters such as temperature, humidity, rain presence, and air quality, and display the information on an LCD screen. It uses an Arduino UNO as the central microcontroller to interface with a DHT11 humidity and temperature sensor, a YL-83 rain sensor, an MQ-2 gas sensor, and an HC-SR04 ultrasonic sensor. The data from these sensors is displayed on a 16x2 LCD. The circuit also includes resistors for current limiting and voltage adjustment purposes.
5V
and GND
pins provide power to the sensors and LCDD2
to D13
and analog pin A0
are used for interfacing with sensors and the LCDVSS
, RW
, and K
pins are connected to GNDVDD
pin is connected to 5VV0
pin is connected to a 1k Ohm resistor for contrast adjustmentRS
, E
, D4
to D7
pins are connected to Arduino digital pins for data/command transmissionA
pin is connected to a 200 Ohm resistor for backlight controlVDD
pin is connected to 5V through a 200 Ohm resistorDATA
pin is connected to Arduino digital pin D2
GND
pin is connected to GNDVCC
pin is connected to 5V through a 200 Ohm resistorGND
pin is connected to GNDDO
pin is connected to Arduino digital pin D3
+VCC
pin is connected to 5V through a 200 Ohm resistorTrigger
pin is connected to Arduino digital pin D6
Echo
pin is connected to Arduino digital pin D13
GND
pin is connected to GNDV0
pin of the LCD for contrast controlA
pin of the LCD for backlight controlVCC
pin is connected to 5V through a 200 Ohm resistorGND
pin is connected to GNDANALOG
pin is connected to Arduino analog pin A0
#include <DHT.h>
#include <LiquidCrystal.h>
#include <NewPing.h> // Include the NewPing library for the ultrasonic sensor
#define DHTPIN 2
#define DHTTYPE DHT11
#define TRIG_PIN 6
#define ECHO_PIN 13
#define MAX_DISTANCE 200
#define GAS_LOW_THRESHOLD 40 // Upper limit for Low AQI
#define GAS_MEDIUM_THRESHOLD 60 // Upper limit for Medium AQI
#define DISTANCE_THRESHOLD 7 // Distance in cm for covering the sensor
#define PAGE_INTERVAL 3000 // Interval in milliseconds to change pages
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Update the pin numbers according to your setup
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
const int rainSensorPin = 3;
const int gasSensorAnalogPin = A0;
int currentPage = 0; // Variable to store the current page
unsigned long lastChangeTime = 0; // Variable to store the last time the page was changed
void setup() {
lcd.begin(16, 2);
dht.begin();
Serial.begin(9600);
pinMode(rainSensorPin, INPUT);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int rainStatus = digitalRead(rainSensorPin);
int gasAnalog = analogRead(gasSensorAnalogPin);
// Calculate AQI percentage
int gasPercentage = map(gasAnalog, 0, 1023, 0, 100); // Map directly from 0% bad to 100% good
// Debugging print statements
Serial.print("Gas Analog Value: ");
Serial.println(gasAnalog);
Serial.print("Mapped Gas Percentage: ");
Serial.println(gasPercentage);
// Determine AQI category
String gasStatus;
if (gasPercentage <= GAS_LOW_THRESHOLD) {
gasStatus = "Low";
} else if (gasPercentage <= GAS_MEDIUM_THRESHOLD) {
gasStatus = "Medium";
} else {
gasStatus = "High";
}
delay(100);
unsigned int uS = sonar.ping();
int distance = uS / US_ROUNDTRIP_CM;
if (distance >= DISTANCE_THRESHOLD) {
if (millis() - lastChangeTime >= PAGE_INTERVAL) {
currentPage = (currentPage + 1) % 2; // Swap pages every PAGE_INTERVAL milliseconds
lastChangeTime = millis(); // Update the last change time
}
} else {
lastChangeTime = millis(); // Reset the last change time if an object is detected
}
lcd.clear();
if (currentPage == 0) {
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print((int)t - 2); // Display temperature as an integer
lcd.print((char)223); // Degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Rain: ");
lcd.print(rainStatus == HIGH ? "Yes" : "No");
} else if (currentPage == 1) {
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print((int)h); // Display humidity as an integer
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("AQI: ");
lcd.print(gasStatus);
lcd.print(" ");
lcd.print(gasPercentage); // Display AQI percentage
lcd.print("%");
}
// Print to Serial for debugging
Serial.print("Temp: ");
Serial.print((int)t); // Print temperature as an integer
Serial.print(" C, Humidity: ");
Serial.print((int)h); // Print humidity as an integer
Serial.print(" %, Rain: ");
Serial.print(rainStatus == HIGH ? "Yes" : "No");
Serial.print(", Gas Level: ");
Serial.print(gasStatus);
Serial.print(" (");
Serial.print(gasPercentage); // Print gas percentage
Serial.println("%)");
delay(1000);
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the sensors and LCD, reads data from the sensors, and displays the information on the LCD. It also prints the sensor data to the serial monitor for debugging purposes. The code includes functionality to switch between different display pages based on a time interval or the presence of an object near the ultrasonic sensor.