This circuit integrates an Arduino UNO microcontroller with a DHT11 humidity and temperature sensor, an HC-SR05 ultrasonic sensor, and a 16x2 I2C LCD display. The Arduino UNO reads data from the sensors and displays the information on the LCD. The DHT11 sensor measures humidity and temperature, while the HC-SR05 sensor measures distance. The LCD displays the sensor readings.
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
const int PINDHT = 10;
const int DHTTYPE = DHT11;
DHT dht(PINDHT, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int trigPin = 8;
const int echoPin = 9;
long thoigian;
float khoangcach;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
dht.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
thoigian = pulseIn(echoPin, HIGH);
khoangcach = thoigian * 0.0347 / 2.0;
float doam = dht.readHumidity();
float nhietdo = dht.readTemperature();
if (isnan(doam) || isnan(nhietdo)) {
lcd.setCursor(0, 0);
lcd.print("Loi cam bien DHT");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DA:");
lcd.setCursor(3, 0);
lcd.print((int)doam);
lcd.setCursor(8, 0);
lcd.print("ND:");
lcd.setCursor(11, 0);
lcd.print(nhietdo);
lcd.setCursor(0, 1);
lcd.print("KC:");
lcd.setCursor(4, 1);
lcd.print(khoangcach, 4);
}
Serial.print(" ");
Serial.print(khoangcach, 4);
Serial.println();
delay(300);
}
This code initializes the DHT11 sensor, the HC-SR05 sensor, and the 16x2 I2C LCD. In the setup
function, the serial communication, LCD, and sensors are initialized. The loop
function continuously reads data from the sensors and displays the humidity, temperature, and distance on the LCD. The distance is also printed to the serial monitor.