Digital Clock using  Network Time protocol , NO RTC

Digital Clock using Network Time protocol , NO RTC

Today we will learn about network time protocol or NTP which can provide time using network and we will make a clock using it.

Network Time Protocol.

NTP is a network protocol for clock synchronization amongst various computer systems. 

NTP synchronizes all the partaking computer systems in a time range of a few milliseconds of the Coordinated Universal Time. 

NTP is a standard internet protocol used to which can synchronize computer clocks which can be done using a cheap RTC module but they are not perfectly accurate. 

Here we will use a Node MCU to get the time from the Network. We also use an ESP8266 but we just need a proper Wi-Fi connection . 

Now open the Arduino IDE and make sure that ESP8266 boards are installed in your Arduino IDE.

Now the next step is to install NTP Client Library in your Arduino IDE.

Once done with this, you can try the examples given in the library or you can copy my code.

Code

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

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

const long utcOffsetInSeconds = 19800;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

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

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.print(daysOfTheWeek[timeClient.getDay()]);
  Serial.print(", ");
  Serial.print(timeClient.getHours());
  Serial.print(":");
  Serial.print(timeClient.getMinutes());
  Serial.print(":");
  Serial.println(timeClient.getSeconds());
  //Serial.println(timeClient.getFormattedTime());

  delay(1000);
}

Before uploading the code make sure that you put your Wi-Fi SSID and  Password in the IDE.

you also need to edit the offset time according to UTC + X. e.g. for India the timeline is UTC + 5:30 which is 19800. 

you can easily calculate your offset time according to your timeline. 

After editing the offset time hit the upload button and upload the code into the NodeMCU Board.

After uploading the code, open the serial monitor and there you will be able to see the time and date on the serial monitor.

To see the output on the display we are going to use the 1.8 inch display.

Connect TFT display to the NodeMCU as per the schematic shown below.

Copy this modified code and open it in arduino ide.

#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// ST7735 TFT module connections
#define TFT_RST   D4     // TFT RST pin is connected to NodeMCU pin D4 (GPIO2)
#define TFT_CS    D3     // TFT CS  pin is connected to NodeMCU pin D4 (GPIO0)
#define TFT_DC    D2     // TFT DC  pin is connected to NodeMCU pin D4 (GPIO4)
// initialize ST7735 TFT library with hardware SPI module
// SCK (CLK) ---> NodeMCU pin D5 (GPIO14)
// MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
 
float p = 3.1415926;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

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

WiFiUDP ntpUDP;
//NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 19800, 60000);
NTPClient timeClient(ntpUDP);

String formattedDate;
String dayStamp;
String timeStamp;

void setup(void) {
  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
  }

  timeClient.begin();
  timeClient.setTimeOffset(19800);
  
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
 tft.setRotation(1);
  uint16_t time = millis();
  tft.fillScreen(ST7735_BLACK);
  time = millis() - time;
  
  delay(500);
 
  // large block of text
  tft.fillScreen(ST7735_BLACK);
//  testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST7735_WHITE);
  delay(1000);
 
}


void loop() {
  timeClient.update();
  tft.setTextColor(ST7735_YELLOW);
  tft.setCursor(10,30);
  tft.setTextSize(3);
  tft.println(timeClient.getFormattedTime());
  tft.setCursor(30, 80);
  tft.setTextSize(2);
  tft.println(daysOfTheWeek[timeClient.getDay()]);
  
  

  delay(1000);
  tft.fillScreen(ST7735_BLACK);
}

Before you upload the code, make sure you have ST7735 libraries in your Arduino IDE and then upload the code. 

Hit the upload button and then wait for a few seconds for the code to be uploaded.

After uploading the code we can see our time  and day on our TFT display.

To make it look more professional lets make a PCB of it.

Designing the PCB

To design the circuit and PCB, we used EasyEDA which is a browser based software to design PCBs.

Designing the circuit works like in any other circuit software tool, you place some components and you wire them together. Then, you assign each component to a footprint.

Having the parts assigned, place each component. When you’re happy with the layout, make all the connections and route your PCB.

Ordering the PCBs at PCBWay

This project is sponsored by PCBWay. PCBWay is a full feature Printed Circuit Board manufacturing service.

Turn your DIY breadboard circuits into professional PCBs – get 10 boards for approximately $5 + shipping (which will vary depending on your country).

Once you have your Gerber files, you can order the PCB. Follow the next steps.

1. Download the Gerber files – click here to download the .zip file.

2. Go to PCBWay website and open the PCB Instant Quote page. 

3. PCBWay can grab all the PCB details and automatically fills them for you. Use the “Quick-order PCB (Autofill parameters)”.

4. Press the “+ Add Gerber file” button to upload the provided Gerber files.

And that’s it. You can also use the OnlineGerberViewer to check if your PCB is looking as it should.

Now select the shipping method , the one you prefer and has cost efficient.

You can increase your PCB order quantity and change the solder mask color. I’ve ordered the Black color.


Once you’re ready, you can order the PCBs by clicking “Save to Cart” and complete your order.

After approximately one week using the DHL shipping method, I received the PCBs at my place.As usual, everything comes well packed, and the PCBs are really high-quality. 

The letters on the silkscreen are really well-printed and easy to read. Additionally, the solder sticks easily to the pads.

One thought on “Digital Clock using Network Time protocol , NO RTC

Leave a Reply

Your email address will not be published. Required fields are marked *