Skip to content

Posts from the ‘Research’ Category

Building a Stratum 1 NTP Server on a Raspberry Pi

Hi, old friends. I’ve been busy with a variety of projects. Some LabVIEW, some not. I’ve also been doing some interesting (and over-engineered!) home IT projects, such as updating the computers in my Proxmox virtual machine cluster that I use for customer projects. Along the way, I’ve built a faster home network, added a redundant WAN, upgraded computers, and improved system monitoring. Earlier this year, I decided to add a high-quality time server to my network. You know, just because.

My History with Network Time

I joined the LabVIEW team at National Instruments in 1988 as a C developer with Macintosh programming experience. On the side, I was also R&D’s system and network administrator. I mostly looked after a handful of Sun workstations and the small thin-Ethernet and AppleTalk/LocalTalk networks. Not long after we got our first internet connection (a terrifically fast 56 kilobaud line), I set up an NTP (Network Time Protocol) client to keep our main server’s clock synchronized with the outside world. Our local NTP server would then distribute that time to the rest of the NI network. It did what we needed: system clocks stayed close to reality. We weren’t chasing microseconds; we just didn’t want file timestamps and log entries to be noticeably wrong.

Years later, I became NI’s representative to the LXI Consortium—the standards body defining network-based test instrumentation. Among those standards were technologies for how instruments synchronize clocks and share triggers over a network. Timing is a much bigger deal in LXI systems than it had ever been on our Sun workstations. One of the benefits of LXI is that measurement devices can be spread out geographically. But if Ethernet is your only connection between devices, the question of how to synchronize everything becomes more complicated. Even on a local network, software NTP is typically good only to tens or hundreds of microseconds, and to a millisecond or more once traffic crosses routers.

To address this, the LXI Consortium had a close working relationship with the IEEE 1588 working group. This group was working on a new and improved network time solution called PTP, or Precision Time Protocol. That’s where I got to know Dr. John C. Eidson, who chaired the IEEE 1588 committee. I learned a great deal about PTP directly from him and have always been grateful for his mentorship and friendship.

In the LXI world, I worked on projects using PTP to synchronize time across instruments in an RF test system—coordinating triggers and correlating data collected from different places at once. That work also introduced me to vendors whose entire business was timing—companies like Meinberg, who build nothing but time synchronization hardware. Around the same time, another part of NI was working on PXI slot 2 timing controllers, and a PTP-capable one was the natural next step. NI was also involved with White Rabbit, used by CERN to build even more accurate fiber-based large-distance timing systems.

I wrote about this at the time—back in 2006, on an old blog of mine, I was already noting that “IEEE-1588 synchronization works best in small closed networks.” Twenty years later, that’s still true.

Fast forward to today, and one of my current projects, the Santa Fe Opera electronic libretto network, involves keeping clocks on over 2,000 networked devices reasonably close together. Plain NTP handles that fine: nothing there needs microsecond precision, just clocks close enough to compare timestamps in filesystems and logs across devices.

Anyway, for whatever reason, I decided it’d be cool to add a GPS-disciplined time source to my network. I already had a couple of Raspberry Pi Model 4Bs, so adding an inexpensive GPS hat and a GPS antenna was a good excuse to get hands-on with hardware.

I used Claude, an AI assistant, for help. I don’t blindly trust AI. I tend to treat AI assistants as colleagues who know a little bit more about a subject than I do. They can point me in a good direction and help me get unstuck when I can’t tell why something isn’t working. Having said that, AI and I both made mistakes putting this project together.

A Home GPS Server vs. Higher-End Timing Solutions

A “GPS-disciplined NTP server” sounds impressive, but it solves only one of the timing problems in a test system. Here’s how it compares with other approaches.

10 MHz reference clock distribution is one of the most common techniques in test and measurement. You run a cable, usually with SMA connectors, from a 10 MHz source to every instrument with a reference clock input. Each instrument then locks its own sample clock to that reference instead of free-running on its own crystal. Inside each instrument, a phase-locked loop (PLL) steers the local oscillator until its edges line up with the 10 MHz input. A small fixed offset can remain, from cable length and the PLL itself, but it doesn’t drift. None of the instruments need to know absolute time. They only need to agree very precisely on the length of a second, so two separate measurements can be compared as if one instrument took both. That’s what my first LXI-based test system needed: an Agilent 26.5 GHz RF downconverter feeding an Agilent RF digitizer, sharing one timebase.

NI-TClk is NI’s proprietary system for synchronizing multiple PXI chassis or instruments. It builds on the reference clock and accounts for cable lengths, getting down to sub-nanosecond alignment between devices in a bounded, calibrated system.

A GPS-disciplined stratum 1 NTP server—what I built—solves a different problem: absolute time synced to UTC. Plain NTP over the internet is usually good to a few milliseconds. PPS discipline brings this build to around a microsecond. Time servers are ranked by stratum, their distance from a reference clock such as an atomic clock or GPS receiver. A stratum 1 server is connected directly to one, a stratum 2 server gets its time from a stratum 1 server, and so on. Lower is better.

