/**************************************************************************************************************
* Project: DHT11 and I2C exercise project for students | Ver: 1.0 Date: 26.6.2024 | by: Ciljak *
*-------------------------------------------------------------------------------------------------------------*
* Project description: *
* Exercise for students include data read from DHT11 temperature/ humidity sensor and further display them on *
* LCD 16 x 2 display with I2C interface. After one successful conversion and display data is blinked green LED*
* interconected on arduino module. Project is created for engancing practical skills with real hw and *
* crossrereference them against wokwi simulation https://wokwi.com/projects/401734873955442689 . *
***************************************************************************************************************
* Functions to implement/ implemented and changelog: (I) implemented, (T) to be done *
* 1. Reading data from DHT11 tem/ hum sensor and display them via serial interface. (I) *
* 2. Display obtained data also on 2x14 LCD with I2C interface. (I) *
* 3. Expand output interface with green LED indicating end of conversion/ display with 500mS light on. *
* *
**************************************************************************************************************/
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#define DHTPIN 2 // Pin, na ktorý je pripojený DATA výstup DHT11
#define DHTTYPE DHT22 // Definovanie typu snímača DHT11
#define LEDPIN 13 // Pin, na ktorý je pripojena LED bliknuca po refreshi údajov
// I2C connection of LCD is A4 - SDA and A5 - SCL line
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_PCF8574 lcd(0x27); // Inicializácia LCD displeja s I2C adresou 0x27
void setup() {
Serial.begin(9600);
pinMode(LEDPIN,OUTPUT);
dht.begin();
lcd.begin(16, 2); // Spustenie LCD displeja s rozmermi 16x2 znaky
lcd.setBacklight(255); // Zapnutie podsvietenia
Serial.println("DHT11 sensor and LCD setup completed.");
}
void loop() {
// Čítanie vlhkosti a teploty
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Kontrola, či čítanie bolo úspešné
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor error!");
return;
}
// Vypísanie hodnôt na sériové rozhranie
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
// Vypísanie hodnôt na LCD displej
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
// Pauza pred ďalším čítaním
delay(2000);
// Bliknutie po programovej slučke trvajuce 0,5S
digitalWrite(LEDPIN,HIGH);
delay(500);
digitalWrite(LEDPIN,LOW);
}