We are going to learn how to build this cute little digital clock that communicates with
NTP servers and displays network or internet time. The idea originated from this week’s
video that was released by the guy with the Swiss accent and is based on his sketch.
The traditional way to build a clock is by using a time keeping module, also called a
real time clock or RTC. These modules cost money, they require a battery to operate,
they need to be set manually which itself takes time and this results in errors. They
also do not automatically adjust for daylight savings and thus need to be manually corrected.
Obtaining and using network time gets rid of all of these issues.
Here’s what the sketch modules look like. We start by connecting to a WiFi network to
get internet access. We then obtain the network time from the NTP servers. The sketch uses
a pool of NTP servers and you can change this if needed. We then convert this network time
into local time depending on the timezone. I’ll be displaying UK time but you can visit
this link and copy the string corresponding to your time zone. This string is responsible
for any adjustments that are needed during the course of a year.
To build this project, you will need a WeMos D1 mini or compatible board that uses the
ESP8266 chipset along with an OLED module. The sketch should also work with the ESP32
boards but I haven’t tested this. Download the sketch and open it up in the Arduino IDE.
Start by adding your network credentials as we need to connect to a WiFi network. Then,
make sure you add the correct time zone information. You can visit the link in the sketch and obtain
the string corresponding to your time zone. And that’s all you need to change in the
sketch.
Let’s go over the sketch before we upload it. We use the U8g2 library for the OLED module
and we initialize it in the 8x8 mode as we will only be displaying text. We setup the
serial port and start connecting to the WiFi network. The original sketch had a 2 second
timeout for the WiFi connection but this was too short and caused the board to get stuck
in a boot loop. I increased this to 20 seconds which should be more than enough for the board
to connect to a WiFi network. If for some reason it fails to connect, then it will restart
the board and attempt to establish a connection again. Once connected, we set the NTP server,
time zone and try to communicate with the NTP server to obtain the time. We then display
this to the serial monitor and OLED module.
The loop function obtains the time and prints it out every second. The time function communicates
with the NTP server every hour and this reduces the network traffic which also reduces the
overall current consumption. The show time function prints the local time to the serial
terminal and also displays this to the OLED module. The day of week obtained is a number
and we use the getDOW function to convert this to a string that we can display on the
OLED module.
Before you can download the sketch, you need to install the u8g2 library. Open up the library
manager from the tools menu and type in u8g2. Select and install this library. We also need
to make sure we have the board support package for the ESP8266 boards. To do this, open up
the Arduino preferences window and add the following URL to the additional board manager
URL section. Then, open up the boards manager and type in ESP8266 and install the package
that shows up. Once done, plug in the board and make sure you have the right settings
selected from the tools menu. Your COM port will vary depending on your system. Hit the
upload button and wait for it to complete. Once done, open up the serial monitor and
press the reset button. The board should connect to the network and print out the local time.
Make sure the time is correct and update the timezone if needed.
Connect the OLED module using the wiring diagram shown here. The OLED module has a 3.3V regulator
on board and uses 3.3V for the data-lines which is compatible with the microcontroller
board. Once wired, power on the board and you should see the time on the OLED module.
I will be using this model from thingiverse for the enclosure. The board sits nicely on
this cover which also allows for easy access to the microUSB port. The OLED module sits
right at the other end of the shell so make sure you add wire of suitable length. I decided
to solder some wires to the OLED module and add a thin layer of double-sided tape to it.
I then placed it in position and also added a bit of hot glue to the top section, near
the wires. I then soldered the wires directly to the board using the same wiring diagram
as before which is also shown here. I then pushed the back cover in place and applied
some glue to hold it together. Finally, I plugged in the microUSB cable to watch it
in action.
The total module cost for this build was less than $9. You can obtain the modules at a better
price by purchasing them from other websites that tend to have longer delivery times.
I will most likely create a version of this that obtains and displays online weather,
so do subscribe to stay notified. Don’t forget to hit that like button if you liked
this build. Thank you for watching and I will see you in the next one.