View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Intrigued by how accurate timekeeping is achieved using an ESP32 microcontroller and transparent OLED displays? Discover the magic of Network Time Protocol (NTP) in this deep dive into atomic clocks, stratum devices, and GPS satellites.

Related Content
Transcript

[0:00] I bet you’ve never seen this before - it’s a transparent OLED display - I’m probably
[0:05] the first person to…
[0:07] Ok, so you’ve seen it before.
[0:09] But I bet you’ve never seen a transparent OLED being used as a clock - I can’t imagine
[0:14] anyone…
[0:15] Ok, so that’s been done to death as well, why am I even bothering to show you this.
[0:21] There’s no denying it’s pretty cool and there are some impressive things you can do
[0:26] like stacking multiple displays to make a 3D display.
[0:29] However, what I find exciting about this project is that the time and date are correct.
[0:35] Now, I can already hear your mouse moving to click away from this video but stick with me.
[0:41] I can remove power from the microcontroller and it automatically knows what the time is when power is restored.
[0:48] The microcontroller I’m using - the ESP32 - doesn’t have a built-in real-time clock
[0:53] and there’s no battery backup.
[0:55] How does this work?
[0:57] I can see you’re still not impressed?
[0:59] We’ve got used to knowing what the time is - our computer displays it all the time
[1:03] and so does our phone.
[1:05] But how do they actually know what the time is?
[1:08] Would you believe me if I said that my ESP32 was getting its time from an atomic clock?
[1:14] Would you believe me if I told you that my ESP32 is probably getting its time from multiple
[1:20] atomic clocks that are orbiting the earth at around 14,000 km/hour?
[1:26] Let me introduce you to the Network Time Protocol - or NTP.
[1:31] This is what I’m using in my clock and it’s most likely what your computer and phone are
[1:35] using as well.
[1:36] But first, I want to give a quick plug to PCBWay who sponsors the channel - I’ve got
[1:41] a bunch of videos coming up using PCBs that I’ve had manufactured by them, so don’t
[1:46] forget to hit that subscribe button and the bell so you don’t miss anything.
[1:50] So what is the Network Time Protocol?
[1:54] Wikipedia says:
[1:55] The Network Time Protocol (NTP) is a networking protocol for clock synchronization between
[2:01] computer systems over packet-switched, variable-latency data networks.
[2:07] What’s impressive is that this protocol is that it has been In operation since 1985,
[2:12] it’s one of the oldest Internet protocols in current use.
[2:16] It’s probably been around longer than a lot of you have been alive.
[2:20] How does it work?
[2:22] Well, the first thing we need is some accurate clocks.
[2:26] The most accurate clocks we’ve managed to create are atomic clocks.
[2:29] There are a few flavours of this - we can use vibrating Caesium atoms - these vibrate
[2:34] at nine billion one hundred ninety-two million six hundred thirty-one thousand seven hundred
[2:40] seventy (9,192,631,770) times per second.
[2:41] That’s a big number!
[2:43] Another popular choice is to use rubidium and you also have the very excitingly named
[2:50] hydrogen maser clock.
[2:50] Obviously not everyone has an atomic clock lying around - they are pretty expensive.
[2:56] So a lot of people rely on Global Navigation Satellite Systems, such as GPS.
[3:01] A typical GPS satellite will have up to four atomic clocks, we can use these atomic clocks
[3:07] to create our accurate clock.
[3:10] These super-accurate clocks are known in the NTP world as “Stratum 0” devices.
[3:16] They don’t offer any time services over the network they just provide the time.
[3:21] Connected directly to the Stratum 0 devices we have Stratum 1 devices - these devices
[3:27] do offer time synchronisation services over the network,
[3:31] Below these, we have Stratum 2 devices.
[3:33] These devices are connected over a network connection to Stratum 1 devices.
[3:37] And below these we have Stratum 3 devices - this goes on up to a maximum level of Stratum 15.
[3:43] Stratum 16 indicates that the device is not synchronised and you shouldn’t use it.
[3:48] So let’s have a look at this in action.
[3:51] We can install an application called WireShark on our computer.
[3:55] This lets us look at all the network traffic that is being generated on our network.
[4:00] The NTP protocol runs over UDP port 123.
[4:03] So we can tell WireShark that we only want to see this traffic.
[4:07] Let’s generate some requests to an NTP server.
[4:11] We can see the packet going out from our computer and we can see the response coming back in.
[4:16] If we look at the response we can see that we were talking to a Stratum 1 device.
[4:21] And we can also see that it’s got its time from GPS.
[4:24] We’ve just got time from some an atomic clock in orbit around the earth.
[4:29] It’s mind-blowing.
[4:30] There are also 4 timestamps in the response.
[4:36] The reference, origin, receive and transmit timestamps.
[4:40] These are used by our computers to compensate for network delays.
[4:44] We have a delay due to sending a packet from our computer to the server, and we have a
[4:49] delay from the server sending a packet to our computer.
[4:53] The origin timestamp is the time the request left our computer.
[4:57] the receive timestamp is the time the request arrived at the server
[5:01] the transmit timestamp is the time the server sent its reply
[5:05] Our computer also records the timestamp the reply arrived.
[5:09] From these four timestamps, we can estimate the network delay using this formula.
[5:14] We can also estimate the offset from our local clock from the server clock.
[5:20] These values are then used to adjust the local clock so that it matches the correct time.
[5:25] In most applications having time change can have a disastrous effect - particularly if
[5:30] time goes backwards.
[5:33] Imagine we’re collecting a sequence of events coming into our system, if the time on our
[5:37] machine goes backwards our events will suddenly be recorded in a different order from when
[5:42] they occurred.
[5:43] To prevent this most systems will gradually adjust the local clock to match the server
[5:48] time and won’t allow time to go backwards.
[5:51] Obviously for this to work you need to be starting with a time that is already almost
[5:56] correct.
[5:57] So, this is all pretty complicated - how do we actually use it on the ESP32?
[6:02] Well, there are actually only 2 lines of code required.
[6:07] And these two lines are all the code that is going to be in this video.
[6:11] First, we need to tell the system which NTP server we want to use.
[6:16] And then once we’ve done that we can just use this line of code to get the time,
[6:21] This will automatically refresh the time from the time server - that’s all we need to do.
[6:25] It’s magic.
[6:26] Now you may have noticed that I’m using a pool of machines to get my time.
[6:30] This is much more reliable than using just one machine as an individual machine may go down and you won’t be able to get the time.
[6:37]

[6:38] Two lines of code to connect to an atomic clock.
[6:41] Pretty cool if you ask me.
[6:43] There is one last thing that is worth mentioning.
[6:45] A natural thought is to assume that you should always try and connect to a Stratum 1 device
[6:50] as that should have the most accurate time - it is closest to the actual clocks
[6:57] The problem with this thought is that it ignores any network delays.
[7:00] A stratum 1 device may be quite far away from you in terms of network hops.
[7:05] And the route from your machine to the server may be completely different from the route
[7:09] back from the server to your machine.
[7:11] This can introduce quite a large error in your calculations.
[7:15] It may be much more sensible to connect to a higher stratum device.
[7:20] These may be closer to you and may be hosted by companies that have high-speed direct connections
[7:24] to somewhere closer to a lower stratum device.
[7:27] The other thing to bear in mind is that you might be connecting to a server that is overloaded if you all try to connect to the same stratum 1 device.
[7:35] This could cause your time to be completely out of whack as the server might be struggling to serve all the requests that are coming in.
[7:42] So it’s much better to use either a pool of machines or use a time server that isn’t a stratum 1 device.
[7:49] So, hope this was interesting, don’t forget to like and subscribe - I’ll see you in the next video!


HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts