#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Library for LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2); // Set the LCD I2C address and size
#include "DHT.h"
#define DHTPIN 10 // DHT PIN 12
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#define trigPin 12 // Trigger pin of the HC-SR04
#define echoPin 11 // Echo pin of the HC-SR04
void setup() {
lcd.init(); // Initialize the LCD screen
lcd.backlight(); // Turn on the backlight
pinMode(trigPin, OUTPUT); // Set the trigger pin as output
pinMode(echoPin, INPUT); // Set the echo pin as input
Serial.begin(9600);
dht.begin();
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Set the trigger pin low for 2 microseconds
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Set the trigger pin high for 10 microseconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Measure the duration of the echo pulse
distance = duration * 0.034 / 2; // Calculate the distance in cm
lcd.setCursor(0,0); // Set the cursor to the first column of the first row
lcd.print("Distance: "); // Print the label
lcd.print(distance); // Print the distance value
lcd.print(" cm"); // Print the units
{delay(1000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
lcd.setCursor(0,1); // Set the cursor to the first column of the first row
lcd.print("Temprature: "); // Print the label
lcd.print(t); // Print the distance value
lcd.print(" *C\t"); // Print the units
}
}