ST7789 TFT Display Clock with DS3231 RTC Module, DHT11 Humidity & Temperature Sensor Using Arduino

preview_player
Показать описание
ST7789 TFT Display Clock with DS3231 RTC Module, DHT11 Humidity & Temperature Sensor Using Arduino
Code is in the comment box below. Thanks.
Please Subscribe my Channel. ThankYou!
Рекомендации по теме
Комментарии
Автор

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <RTClib.h>
#include <DHT.h>

// --- ST7789 Pins ---
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8

// --- DHT11 Setup ---
#define DHTPIN 7 // Connect DATA pin here
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// --- Initialize Display & RTC ---
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;

// --- Track previous values ---
int prevHour = -1, prevMinute = -1, prevSecond = -1;
int prevDay = -1, prevMonth = -1, prevYear = -1;

// --- For timing DHT updates ---
unsigned long lastDHTRead = 0;
const unsigned long DHT_INTERVAL = 5000; // 5 seconds

void setup() {
Serial.begin(9600);
while (!Serial);

Serial.println(F("ST7789 + DS3231 + DHT11"));

// Initialize display
tft.init(240, 240, SPI_MODE2);
tft.setRotation(2);


// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

// Initialize DHT11
dht.begin();
delay(2000); // Allow DHT sensor to stabilize

// Initial time/date and DHT display
DateTime now = rtc.now();
updateTimeDisplay(now);
updateDateDisplay(now);
updateDHTDisplay();

// Footer label
tft.setTextSize(2);

tft.setCursor(140, 210);
tft.print("DoIt.20");
}

void loop() {
DateTime now = rtc.now();

// Update time if changed
if (now.hour() != prevHour || now.minute() != prevMinute || now.second() != prevSecond) {
updateTimeDisplay(now);
prevHour = now.hour();
prevMinute = now.minute();
prevSecond = now.second();
}

// Update date if changed
if (now.day() != prevDay || now.month() != prevMonth || now.year() != prevYear) {
updateDateDisplay(now);
prevDay = now.day();
prevMonth = now.month();
prevYear = now.year();
}

// Update DHT every 5 seconds
if (millis() - lastDHTRead >= DHT_INTERVAL) {
updateDHTDisplay();
lastDHTRead = millis();
}

delay(100); // Small delay for loop stability
}

// --- Display Functions ---

void updateTimeDisplay(DateTime now) {
tft.setTextSize(4);
tft.setCursor(20, 50);

tft.fillRect(20, 50, 220, 40, ST77XX_BLACK);

if (now.hour() < 10) tft.print("0");
tft.print(now.hour()); tft.print(":");

if (now.minute() < 10) tft.print("0");
tft.print(now.minute()); tft.print(":");

if (now.second() < 10) tft.print("0");
tft.print(now.second());
}

void updateDateDisplay(DateTime now) {
tft.setTextSize(3);
tft.setCursor(20, 90);
tft.fillRect(20, 90, 200, 30, ST77XX_BLACK);

tft.print(now.day()); tft.print("/");
tft.print(now.month()); tft.print("/");
tft.print(now.year());
}

void updateDHTDisplay() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();

// Validate readings
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Temp: "); Serial.print(temp); Serial.print(" C, ");
Serial.print("Humidity: "); Serial.print(hum); Serial.println(" %");

tft.setTextSize(2);

tft.setCursor(20, 120);
tft.fillRect(20, 120, 200, 60, ST77XX_BLACK); // Clear area
tft.print("Temp: ");
tft.print(temp, 1); // One decimal
tft.println(" C");

tft.setCursor(20, 140);
tft.fillRect(20, 140, 200, 60, ST77XX_BLACK);
tft.print("Hum: ");
tft.print(hum, 1);
tft.println(" %");
}

doit.
Автор

Your project is very good, I tested it with Arduino Uno R3 and it works very well. I am interested in whether the project also works with Arduino Nano V3? Has anyone tried it with Nano V3?

noaptesdaniel
join shbcf.ru