Atomic Time for the Raspberry PiFollow article
One of the ways the Raspberry Pi keeps costs low is by omitting a real-time clock (RTC) chip. When connected to the Internet system time can be set from NTP servers, but when you don't have Internet access and need accurate time a “radio clock” provides a cost-effective solution. This post covers initial results using a Raspberry Pi with a 60KHz MSF receiver module.
The 60KHz MSF radio time signal is provided by the UK's National Physical Laboratory (NPL), derived from three atomic clocks and broadcast from Cumbria. It uses a simple on-off keying based protocol whereby one bit of information is transmitted every second, and the 60 bits transmitted over the course of a minute provide the time and date, difference between atomic and astronomical time, an indication of whether summer time is or will imminently be in effect, and parity.
A similar service, DCF77, is broadcast on 77.5kHZ by Germany's national physics laboratory, and the US National Institute of Standards and Technology provides its own time signal on 60kHZ, WWVB. All three have simple receiver requirements and designs are available to construct these from scratch, although convenient ready-made modules that come supplied with an antenna and output serial data can be had for less than £10. In the example given here I made use of the SYM-RFT-60 receiver from Symtrik, but the form factor and pinout for such modules doesn't seem to vary much and some have salvaged those used in cheap radio-controlled wall clocks.
A common way of interfacing these receiver modules is by using a single transistor to buffer the output and to connect this to the DCD line of the host serial port. Unfortunately the Raspberry Pi UART does not provide DCD and so the quickest solution was to use a USB to serial adapter.
The adapter can be seen above (bottom left) cabled to the Raspberry Pi and appears as ttyUSB0. The DCD and GND lines from this port are wired up to the MSF receiver via the transistor on the bottom half of the breadboard. There is a second adapter just above this which is plugged into a laptop and connected to the Raspberry Pi UART in order to provide a serial console, as I prefer using a combination of this and ssh to having a second keyboard, monitor and mouse for the Pi.
When it came to software I was surprised to discover that, whilst it supported two commercial receivers, ntpd did not provide direct support for this type of hardware setup. The Debian repositories contain a package called radioclk that adds support, but I didn't find it too helpful in debugging and found this much easier when using software called radioclkd2.
Since the Symtrik module provides an inverted output it is necessary to specify this and the above debug output was generated by running the command:
$radioclkd2 -d -v ttyUSB0:-dcd
With testing complete it can then be started without “-d -v” and the process will fork and run in the background. Note that it may take some fiddling around with orientation of the antenna in order to receive a signal strong enough to be decoded, and debug mode provides a useful aid. The antenna should obviously be placed on a non-conductive surface and away from large metal objects too.
Now the receiver has a signal and we have software decoding this the next step is to configure ntpd via the file /etc/ntp.conf. Because I had a network connection I commented out the default ntp server pool so that these would not be used, but there is no reason why you cannot have many different time sources configured. I then added added two lines for the radio clock time source:
fudge 127.127.28.0 refid MSF
The radioclkd2 (or radioclk) software provides time and date information to ntpd via a shared memory interface, and the first line contains a special address which tells ntpd to source the time this way. The second line simply adds a reference to that source which can be useful in debugging.
After restarting ntpd we can then run the interactive ntpq command to check the operation of ntpd. In the above screenshot we can see that we have a peer time source called MSF that is polled every 64 seconds and was last polled 40 seconds ago, and that the offset between the time source and our system clock was 13.851 milliseconds at that point.
The method of hardware interfacing described here is clearly a hack, and the use of a USB to serial adapter is not ideal in that it likely adds latency, which for obvious reasons is highly undesirable. But luckily the Raspberry Pi has a GPIO port and a far more elegant solution would be to modify the radioclk/radioclkd2 software to take its input from one of those pins.
More than simply providing a convenient means of setting the Raspberry Pi system clock at boot, this keeps it closely — or at least close enough for most of us — synchronised with extremely accurate atomic clocks operated by NPL. As such the combination of a Raspberry Pi and an MSF receiver also provides the basis of an incredibly cheap radio-controlled ntp time server.
Image top: detail of atomic clock-based equipment at the London Science Museum, that was once responsible for generating the familiar time signal “pips” broadcast by the BBC.