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

WiFi Internet Clock with Nano ESP32 and TFT Display

Wiring the Components

In this project, we use the powerful Arduino Nano ESP32, which retrieves the accurate time from internet servers (NTP) via its built-in Wi-Fi module. We use a 2.0" TFT LCD screen to display the time and date.

Connections (Nano ESP32 → TFT LCD):

IMPORTANT: In the Arduino IDE, under the Tools -> Pin Numbering menu, it is essential to select "By Arduino pin number" so that the pin labels in the code match the holes on the board!

Arduino Code

To function, you need the Adafruit GFX and Adafruit ILI9341 (or ST7789, depending on your screen) libraries. Install them via the Library Manager in the Arduino IDE.

// Arduino Nano ESP32 WiFi Clock with TFT Display (ST7789)
// Author: Borut Bukovnik - vadnica.org

#include <SPI.h>
#include <WiFi.h>
#include "time.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>

const char* ssid = "Enter_WiFi_Name";
const char* password = "Enter_Password";

const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;

#define TFT_CS   10
#define TFT_DC    8
#define TFT_RST   9

// Initialization for the ST7789 driver
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

void setup() {
    Serial.begin(115200);
    pinMode(TFT_RST, OUTPUT);
    digitalWrite(TFT_RST, LOW); delay(100);
    digitalWrite(TFT_RST, HIGH); delay(100);
    
    // Initialization of the ST7789 display (width: 240, height: 320)
    tft.init(240, 320);           
    tft.invertDisplay(true); 
    tft.setRotation(2);      
    tft.fillScreen(ST77XX_BLACK);
    
    tft.setCursor(40, 100);
    tft.setTextColor(ST77XX_WHITE);
    tft.setTextSize(2);
    tft.print("CONNECTING...");
    
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) { delay(500); }
    
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    tft.fillScreen(ST77XX_BLACK);
}

void loop() {
    static int last_second = -1;
    struct tm info;
    if (!getLocalTime(&info)) return;
    
    if (info.tm_sec != last_second) {
        last_second = info.tm_sec;
        display_clock(&info);
    }
}

void display_clock(struct tm *info) {
    char time_str[10], date_str[20];
    // Color selection based on night/day time
    uint16_t color = (info->tm_hour >= 22 || info->tm_hour < 6) ? ST77XX_RED : ST77XX_BLUE;
    
    tft.fillRect(0, 70, 320, 60, ST77XX_BLACK); 
    
    tft.setCursor(10, 80);
    tft.setTextColor(color);
    tft.setTextSize(4); 
    strftime(time_str, 10, "%H:%M:%S", info);
    tft.print(time_str);
    
    tft.setCursor(60, 160);
    tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
    tft.setTextSize(2);
    strftime(date_str, 20, "%m/%d/%Y", info); // Transformed to standard English date format
    tft.print(date_str);
    
    tft.setCursor(10, 10);
    tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
    tft.setTextSize(1);
    tft.print("VADNICA.ORG CLOCK");
}

Required Material

  1. Arduino Nano ESP32: Microcontroller with built-in Wi-Fi.
  2. TFT LCD SPI 2.0": Color display (ILI9341 controller).
  3. Protoboard SET: For testing the circuit without soldering.
  4. Connecting Wires: For SPI communication.