IEEE 1588 (PTP), the standard I spent time around at LXI, goes further: typically sub-microsecond absolute time. NTP usually runs in software, so a desktop OS stamps each packet some unknown time after it arrives, with jitter from interrupts and scheduling. (NTP can use hardware timestamps, but it isn’t the default.) PTP is built around hardware timestamping at the network interface, often in the PHY, so OS latency drops out. Devices with that hardware tend to care about time and are more likely to run an embedded OS. PTP can work in a small network using a direct cable or a plain switch, but as the network grows past that, you need PTP-aware boundary or transparent clocks to keep the precision.

If you know absolute time well enough, relative time comes for free, and microsecond-level sync is plenty for many applications. It still won’t beat a shared 10 MHz reference feeding instruments with good crystals. A good crystal is steadier over short intervals, while GPS-traced time doesn’t drift over the long term the way a free-running crystal does.

White Rabbit, which grew out of CERN, goes further still. It adds Synchronous Ethernet for clock recovery and direct phase measurement on top of PTP, and reaches sub-nanosecond accuracy, with precision below 10 picoseconds, over kilometers of fiber. At that level, even temperature matters. At the LHAASO cosmic-ray observatory, 4,300 meters up, researchers had to characterize and correct for temperature effects on the fiber, the transceivers, and even circuit-board delays.

NI also sells hardware that bridges the relative and absolute worlds: the PXI-6683(H) timing modules accept GPS, PTP, IRIG-B, or PPS as an external reference. They can discipline each chassis to GPS directly, or discipline one master chassis and distribute that reference to the rest over PTP or PPS. Accuracy lands around 100 nanoseconds, between a GPS-disciplined NTP server and TClk’s sub-nanosecond relative precision.

So: a 6683H plus a GPS antenna is several thousand dollars of vendor-supported, certified hardware. The GPS parts I added to my existing Pi cost under $100 before tax and shipping, plus an afternoon with a soldering iron. Over eight days of logs, chrony typically held my clock within about 110 nanoseconds of the PPS signal, and under a microsecond 97 percent of the time. That measures how tightly the clock follows the GPS, not its accuracy against UTC, which would take an independent reference. The gap to the 6683H and TClk is real, but much narrower than the price difference. Much of what the extra money buys is support, determinism, and integration. I also didn’t build a PTP grandmaster, the next step up from NTP. Nothing on my network speaks PTP, and I don’t expect to add anything that does, so a PTP-capable Pi HAT would have had no one to talk to.

Building it

The hardware is fairly simple: a Raspberry Pi 4B, an Adafruit Ultimate GPS HAT, which stacks directly onto the Pi’s 40-pin header, and a GPS antenna. This HAT provides both NMEA data (over serial pins) and a hardware pulse-per-second (PPS) signal—the part that matters for precision. NMEA sentences tell you what time it is; PPS tells you exactly when each second begins, at a level of precision NMEA alone can’t touch.

Raspberry Pi 4B in a clear acrylic case with an Adafruit Ultimate GPS HAT, an antenna cable attached, and the HAT's red fix LED lit.
The finished server: a Raspberry Pi 4B with an Adafruit Ultimate GPS HAT and its antenna cable. The red LED on the HAT is the fix indicator.

Parts list: This is what I bought to add GPS to a Raspberry Pi 4B I already had.

PartProductPriceNotes
GPS/PPS hardwareAdafruit Ultimate GPS HAT$29.95Requires light soldering—see below
Antenna adapterSMA to u.FL/IPEX RF adapter cable$3.95Connects the antenna’s SMA connector to the HAT’s u.FL connector
Backup batteryCR1220 3V lithium coin cell$0.95Backup battery for the GPS module’s clock and satellite data
StandoffsBrass M2.5 standoffs for Pi HATs, pack of 2$0.75
CaseC4Labs Zebra Adafruit GPS HAT Case for Raspberry Pi 4B, 3B+, Pi 3, B+, or Pi 2, from C4Labs’ Etsy shop$29.99This specific listing isn’t there anymore—check the shop for a current equivalent
Antenna (in use)Geekstory Active GPS+GLONASS Navigation Antenna, SMA male, 33 dB gain, 3 m cable$12.98Matches the HAT’s GPS+GLONASS reception
Prices are before sales tax and shipping.

A few lessons I learned:

Check that the header is soldered. The Adafruit HAT ships as a kit with a 40-pin header. I assumed–because nothing told me otherwise–that the header was friction fit. Enough signals worked that the HAT powered up and did some things. But I was having trouble getting everything to work. While struggling with this, one AI assistant suggested I jumper some of the pads together. That seemed like a one-way path that could be a mistake, so I didn’t trust it. After some sleuthing, Claude ran across the words “light soldering required” somewhere on the spec web pages. It stepped me through some hardware debugging and ultimately, soldering the header was the fix.

