Build a compact NTP clock with a TM1637 7-segment display — automatic daylight saving time, no hard-coded WiFi password, and just 18 lines of code.
Connections:
ESP8266 -> TM1637
=================
GND --------> GND
3V3 --------> VCC
D2 --------> DIO
D1 --------> CLK
Sketch for a simple clock:
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager/
#include <TM1637Display.h> // https://github.com/avishorp/TM1637/
struct tm tInfo; // https://cplusplus.com/reference/ctime/tm/
WiFiManager myWiFi;
TM1637Display ledSeg(5, 4); // scl (clk) -> GPIO5 (D1) / sda (dio) -> GPIO4 (D2).
void setup() {
Serial.begin(115200); // watch serial monitor in case of new WiFi-connection
myWiFi.autoConnect("TM1637_ESP"); // connect ESP to new wifi via GSM or PC in that case
ledSeg.setBrightness(2); // 0 .. 7
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "be.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}
void loop() {
getLocalTime(&tInfo); // sntp sync at startup & every hour from then on
ledSeg.showNumberDecEx(tInfo.tm_hour * 100 + tInfo.tm_min, 0x40 * (millis() % 1000 < 500), 1); // 0x40 = ":", 1 = leading zeros
}
#include <WiFiManager.h> — library to connect the ESP to the WiFi, including a web server if the WiFi data was not previously entered. If the connection fails, you will see on the serial monitor:
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "be.pool.ntp.org"); — this is the line that ensures that the correct time zone is used, the clock automatically adjusts to summer or winter time, and then indicates which NTP servers you wish to synchronize with.ledSeg.showNumberDecEx(ti.tm_hour * 100 + ti.tm_min, 0x40 * (millis() % 1000 < 500), 1); — the time is shown on the display with a colon (hidden or shown every half second), and with leading zeros.getLocalTime(&timeinfo); requests the time from the ESP8266, adjusted according to time zone and daylight saving time / standard time. On startup the internal clock is synchronized with the NTP server, thereafter the synchronization occurs every hour. This was extensively tested with callback functions.The following code for our "minimum clock" is adapted for both the ESP8266 and the ESP32. The default time synchronization interval for the ESP32 is 3 hours, for the ESP8266 it is 1 hour. The only modifications are the display GPIO connections:
Connections:
ESP32 ---> TM1637
=================
GND --------> GND
3V3 --------> VCC
G21 --------> DIO
G22 --------> CLK
Sketch:
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager/
#include <TM1637Display.h> // https://github.com/avishorp/TM1637/
struct tm tInfo; // https://cplusplus.com/reference/ctime/tm/
WiFiManager myWiFi;
#if defined(ESP32)
TM1637Display ledSeg(22, 21); // scl (clk) -> G22 / sda (dio) -> G21.
#elif defined(ESP8266)
TM1637Display ledSeg(5, 4); // scl (clk) -> GPIO5 (D1) / sda (dio) -> GPIO4 (D2).
#endif
void setup() {
Serial.begin(115200); // watch serial monitor in case of new WiFi-connection
myWiFi.autoConnect("TM1637_ESP"); // connect ESP to new wifi via GSM or PC in that case
ledSeg.setBrightness(2); // 0 .. 7
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "be.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}
void loop() {
getLocalTime(&tInfo); // sntp sync at startup & every hour from then on
ledSeg.showNumberDecEx(tInfo.tm_hour * 100 + tInfo.tm_min, 0x40 * (millis() % 1000 < 500), 1); // 0x40 = ":", 1 = leading zeros
}
We could extend the sketch so that when the clock establishes a WiFi connection during startup, it looks up its own location via an API. Then the time zone could be assigned automatically. Thus, you could create an NTP clock with the ESP that you could use anywhere in the world. After connecting to an available WiFi, it displays the correct local time.
Curious about other displays? Check out the full overview, including SSD1306, SH1106, ST7735, ST7789, e-paper and integrated displays like the Lilygo T-Display and the Waveshare ESP32-S3.