/*218540787 KF KHANYA*/
#include <DHT.h> // Include the DHT library for interfacing with the DHT sensor.
#include <LiquidCrystal.h> // Include the LiquidCrystal library for interfacing with the LCD.
// Define constants for pin connections.
#define DHTPIN 2 // Pin where the DHT22 sensor is connected.
#define DHTTYPE DHT22 // Type of DHT sensor being used.
#define LCD_RS 7 // LCD Register Select (RS) pin.
#define LCD_EN 6 // LCD Enable (EN) pin.
#define LCD_D4 5 // LCD Data pin 4 (D4).
#define LCD_D5 4 // LCD Data pin 4 (D4).
#define LCD_D6 3 // LCD Data pin 6 (D6).
#define LCD_D7 9 // LCD Data pin 7 (D7)
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object with the specified pin and sensor type.
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); // Create an LCD object with the specified pins.
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows.
dht.begin(); // Initialize the DHT sensor.
}
void loop() {
float temperature = dht.readTemperature(); // Read the temperature from the DHT sensor.
float humidity = dht.readHumidity(); // Read the humidity from the DHT sensor.
if (isnan(temperature)) { // Check if the temperature reading is not a valid number
lcd.setCursor(0, 0); // Set the LCD cursor to the first row, first column.
lcd.print("Error reading data"); // Display an error message on the LCD.
} else {
lcd.setCursor(0, 0); // Set the LCD cursor to the first row, first column.
lcd.print("Temp: "); // Display the text "Temp: " on the LCD.
lcd.print(temperature); // Display the temperature value on the LCD.
lcd.print(" C"); // Display the unit "C" for Celsius on the LCD.
lcd.setCursor(0, 1); // Set the LCD cursor to the second row, first column.
lcd.print("Hum: "); // Display the text "Hum: " on the LCD.
lcd.print(humidity); // Display the humidity value on the LCD.
lcd.print(" %"); // Display the unit "%" for relative humidity on the LCD.
}
delay(2000); // Delay for 2 seconds
}
}