Antennas. I first taped the GPS antenna to the inside of my office window: no drilling, no cable to route outside. I didn’t really expect it to work, and I was right. Taping the same antenna to the outside of the window instead, with the cable running back in through a gap in the sill, fixed it. The antenna didn’t change, just which side of the glass it sat on.

A recent health check showed 17 satellites visible and 13 in use (9 GPS, 4 GLONASS). That’s more than it needs: once the receiver knows where it is, one satellite is enough to keep time.

How I ended up with the right antenna is a stroke of luck: an AI originally told me to buy the wrong one, but a shipping delay led me to buy a better antenna. Different countries have different GPS technologies, and your antenna and GPS receiver have to agree on one or more technology for them to work. The Adafruit HAT understands GPS (USA system) and GLONASS (Russian system). When I was first shopping, an AI recommended an antenna from Walmart of all places. I didn’t know any better, so I just trusted the choice. It turns out that that antenna understood GPS and BeiDou (Chinese system). But… that order got stuck in shipping and I was uncertain when it would arrive. Not wanting to wait, I ordered a second antenna, looking only for one I could get quickly. It turned out to be a GPS+GLONASS antenna, which matches the hardware. The BeiDou antenna arrived a few days later and now sits in a drawer.

Having said all that, the antenna probably doesn’t matter. As I said earlier, one satellite is sufficient for time-keeping. Having the additional GLONASS satellites to choose from helps in a minor way because I’m not using ideal antenna placement. (Turns out that “taped to the outside of my window” doesn’t give it clear sky coverage.)

Expect to fight with gpsd. Most tutorials have gpsd hand the PPS signal to chrony, through shared memory or a socket. I tried both over one long afternoon, gave up, and had chrony read PPS straight from the kernel instead. gpsd turned out to be useful after all, but only for one thing: the NMEA time.

PPS for precision, NMEA for coarse time. NMEA is only good to a couple hundred milliseconds, but that’s enough to tell chrony which second each PPS edge belongs to. So chrony reads the PPS device directly, locks it to gpsd’s NMEA time, and marks it as trusted so the internet NTP servers can’t outvote it. The internet NTP servers are configured as a fallback if the GPS drops out, and are also useful as a cross-check.

How well it works. Here’s what the server reports for the GPS and for internet time sources, simplified to how far off each one is and how uncertain that estimate is:

Time sourceTypical offsetUncertainty
GPS PPS (this build)112 ns± 0.5 μs
Internet NTP servers (13 sources, Nov 2026)~4 ms± 18-151 ms
Internet NTP, NTS-authenticated (3 servers, Nov 2026)-7 to +21 ms± 18-94 ms

The GPS is off by about 110 nanoseconds. The internet sources are off by milliseconds, with uncertainty from tens to over a hundred milliseconds: good enough to say which second it is, nowhere near good enough to discipline a clock. One item of note: numbers taken soon after a restart can look much worse than steady state. Right after I power-cycled the Pi, chronyc tracking showed an RMS offset of 272 microseconds. The next day it was 272 nanoseconds.

Check for anything already squatting on port 123. Once the server was basically working, clients still showed stratum 3 instead of 1. The cause wasn’t chrony. The Pi had been a DNS server in an earlier project, and I’d forgotten Pi-hole was still installed. Recent versions of Pi-hole include an NTP server on UDP port 123, and chrony hadn’t claimed the port, because it doesn’t serve clients until you tell it which networks to allow. So Pi-hole answered every request at its own stratum. I removed Pi-hole, told chrony which subnets to serve, and clients immediately showed stratum 1. If your numbers look worse than expected, check what’s listening on port 123 (sudo ss -lunp | grep :123).

What’s still open

A few questions are still open, if I want to go further.

How close is it to true time? Chrony says my clock follows the GPS pulse to within about 110 nanoseconds, but that measures how tightly the clock is steered, not how close it is to UTC. Answering that would take an independent reference I don’t have.

Would the BeiDou antenna have worked as well? With it, the receiver would see only GPS satellites. Nine GPS satellites is still more than enough to keep time, so I suspect the difference is small, but I haven’t tested it.

Why does chronyd sometimes crash on startup? It intermittently crash-looped with an error tied to time_pps_fetch() EINTR, a signal interrupting a system call that wasn’t expecting it. A short systemd startup delay reliably avoids it, but I never found the cause. If you’ve seen the same error and know why, I’d like to hear it.

Could I apply this elsewhere? The Santa Fe Opera network I mentioned earlier is air-gapped, with no internet access, so today its time comes from me typing in the date and time every so often. A GPS receiver like this one would give it accurate, automatic time without giving up the air gap. Those systems don’t need microseconds, but an accurate time source would be an interesting addition.

What’s next

Thirty-eight years after setting up NTP on a handful of Sun workstations, I’ve built the piece I’d never had my hands on: the GPS-disciplined source itself. My next step was to configure the other devices on my network to use it. That turned out to be harder than I thought. I’ll cover this in a future post.