close-circle
Close
0%
0%

Raspberry Pi GPS Stratum 1 NTP server

The cheapest and simplest way to make a highly accurate time server for your network or the global NTP pool

Similar projects worth following
close
NTP, the Network Time Protocol, is a service that distributes accurate time worldwide over the Internet. NTP's fundamental assumption is that if two clocks read different times, at least one of them is wrong. As such, NTP is designed to pick from amongst the best sources available to synchronize the local clock. An NTP client can also act as an NTP server, further distributing accurate time to clients that might consult it. As the path of trust moves from a source to servers, a numeric value called a "stratum" is incremented. A server that is obtaining time from a stratum 1 server becomes stratum 2 and so on. At the bottom are external sources of a accurate time, such as GPS, NIST time, atomic clocks and so on. Such external sources are "stratum 0."

A GPS receiver can be a stratum 0 source for NTP, and adding a GPS receiver to a Raspberry Pi is perhaps the single highest bang-for-the-buck you can get in terms of the accuracy you can obtain compared to cost.

GPS is intended primarily as a source of accurate positioning information, but it works by using highly accurate time information. That time information can be used on its own as a side benefit. The accuracy possible depends largely on how good your reception is, but by using GPS receivers with different optimizations in their firmware, you can sacrifice some of the positional utility to obtain phenomenal timing accuracy. Most timing receivers perform what's called a survey, where they will average position fixes over a relatively long period. Once the survey is complete, they will operate in a mode where they presume that their position is fixed. With that presumption in place, they can obtain very good timing results with as few as a single visible satellite - a result that would not be possible had the position not been established beforehand.

As for the accuracy that is possible, it can be as good as on the order of 1 ns or so. NTP running on a Unix computer serving network time over Ethernet isn't going to be capable of maintaining accuracy even 3 orders of magnitude coarser than that, so we can gloss over some of the finer points, but in essence, a GPS receiver has two outputs that indicate the time of day: a serial NMEA sentence stream and a PPS (pulse per second) output. The two work together to communicate accurate time to the host.

The NMEA sentences carry with them an expression of time that is in principle granular down to the millisecond, but in practice it is almost never possible to obtain results that good. For one thing, the NMEA sentences almost never contain sub-second values other than zero, and even if they did, there's no indication within the serial stream what the synchronizing element is. There's no particular edge (such as the initial falling edge of the first start bit) which is deemed the mark against which the rest of the stream describes the time. No, the NMEA sentence will generally tell you what second is currently being described, but you can't generally tell where you are within that second by looking at the NMEA data alone (as a footnote, I'll note that the latency of the NMEA sentence data can sometimes be observed and calibrated and thus you can obtain some level of accuracy from it, but that latency can vary wildly between different receivers and is sometimes not at all stable even on a single receiver).

It is the PPS signal that can deliver the full accuracy available from GPS. Most often, the PPS signal is an active-high signal, whose leading (rising) edge is synchronized to GPS time - marking the start of the GPS second. We are going to concern ourselves here with the SkyTraq Venus838LPx-T timing GPS module. Like most GPS modules, internally it is driven by a microcontroller that generates all of the outputs, including the PPS. The microcontroller is itself driven by a clock, but the clock is not itself generally derived from a particularly stable or accurate source (at least by the standards of accuracy we're talking about here). What winds up happening is that there will be quantization error on the PPS output, since the edge of the PPS signal will be by necessity phase locked with the controller clock, but that clock has no relationship at all with GPS time. SkyTraq chose a clock frequency that's fractional to attempt to insure that there is less likely to be any affinity between the two, and thus the quantization error forms a chaotic randomization inside of a roughly ±6 ns corridor. The good news for those concerned with accuracy is that the module knows the magnitude of this quantization error and will report it in an NMEA extension sentence. Those concerned with maximum accuracy can apply the correction to the observed phase difference of the PPS signal when compared to a reference and obtain a resulting accuracy perhaps as good as ±1 ns or so, at least over short measurement intervals.

The quantization error correction is merely of academic interest for us. Even ±6 ns...

Read more »

  • 1 × Any Raspberry Pi running Raspbian Jessie
  • 1 × A GPS module breakout board with suitable antenna

  • Raspberry Pi Zero W GPS clock mashup

    Nick Sayer07/18/2017 at 22:02 0 comments

    I've made a board that makes this project particularly easy - it's the SkyTraq Venus838LPx-T breakout board product I sell on Tindie, but there's a new variant designed to be placed over the first 12 pins of the Raspberry Pi GPIO header. It connects the serial GPS pins up to the two serial pins of the Pi, and PPS to GPIO 19 (pin 12). The GPS module gets power from the 3.3 volt supply, and the 5 volt supply is used to provide active antenna power with an AP2331 to protect from shorts and overcurrent.

    This board plays nicely with any Raspberry Pi, but it plays particularly nicely with my Pi Zero W desk clock. If you use long pins on the GPIO header, knock out the GPIO cover on the case and stick this board on, you can follow the setup instructions here and on the Pi Zero W clock project and you wind up with a Stratum 1 NTP server that also has an accurate LED clock display. This combination, in fact, is ntp2 . kfu . com, which is part of the global NTP pool.

    EDIT:

    If you have one of my GPS Disciplined Oscillators, then this board will let you hook the diagnostic port of your GPSDO up to your Pi to make an NTP server. In fact, as soon as I get the board back from OSHPark, one of my Pi Zero W clocks with this board being fed from my lab GPSDO will be ntp1 . kfu . com.

  • Third place!

    Nick Sayer11/18/2016 at 05:23 0 comments

    Thank you so much to the judges of the Enlightened Raspberry Pi contest! This project was awarded 3rd place! I am humbled to be even mentioned alongside the other entrants.

    This isn't the end of the story for this project by any means. Once upon a time I had designed a "Pi GPS cap" that added GPS to the correct GPIO pins.

    I wonder if there's room in the world for a Pi Zero NTP GPS server adapter board. Such a board would combine a GPS receiver, a Pi Power buck converter and an SPI Ethernet adapter. Connect it onto a Pi Zero and simply add power, Ethernet and a GPS antenna for a tiny, two-board GPS NTP server.

    What do you all think?

View all 2 project logs

  • 1
    Step 1

    The first step is to get your Raspberry Pi networked. For this, the strong recommendation is Ethernet connectivity rather than WiFi. WiFi by its very nature has wildly unpredictable latency that will have an impact on the obtainable accuracy and stability of your NTP service. For the Raspberry Pi, the only option is Ethernet over USB (even the USB ports built in to the larger Pis are in actuality USB devices). This somewhat degrades the available stability if you're going to use the USB bus for other purposes, but it won't be noticeable if you're building a dedicated NTP server with little other traffic and no other USB peripherals.

  • 2
    Step 2

    On the hardware side, you're going to need (in addition to the Pi) a GPS receiver breakout board of some sort. Your breakout board should have a bidirectional serial interface and active-high PPS, with the leading edge synchronized. It should also be powered by either 5v or 3.3v (5v is preferable). The serial I/O and PPS should be 3.3v TTL level serial. If it's 5 volt serial, then you'll need to use diode+pull-up level shifting.

    Connect the ground of the GPS module to pin 6 of the Pi GPIO header. Connect either a +5 power input to pin 2 or a +3.3 power input to pin 1. Connect the serial input of the GPS module to pin 8, the serial output to pin 10 and the PPS to pin 12.

  • 3
    Step 3

    Run raspi-config and go into the advanced menu and disable the login on serial. The serial port is being given over to the GPS module instead.

View all 7 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates