#include <Adafruit_Sensor.h>
#include <DHT_U.h> // DHT sensor library
// Pin Definitions
#define DHTPIN 12 // Pin where DHT22 is connected
#define GAS_SENSOR_PIN 36 // Analog pin for potentiometer (simulated gas sensor)
#define COOLING_FAN_PIN 26 // Pin for cooling fan (simulated by an LED)
#define EXHAUST_FAN_PIN 14 // Pin for exhaust fan (simulated by an LED)
// DHT parameters
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
float temp;
const float tempThreshold = 35.0; // Temperature threshold for cooling fan
const int gasThreshold = 400; // Gas detection threshold (analog value)
// Setup function
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Setup pins as output
pinMode(COOLING_FAN_PIN, OUTPUT);
pinMode(EXHAUST_FAN_PIN, OUTPUT);
// Initially turn off both fans
digitalWrite(COOLING_FAN_PIN, LOW);
digitalWrite(EXHAUST_FAN_PIN, LOW);
Serial.println("Temperature and Gas monitoring system started...");
}
void loop() {
// --- Temperature Monitoring ---
sensors_event_t event;
dht.temperature().getEvent(&event);
// Check if the sensor is reading temperature correctly
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
temp = event.temperature;
Serial.print(F("Temperature: "));
Serial.print(temp);
Serial.println(F("°C"));
// Check if temperature exceeds threshold for cooling fan
if (temp > tempThreshold) {
Serial.println("Temperature exceeded threshold! Turning on cooling fan...");
digitalWrite(COOLING_FAN_PIN, HIGH); // Turn on cooling fan
} else {
Serial.println("Temperature below threshold. Cooling fan is off.");
digitalWrite(COOLING_FAN_PIN, LOW); // Turn off cooling fan
}
}
// --- Gas Monitoring ---
int gasValue = analogRead(GAS_SENSOR_PIN); // Read gas sensor (simulated by potentiometer)
Serial.print("Gas Sensor Value: ");
Serial.println(gasValue);
// Check if gas sensor value exceeds threshold for exhaust fan
if (gasValue > gasThreshold) {
Serial.println("Gas detected! Turning on exhaust fan...");
digitalWrite(EXHAUST_FAN_PIN, HIGH); // Turn on exhaust fan
} else {
Serial.println("No gas detected. Exhaust fan is off.");
digitalWrite(EXHAUST_FAN_PIN, LOW); // Turn off exhaust fan
}
delay(1000); // Wait 1 second before next reading
}