This is an old revision of the document!
Table of Contents
1. Choosing the Right Raspberry Pi
For a PTP (Precision Time Protocol) server, the Ethernet controller is the most important component.
- Best Choice: Raspberry Pi 4 or Raspberry Pi 5. These models have Ethernet controllers (BCM54213PE) that support Hardware Timestamping, which is essential for PTP to achieve sub-microsecond accuracy.
- Avoid: Pi 3 or Zero. These use USB-to-Ethernet bridges or lack hardware timestamping support, which forces you to use “Software PTP,” significantly degrading accuracy.
2. Hardware Connections
You must use a Logic Level Shifter (or a simple resistor voltage divider) to drop the QLG2’s 5V signals down to 3.3V.
| QLG2 Pin | Connection to Raspberry Pi | Note |
|---|---|---|
| GND | GND (e.g., Pin 6) | Common ground is required. |
| TX | RXD (GPIO 15 / Pin 10) | Must be shifted to 3.3V. |
| 1 PPS | GPIO 18 (Pin 12) | Must be shifted to 3.3V. |
| 5V | 5V (Pin 2 or 4) | To power the QLG2. |
Set the QLG2 to 2.8V PPS voltage level: * UPPER: 5V logic level (DEFAULT) * LOWER: 2.8V logic level SER voltage level (GNSS module serial data output, which is the TXD pin on the 4-pin header at the right of the board): * UPPER: 5V logic level (DEFAULT) * LOWER: 2.8V logic level
3. Software Configuration
Step A: Enable Serial and PPS
Edit your boot configuration file: sudo nano /boot/firmware/config.txt (or /boot/config.txt on older OS versions).
Add these lines:
# Enable the hardware UART enable_uart=1 # Define the PPS pin dtoverlay=pps-gpio,gpiopin=18
Then, disable the serial console (so the OS doesn’t try to use the GPS data as a login terminal):
sudo raspi-config → Interface Options → Serial Port → No (Login Shell) / Yes (Hardware enabled).
Step B: Install Time Tools
sudo apt update sudo apt install pps-tools chrony linuxptp
- PPS Check: Run
sudo ppstest /dev/pps0. You should see “source fetch sampled” messages once per second. - GPS Check: Run
cat /dev/ttyS0(orttyAMA0) to see the NMEA sentences ($GPRMC, etc.).
Step C: Configure Chrony (The Grandmaster Clock)
PTP usually gets its time from the System Clock, which should be disciplined by GPS/PPS via chrony.
Edit /etc/chrony/chrony.conf:
refclock SHM 0 offset 0.1 delay 0.2 refid GPS refclock PPS /dev/pps0 lock GPS refid PPS
Step D: Configure PTP (linuxptp)
PTP on Linux uses ptp4l to manage the hardware clock and phc2sys to sync the System Clock (disciplined by your GPS) to the Ethernet controller’s PTP Hardware Clock (PHC).
<HTML><ol style=“list-style-type: decimal;”></HTML>
<HTML><li></HTML><HTML><p></HTML>Sync System Clock to PHC:<HTML></p></HTML>
<HTML><p></HTML>sudo phc2sys -s CLOCK_REALTIME -c eth0 -w &<HTML></p></HTML><HTML></li></HTML>
<HTML><li></HTML><HTML><p></HTML>Run PTP4L:<HTML></p></HTML>
<HTML><p></HTML>sudo ptp4l -i eth0 -m -S (The -S is for software-assisted hardware timestamping).<HTML></p></HTML><HTML></li></HTML><HTML></ol></HTML>
To turn your Raspberry Pi 4 into a PTP Grandmaster, you need a configuration that tells ptp4l two things: “I am the boss” and “Never listen to anyone else’s clock.”
Since the Pi 4 supports hardware timestamping, we will leverage that for maximum precision.
The ptp4l.conf File
Create or edit the file (usually located at /etc/linuxptp/ptp4l.conf):
Ini, TOML
[global] # --- Hardware Settings --- # Use hardware timestamping (Pi 4 supports this on eth0) time_stamping hardware # Two-step is standard for the Pi's Ethernet controller twoStepFlag 1 # --- Grandmaster Role --- # Priority 1: Lower values win the Best Master Clock Algorithm (BMCA) # Default is 128. Setting it to 127 or lower makes it likely to be Master. priority1 127 priority2 128 domainNumber 0 # We are NOT a slave-only device slaveOnly 0 # We ARE capable of being a Grandmaster gmCapable 1 # --- Network Transport --- network_transport UDPv4 delay_mechanism E2E # How often to send Sync messages (2^0 = 1 second) logAnnounceInterval 1 logSyncInterval 0 # --- Clock Discipline --- # Use the Pi's Proportional-Integral servo clockServo pi # Point to the local PTP Hardware Clock (PHC) # ptp4l will find this automatically based on the interface
How to Run It
To start the service using this configuration on your Ethernet interface:
Bash
sudo ptp4l -f /etc/linuxptp/ptp4l.conf -i eth0 -m
The “Secret Sauce”: phc2sys
Running ptp4l alone only manages the clock inside your Ethernet chip. It doesn’t know about the GPS time yet. You must use phc2sys to “push” the high-accuracy time from your System Clock (which Chrony is fixing with the QLG2) into the Ethernet Hardware Clock (PHC).
Run this in a separate terminal or as a service:
Bash
# -s: source (System Clock / CLOCK_REALTIME) # -c: destination (Ethernet interface / eth0) # -w: wait for ptp4l to be ready sudo phc2sys -s CLOCK_REALTIME -c eth0 -w -m
Key Parameters Explained
priority1: This is the “override” switch. By setting this lower than your other devices, the network will automatically elect the Pi as the Grandmaster.twoStepFlag: The Pi 4 hardware requires “two-step” mode, where the precise timestamp of a sync message is sent in a follow-up packet rather than inside the original packet.delay_mechanism E2E: “End-to-End” is the standard for most local networks. If you are using specialized PTP-aware switches, you might change this toP2P(Peer-to-Peer), butE2Eis the safest default.
Verification
Once it’s running, look for these lines in the ptp4l output:
selected local clock as best master: This means the Pi has won the election.assuming the role of MASTER: This confirms it is broadcasting time to the network.
Would you like me to show you how to set these up as systemd services so they start automatically whenever the Pi boots?
