#include <Wire.h>
#include <LiquidCrystal_I2C.h> // importing libraries related to hardware
#include <DHT.h>
#define DHTPIN 10 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // Sets the type of DHT 22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Pins the LCD is connected too
int ledPin1 = 4; // where the red LED is connected to
int ledPin2 = 5; // where the green LED Is conneected to
void setup() {
Serial.begin(10000);
Serial.println("If the gas concentration exceeds 50%, the detector will send out a warning and the red LED will light up.");
lcd.begin(20, 4); // initilizing the lcd
pinMode(ledPin1, OUTPUT); // setting the mode of the pin the leds are connected to, they are both off at the start
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);
dht.begin(); // initlizing the sensor
}
void loop() { // we are using humidity as a proxy for gas due to there being no gas sensor
float humidity = dht.readHumidity(); // inputing the values for humidity and storing it as a float
float temperature = dht.readTemperature(); //inputing value for temp and storing it as a float
if (isnan(humidity) || isnan(temperature)) { //checking if humidity or temperature are not a number
Serial.println("There was an error in reading the data"); // if they are Nan it prints out there was an error
return;
}
if (humidity > 50) { // an if statement to check if humidity is above desired level
Serial.println("The gas concentration is dangerously high!");
lcd.clear(); //clear the lcd
lcd.setCursor(0, 0); //sets cursor of lcd to correct level
lcd.print("DANGEROUS GAS LEVEL!"); // displays warning
lcd.setCursor(0, 2); // sets cursor of lcd to lower level
lcd.print(" ");
lcd.print(humidity); // displayes gas level on lcd
lcd.print("%");
digitalWrite(ledPin1, HIGH); // turns red LED on
digitalWrite(ledPin2, LOW); // tunrs green LED off
}
else { // else the gas levels are within the safe level
Serial.println("Gas concentration is within safe levels."); //prints to console that levels are safe
digitalWrite(ledPin1, LOW); // turns off red led
digitalWrite(ledPin2, HIGH); // turns on green led
lcd.clear(); // clears the lcd screen
lcd.setCursor(0, 0); // sets the cursor of lcd to the correct level
lcd.print("Gas: ");
lcd.print(humidity); // displays the gas level on lcd
lcd.print("%");
lcd.setCursor(0, 2); // sets the cursor to lower level
lcd.print("Temp: ");
lcd.print(temperature); // displays the temperature on lcd
lcd.print("C");
}
Serial.print("Gas concentration:");
Serial.print(humidity); // prints out the gas level on console
Serial.print("% ");
Serial.print("Temperature: ");
Serial.print(temperature); // prints out the temperature to the console
Serial.println("°C");
delay(1000); // adds a delay to the loop to make it easier to read outputted values
}