RaspberryNtpServer
Stratum-1 time server with Raspberry Pi and GPS
Install / Use
/learn @domschl/RaspberryNtpServerREADME
GPS disciplined Stratum-1 NTP Server with Raspberry PI - a HowTo
Stratum-1 NTP time server with Raspberry Pi, GPS and Chrony:
- how to build a precision time server.
- (optional) how to add a 4x20 LCD to display server status and GPS and PPS information.
Requirements - Hardware
- Raspberry Pi
While you can use any model of Raspberry Pi to implement a Stratum-1 NTP server with GPS, the best choice is Raspberry Pi 4, due to 1Gbit network interface and fast hardware for lowest possible latencies.
-
Using a Raspberry Pi 5 gives two additional advantages, but has changed the configuration of the serial port.
- Support for the inbuilt hardware real time clock (RTC) that comes with the Raspberry Pi 5
- Support for the PTP protocol, which allows to transmit precision time information via ethernet hardware
- When connecting the GPS receiver via serial port, the new default is not to use the RX, TX pins on the 40pin connector, but the new three-pin UART connector. To use the 40-pin connector a special configuration is needed.
Both options are of limited advantage for most settings: the hardware clock is only useful for providing time during the first few seconds after boot (and if both GPS and network are inaccessible), and PTP is a time standard requiring IEEE1588-enabled hardware everywhere and provides no advantages over NTP/chrony in most settings. Both however are documented below (See chrony configuration, PTP-chapter).
-
GPS module
- Adafruit GPS hat: Adafruit ultimate GPS hat
- GPS module with serial output and PPS signal: Adafruit GPS module
- GPS module with USB, SMA connector and PPS: Keystudio GPS module
- Cheap NEO6 modules: Aliexpress NEO6)
- Active GPS antenna
When selecting a GPS antenna, make sure to get an active antenna with 3-5V power input. Passive antennas often look similar, but reception quality is far worse at similar cost.
Antenna adapters
Some boards have uFL antenna connectors, whereas almost all external active antennas use SMA, so you might need an uFL to SMA adapter:
To check
Is external antenna selected?
Some GPS modules come with a passive antenna and and external antenna plug. Check the description of your board to find out what's needed to use the external antenna. The Keystudio GPS module listed above for example, requires the removal of capacitor C2 in order to activate the external antenna.
<img src="https://github.com/domschl/RaspberryNtpServer/blob/main/images/gps-with-pps.jpg" align="right" width="300" height="300" />PPS signal easily accessible?
Make sure your GPS module has an accessible output for the PPS signal. (Red mark on image)
If you are using a GPS Hat that plugs into the Raspberry IO connector, check the documentation to which GPIO pin the PPS signal is connected. Adafruit's Hat uses GPIO 4.
USB vs serial
When not using a Pi Hat, decide between USB- and serial connection.
USB-Connection:
- No extra cables for Vcc, GND, Tx, Rx are needed: both powersupply and communication goes over USB.
- No modification of Raspberry's serial console is needed, easier software installation
- Example: Keystudio GPS module
Serial connection:
- Usually cheaper, if a module is purchased.
- All Pi Hats use serial connections.
- Raspberry serial console needs to be disabled
- Sometimes, bluetooth needs to be disabled.
- Raspberry Pi 5 changed the default serial connection to the 3-pin UART connector by default.
Recommended setup
- Raspberry PI 4 (or 5)
- Either GPS hat (e.g. adafruit ultimate GPS hat + uFL to SMA-Adapter) or USB-GPS-Module with PPS output (e.g. keystudio gps module)
- Active GPS antenna with SMA connector and 3-5V
Most cost-efficient setup
NEO6 GPS modules are available at very low cost (1-2Eur) at chinese dealers (Aliexpress) and are perfectly usable as long as they allow connecting an active external GPS antenna and provide a PPS signal.
<img src="https://github.com/domschl/RaspberryNtpServer/blob/main/images/gps-wiring.png" width="600" height="400" />Typical wiring between GPS module and Raspberry Pi connector.
Note: If your GPS module is connected via USB, you only need to connect the PPS connector on the GPS module to GPIO 4 on the Raspberry. Power (Vin), Gnd and Tx/Rx are handled via USB.
Note: The Raspberry Pi 5 has a new connector for the serial port that is used by default instead of the Pins RX, TX on the diagram:
Raspberry PI 5 serial connection changes
- Either use new the three-pin UART connector, which is used by default for serial communication,
- or reconfigure the serial ports by editing
/boot/firmware/config.txtand adding the lines:
dtparam=uart0
dtparam=uart0_console
Documentation for those options is currently in a disarray for Raspberry Pi 5, so some research might be required. See discussion in #6. Also this video on Raspberry Pi 5 Serial Port usage might be helpful.
Test
Before you continue with software: At this point, the GPS module should be connected to the Raspberry Pi and the active antenna. Power up the Raspberry Pi, and check that the GPS module receives the GPS signal (the antenna must have unhindered access to the open sky, it does not work indoors!).
If reception is ok, a led ("FIX" or "PPS") should start blinking on your GPS module.
Check the documentation for your specific hardware how "FIX" is signaled:
- The Adafruit module blinks once per second if there is no fix and once every 10 seconds when there is a fix.
More information
- Adafruit has an excellent GPS guide for their ultimate GPS module.
Professional / Lab considerations: PTP precision time protocol (OPTIONAL)
If you are using laboratory equipment that uses PTP (precision time protocol) information (IEEE 1588), then you will need to use the Raspberry Pi 5 and make sure that your network switches are IEEE 1588 capable. Note that most consumer switches are not IEEE 1588 capable and cannot be used to set up a PTP network. See below (PTP chapter) for setup details and hardware tests.
PTP is completely optional and not required in a network in order to have excellent results with your Raspberry Pi NTP server!
Software
Note: (Optional info) The original repository of
chronyat https://chrony.tuxfamily.org/ seems to be of low availability. In case of problems, use the mirror at https://github.com/mlichvar/chrony) if you want to reference the originalchronysources at some point.
Raspberry Pi OS preparations
Supported are Raspberry Pi OS Bookworm and Trixie
Update Notes
If you are updating from 'Bookworm' to 'Trixie', and you are using the display, you will need to update the display software and its dependencies.
Serial port console (skip, if connected via USB)
If your GPS board is connected via serial connection (Rx/Tx), you need to "free up" Raspberry's serial port, which by default is used to connect a serial (debug) console. We need to disable that console to prevent it from interferring with the GPS module.
This is not needed, if your module is connected via USB.
How to disable the serial console on Raspberry variies greatly depending on hardware revision and Linux flavour used. A few examples:
For Raspberry Pi 4 with Raspberry Pi OS:
- Start raspi-config:
sudo raspi-config. - Select option 3 - Interface Options.
- Select option P6 - Serial Port.
- At the prompt Would you like a login shell to be accessible over serial? answer 'No'
- At the prompt Would you like the serial port hardware to be enabled? answer 'Yes'
- Exit raspi-config and reboot the Pi for changes to take effect.
See "Disable Linux serial console" at raspberrypi.org.
Older versions of raspi-config hide the same serial options under "Advanced options"
Other linux distributions and manual configuration
Note: Recent Raspberry Pi OS has moved the location of kernel config files from
/boot/to/boot/firmware/. Adapt the following paths according to your OS version (If moved, a note is left at the old/boot/location.
- Edit
/boot/firmware/cmdline.txtand remove references to the serial portttyAMA0. E.g. remove:console=ttyAMA0,115200and (if present)kgdboc=ttyAMA0,115200. - Disable getty on serial port.
sudo systemctl disable getty@ttyAMA0or, on some Linux distris:sudo systemctl disable serial-getty@ttyAMA0 - Enable uart in
/boot/firmware/config.txt, add a lineenable_uart=1 - For RPI 3 or lat
