Real-Time Clock with an ESP8266 + TM1637 + NTPClient

Building a real-time clock (RTC) using an ESP8266 and the NTPClient library is a common and useful project, as it allows you to keep accurate time in your IoT projects. The NTPClient library allows your ESP8266 to synchronize its time with Network Time Protocol (NTP) servers on the internet.

 

 

Here’s a step-by-step guide to creating a real-time clock with an ESP8266 and the NTPClient library:

Materials Needed:

  1. ESP8266 Development Board (such as NodeMCU) (Affiliate) – https://s.click.aliexpress.com/e/_DD3JQhj
  2. USB cable for programming and power
  3. Breadboard and jumper wires (Affiliate) – https://s.click.aliexpress.com/e/_Dl5kuk1
  4. A stable internet connection (Duh..)

Software Requirements:

  1. Arduino IDE installed on your computer.
  2. NTPClient library for Arduino. You can install it via the Arduino Library Manager.

 

Wiring:

  • Connect your ESP8266 board to your computer using a USB cable.
  • If you are using a NodeMCU or similar board, there is no need for an external crystal oscillator or RTC module since the ESP8266 itself has built-in WiFi and can synchronize time over the internet.

Coding:

Here’s a sample code to create a real-time clock using the ESP8266 and NTPClient library:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>

// Replace with your network credentials
const char* ssid = "your ssid";
const char* password = "your password";

// NTP Server details
const char* ntpServerName = "pool.ntp.org";
const int utcOffsetInSeconds = 3600;  // Your timezone offset in seconds (e.g., GMT+1)

// Create UDP client for NTP
WiFiUDP udpClient;

// Create an NTP client instance
NTPClient timeClient(udpClient, ntpServerName, utcOffsetInSeconds);

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize the NTP client
  timeClient.begin();
}

void loop() {
  // Update the NTP client to get the current time
  timeClient.update();

  // Retrieve and print the current time
  Serial.print("Current time: ");
  Serial.println(timeClient.getFormattedTime());

  delay(5000); // Update the time every 5 seconds
}

 

  • Replace "your_SSID" and "your_PASSWORD" with your Wi-Fi network credentials.
  • The code sets up a connection to the NTP server and prints the current time every second. You can replace the Serial.println() line with your own code to use the time for your specific application
  • Change your timezone in utcOffsetInSeconds

Upload the Code:

  • Select your ESP8266 board and the correct COM port (111520) from the Arduino IDE.
  • Click the “Upload” button to upload the code to your ESP8266.

Monitor Serial Output:

  • Open the Arduino IDE Serial Monitor (Tools > Serial Monitor) to view the current time being printed.

Testing:

After uploading the code, your ESP8266 should connect to your Wi-Fi network and start fetching time from the NTP server. You can use this synchronized time for various applications, such as scheduling events, logging timestamps, or displaying the current time on an OLED display.

 

 

Adding a TM1637 display

Components Required:

  1. ESP8266 Development Board (such as NodeMCU) (Affiliate) – https://s.click.aliexpress.com/e/_DD3JQhj
  2. TM1637 display module (Affiliate) – https://s.click.aliexpress.com/e/_DCwvPgx
  3. Breadboard and jumper wires
  4. USB cable for power and programming
  5. A computer with the Arduino IDE installed

Connections:

Connect the components as follows:

  • TM1637 CLK to D5 on the ESP8266.
  • TM1637 DIO to D6 on the ESP8266.
  • Connect VCC to a +4.7V External Regulated Power Supply (or to 5V on the Esp8266 <- not recommended because this Wemos D1 Mini is only 3.3v logic tolerant, we can see this on the tm1637 Datasheet that the logic value is 0.7*VCC witch give us approx. +3.3v of Logic Level Voltage and this is OK if we donĀ“t want to burn the Little Guy)
  • Connect the GND pin of the TM1637 to the GND pin of the ESP8266/Global GND.

 

 

Instructions:

  • Install the required libraries (NTPClient and TM1637Display) using the Arduino Library Manager.

 

 

 

 

 

  • Replace "your_SSID" and "your_PASSWORD" with your Wi-Fi network credentials.
  • Connect your ESP8266 and TM1637 display as described in the “Connections” section.
  • Upload the sketch to your ESP8266 board using the Arduino IDE.
  • Open the Serial Monitor to monitor the connection status and time updates.
  • Your clock should now display the time in HH:MM format, and it will automatically synchronize with an NTP server to keep accurate time.

 

Arduino Sketch (Code):

 

#include <TM1637Display.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

// Replace with your network credentials
const char* ssid = "your SSID";
const char* password = "your Password";

// Define the NTP client and server details
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

// Pins for TM1637 display
#define CLK_PIN 14 // (D5)
#define DIO_PIN 12 // (D6)

// Create a TM1637Display object
TM1637Display display(CLK_PIN, DIO_PIN);

void setup() {
  // Start serial communication for debugging
  Serial.begin(115200);

  // Connect to Wi-Fi
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize the NTP client and get the time
  timeClient.begin();
  timeClient.setTimeOffset(3600);  // Set your timezone offset in seconds (e.g., GMT+1)

  // Initialize the display
  display.setBrightness(0x4); // Adjust the brightness (0x00 to 0x0f)
  Serial.println("Clock setup complete");
}

void loop() {
  // Update the NTP client to get the current time
  timeClient.update();

  // Get the current time
  int hours = timeClient.getHours();
  int minutes = timeClient.getMinutes();

  // Display the time on the TM1637 display
  display.showNumberDecEx((hours * 100) + minutes, 0b01000000, true);

  // Print the time to Serial for debugging
  Serial.print("Time: ");
  Serial.print(hours);
  Serial.print(":");
  Serial.println(minutes);

  delay(2000); // Update every second
}

 

 

 

 

 

Conclusion:

With this setup, your ESP8266-based real-time clock will always display the correct time, even if the device is restarted or powered off, thanks to the NTPClient library. You can further customize the code to display additional information, such as seconds or the date, if desired.