#include <RTClib.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
#define DHT22_PIN 7
#define DHTTYPE DHT22
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
LiquidCrystal_I2C MyLCD(0x27, 20, 4); // Creates I2C LCD Object With (Address=0x27, Cols=16, Rows=2)
DHT DHT22_Sensor(DHT22_PIN, DHTTYPE);
byte Degree[] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup () {
Serial.begin(9600);
// SETUP RTC MODULE
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
// automatically sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// manually sets the RTC with an explicit date & time, for example to set
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
MyLCD.init();
MyLCD.backlight();
MyLCD.setCursor(0, 0);
DHT22_Sensor.begin();
sensors.begin();
// Start the LCD and turn on the backlight:
lcd.init();
lcd.backlight();
// Create a custom character:
lcd.createChar(0, Degree);
}
void loop () {
DateTime now = rtc.now();
Serial.print("Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
float DHT22_Humidity = DHT22_Sensor.readHumidity();
float DHT22_TempC = DHT22_Sensor.readTemperature(); // Read Temperature(°C)
// Print Temperature Reading To The I2C LCD
MyLCD.clear();
MyLCD.print("Humidity: ");
MyLCD.print((int)DHT22_Humidity);
MyLCD.print("%");
MyLCD.setCursor(0, 1);
MyLCD.print("Temp: ");
MyLCD.print((int)DHT22_TempC);
MyLCD.print((char)223); // Show Degree Symbol (º)
MyLCD.print("C");
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
// Print the temperature on the LCD;
lcd.setCursor(0,2);
lcd.print("Water Temp:");
lcd.print(tempC);
lcd.write(0); // print the custom character
lcd.print("C");
delay(1000);
}
Loading
ds18b20
ds18b20