⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

Project: Modern TFT Digital Clock

In this project, we will upgrade the classic clock. We will use the Arduino Nano ESP32, which offers incredible speed, and a TFT 2.0" SPI display for a colorful time output. The DS3231 module still ensures time precision.

Technical Reminder

Wiring Instructions

Connecting the components to the Arduino Nano ESP32:

Complete Project Code

To function, you need the Adafruit GFX and Adafruit ST7789 libraries, along with RTClib. The code is optimized for fast refreshing of only those parts of the display where the time actually changes.

// Digital Clock: Nano ESP32 + TFT 2.0 SPI (ST7789) + RTC DS3231
// Author: Borut Bukovnik - vadnica.org

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

// Pins for TFT 2.0 SPI
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;

void setup() {
    tft.init(240, 320);
    tft.setRotation(1);
    tft.fillScreen(ST77XX_BLACK);
    
    if (!rtc.begin()) {
        tft.setTextColor(ST77XX_RED);
        tft.print("RTC not found!");
        while (1);
    }
    
    tft.setTextColor(ST77XX_CYAN);
    tft.setTextSize(3);
    tft.setCursor(20, 20);
    tft.print("DIGITAL CLOCK");
}

void loop() {
    DateTime now = rtc.now();
    
    tft.setTextSize(5);
    tft.setCursor(50, 100);
    
    // Output time with a black background (prevents flickering)
    tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
    
    char buffer[9];
    sprintf(buffer, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
    tft.print(buffer);
    
    tft.setTextSize(2);
    tft.setCursor(80, 180);
    tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
    tft.print(now.day());
    tft.print(". ");
    tft.print(now.month());
    tft.print(". ");
    tft.print(now.year());
    
    float temp = rtc.getTemperature();
    tft.setCursor(20, 200);
    tft.setTextSize(2);
    tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
    tft.print("Temp: ");
    tft.print(temp);
    tft.print(" C");
    
    delay(1000);
}

Required Material