↩ All projects and sketches Deze pagina in het Nederlands
ESP8266 · ESP32 · TM1637

TM1637 display: NTP clock with ESP8266 or ESP32

TM1637 7-segment LED display connected to an ESP8266
TM1637 on Amazon — 7-segment LED display, driven over 2 pins (CLK/DIO)

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.

What do you need for this?

TM1637 display + ESP8266

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
}

How does it work?

Where are the libraries? In the many examples of clocks you find on the Internet, you see "NTPClient.h", "Time.h", "TimeLib.h" and "WiFiUDP.h". Where are those in this sketch? Short answer: you don't need those. Time.h is built into the ESP core. So we should not add these libraries to the list of the Arduino IDE, and we should not declare them in these sketches.

TM1637 display + ESP32

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.

More displays with the ESP8266 / ESP32

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.