#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27,16,4);
int gas = A3;
const int DHTPIN = 2;
#define DHTTYPE DHT22 // Loại cảm biến DHT11
float humidity;
float temperature;
int gas_value;
DHT dht(DHTPIN, DHTTYPE);
int speaker = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
dht.begin();
pinMode(speaker, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
read_sensor();
}
void read_sensor(){
humidity = dht.readHumidity();
temperature = dht.readTemperature();
gas_value = analogRead(gas);
if (gas_value>500) tone(speaker, 1000);
else noTone(speaker);
print_lcd();
// Serial.println(humidity);
// Serial.println(temperature);
// Serial.println(gas_value);
// delay(1000);
}
void print_lcd(){
String line1 = "Temp:"+ String(temperature) + "C";
String line2 = "Hum :"+ String(humidity) + "%";
String line3 = "GAS :"+ String(gas_value)+" ";
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);
lcd.setCursor(0,2);
lcd.print(line3);
}