This circuit is designed to monitor environmental conditions using various sensors and display the data on a 16x2 LCD. The circuit includes an Arduino UNO microcontroller, a DHT11 humidity and temperature sensor, a YL-83 rain sensor, an ultrasonic sensor, an MQ-2 gas sensor, a BMP180 barometric pressure sensor, and a 16x2 LCD display. The Arduino UNO reads data from the sensors and displays the information on the LCD. The circuit also includes resistors to ensure proper voltage levels for certain components.
Arduino UNO
16x2 LCD
DHT11 Humidity and Temperature Sensor
YL-83 Rain Sensor - Control Board
Ultrasonic Sensor
Resistor (1k Ohms)
Resistor (200 Ohms)
MQ-2 Gas Sensor
BMP180 Barometric Pressure Sensor
#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
}