Making a Raspberry Pi Stratum 1 Clock

After reading how to make a GPS NTP server on a RPi, i thought I could do it smaller and cheaper.  These are the steps to duplicate my low cost (<$50) RPi, which makes an excellent accurate time source.

I did this with a spare RPi2, but it should work with anything newer than a B+.

I chose Alpine Linux for the minimal footprint, which allowed me to fit all this on a 4Gb SD card.

Things you’ll need:

  • RPi B+ (1, 2, or 3)
  • SD Card
  • Power Source
  • GPS module with PPS

 

Follow the instructions here on setting up Alpine Linux on a Raspberry Pi through to the point of fixing clock errors (since we don’t have a RTC).

In the config.txt add the following line on it’s own line:

dtoverlay=pps-gpio,gpiopin=18

Note that if you want to adjust the memory split to 16, you will need to get the start_cd.elf and fixup_cd.dat from the raspberry pi firmware.

In the cmdline.txt remove all references to ttyAMA01, it should look like this (assuming the default Alpine Linux settings):

modules=loop,squashfs,sd-mod,usb-storage quiet dwc_otg.lpm_enable=0 console=tty1

Now, boot the pi up and run the following commands:

apk update -U
apk install  chrony gpsd vim 

echo "pps-gpio" >> /etc/modules
cat > /etc/chrony/chrony.conf << EOF
pool us.pool.ntp.org iburst
initstepslew 10 us.pool.ntp.org
driftfile /var/lib/chrony/chrony.drift
refclock SHM 0 delay 0.5 refid NMEA
refclock PPS /dev/pps0 lock GPSD refid KPPS
allow
EOF

cat > /etc/conf.d/gpsd  << EOF
# /etc/conf.d/gpsd
DEVICE="/dev/ttyAMA0 /dev/pps0"
BAUDRATE="4800"
ARGS="-n -b"
/bin/stty -F /dev/ttyAMA0 \${BAUDRATE}
/bin/setserial /dev/ttyAMA0 low_latency
EOF

rc-update add gpsd 
lbu ci -d
halt

That will download and install the required packages, add the required config and then save the changes to the disk, and shut down the RPi safely so you can remove power.  That should configure the software side of things.

On the hardware side of things, now we just need to cable everything up.  I used a receiver with 5 pins (VCC,GND, RX, TX, PPS), which lined up perfectly with pins 4 (5v) , 6 (GND), 8 (TXD),  10 (RXD), and 12 (GPIO 18) on the RPi header.  I  was able to solder a header on the module and plug it straight on.  However, as long as your receiver has pps, it doesn’t matter the pinout.  Match all the pins appropriately: TX -> RX, RX -> TX, PPS -> GPIO18, 5v (or 3.3v, read the spec sheet) -> VCC, GND -> GND.  Assuming you didn’t blow anything up, you should be able to plug power back in.  Make sure the antenna has a clear view of the sky, and within a few minutes it should get a lock on satellites.  There is a handy reference for pin position on the RPi header at pinout.xyz

 

You can run the following command to watch chrony getting progressively better time sync.

watch chronyc sources -v

With a cheap (<$50) project, you can get an incredibly accurate clock, which can be used by all sorts of projects that require accurate time, and even more importantly, need accurate time without an internet connection.

Not bad for a computer that doesn’t have a RTC onboard.

 

Edit: I went back and followed my steps and was able to do this even less expensively.  I was able to drop the price to under $15 by using a RPi Zero ($5), a USB OTG network adapter ($2), and the same gps module ($6).