//20240614 prj22 RTC DHT sensor LCD I2C
//https://wokwi.com/projects/344892587898831442
//DHT-sensor-library examples
//https://wokwi.com/projects/305979285237137984 //RTC
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include <DHT.h> // DHT sensor library
#include <LiquidCrystal_I2C.h> // lcd I2C library
#include "RTClib.h" // RTC library
#define pled14 14 // pin 14 leg DHT22
// Library nicname
DHT dht(pled14, DHT22); // dht is nicname
LiquidCrystal_I2C lcd(0x27, 16, 2); // lcd is nicname 16 ตัวอักษร 2 แถว
RTC_DS1307 rtc; // rtc is nicname
// global variable
int temp; // DHT sensor Temperature
int hum; // DHT sensor Huminity
// RTC global variable ymd
int y; // now.year(); พศ +543;
int m; // now.month(); month
int d; // now.day(); day
// setup()
void setup(){
Serial.begin(115200); // Serial monitor
dht.begin(); // dht module activate
lcd.init(); // lcd module activate
lcd.backlight(); // lcd backlight activate
hello();
Ddate();
delay(5000);
}
// loop()
void loop(){
displaytemp();
}
// eof loop()
// display Temperature, Humidity
void displaytemp(){
lcd.clear();
temp = dht.readTemperature();
hum = dht.readHumidity();
// Serial monitor on screen to tracking
Serial.print("temp ");
Serial.print(temp);
Serial.print(" Hum ");
Serial.println(hum);
// diskplay on lcd I2C for work
lcd.setCursor(0, 0);
lcd.print("Temp ");
lcd.print(temp);
/*
lcd.setCursor(5, 0);
lcd.print(" ");
delay(1000);
*/
lcd.setCursor(8,0);
lcd.print("Hum ");
lcd.print(hum);
lcd.print(" ");
delay(1500);
}
// eof function displaytemp()
// function hello()
void hello(){
lcd.setCursor(0,0);
lcd.print(" Hello World");
lcd.blink();
//lcd.clear();
}
// eof function hello()
// lcd.print zero when >=1 month || day < 10
void lzeroDayMonth(int dm){
if (dm<10) {
lcd.print("0");
}
}
// Serial.print zero when >=1 month || day < 10
void SzeroDayMonth(int dm){
if (dm<10) {
Serial.print("0");
}
}
//
// function hello()
void Ddate(){
rtc.begin();
DateTime now = rtc.now();
y = now.year(); // พศ +543;
m = now.month(); // month
d = now.day(); // day
// display date on Serial monitor
Serial.print(y);
Serial.print(" ");
SzeroDayMonth(m);
Serial.print(m);
Serial.print(" ");
SzeroDayMonth(d);
Serial.println(d);
// display date on lcd I2C
lcd.setCursor(0,1);
lcd.print("Date ");
lcd.print(y);
lcd.print(" ");
lzeroDayMonth(m);
lcd.print(m);
lcd.print(" ");
lzeroDayMonth(d);
lcd.println(d);
}
// eof function Ddate()
// eof program