Accurate time keeping on an Apple Mac or Raspberry Pi for WSPR/JT65/JT9/FT8

If you use any of the data modes for QSOs such as JT65, JT9, or FT8 or if you operate using WSPR then you’ll know how important it is that your computer clock is accurate.  Anything more than a couple of seconds out and you won’t decode anything and nobody will decode your signal.

On a Windows computer there are a number of software utilities that keep your clock accurate, one of the most commonly used is called Dimension 4 and most people using the data modes run a third party utility to keep the clock accurate to with a second.

My Mac has always been very good at timekeeping and I’ve very rarely felt the need to use anything additional to keep the clock accurate but I’ve been playing around with WSJT-X on a Raspberry Pis recently and a couple of times I’ve noticed that the clock had drifted just enough to cause a problem.

I started looking into whether there are any third party add ons for the Pi to check and set the clock at regular intervals and found that there was nothing really available but there is an ntp client built into the OS which just sometimes needs a nudge to wake it up or force it to check.

How to automatically set your time using an Apple Mac with Mac OS X

I looked at my Mac first – Mac OS is based on BSD and I figured that if I can do it on the Mac then I can do it on a Raspberry Pi. OS X uses ntpdate to set the time and I decided to force that to run once every ten minutes.  This might be overkill but I’d rather my clock is accurate than not be able to decode stations I’m trying to work.
The first thing to do is to test this to make sure it works.
On your Mac, open a terminal window and type the following:

sudo ntpdate -u pool.ntp.org

You’ll almost certainly be prompted for your password – This is the main administrator account password for your Mac computer, enter it and press return.

After a few seconds, you’ll get a reply which will be something like this:

ntpdate: adjust time server 85.199.214.100 offset -0.009396 sec

You can see my clock was only out by 0.009396 seconds – That’s pretty accurate and is because I run this regularly.

What this command is doing is running ntpdate using the internet cluster of ntp servers to automatically find the closest server to you and set the time.

As you can see, the ability to automatically update your clock is built into the OS but it doesn’t run regularly enough for me.  To force it to run more often, you need to schedule it.  Mac OS has a built in task scheduler called cron and it’s relatively straightforward to add an entry to cron.

By default, Mac OS uses a very powerful text editor to edit system files but that can be a little daunting at first.  If you’re happy to use that then that’s fine, go ahead but I’m going to give you some instructions now on how to change your default system editor to nano.

In your terminal window, type the following:
nano .bash_profile

You will get an empty window just like this:

Copy (cmd-c) the following three lines of text and paste (cmd-v) them into the window:

# Set Default Editor (change ‘Nano’ to the editor of your choice)
# ————————————————————
export EDITOR=/usr/bin/nano

The window will look like this:

Press control-x, press y and then press enter to save the file.

Now either restart your Mac or simply log out and then back in.  If you don’t do this then it won’t use the editor we’ve just configured.

When you’ve logged back in, open a terminal window again and type:
sudo crontab -e

Enter your password as before.

Unless you’ve already added something into cron previously, this file will be empty.  You may have followed my instructions on cleaning a useless cache and if so, there will be a line there already.

It doesn’t matter whether the file is empty or not.
Using your cursor keys. scroll to the bottom and add a new line:
*/60 * * * * ntpdate -u pool.ntp.org

This line is adding a task to automatically run the command we discussed above every sixty minutes. You could change the 60 to be any number you want and it will run at those intervals.  For more information about how to configure cron, please see here.

Press control-x, press y and then press enter to save the file.  You’re done.  Your Mac will now keep very accurate time with no further intervention.

How to automatically set your time on a Raspberry Pi

A Raspberry Pi needs an extra couple of commands to be run first because Raspbian doesn’t come with ntpdate already installed.  Either log onto your Pi via SSH or if you’re using the GUI then open a terminal window and type the following two commands:
sudo apt-get update
sudo apt-get install ntpdate -y

These commands will take a few seconds to run and when they’ve finished, you’ll have installed the required software. You can test this as before using this command:
sudo ntpdate -u pool.ntp.org

Assuming this works OK, you can add the line into cron as on the Mac.

sudo crontab -e

If this is the first time you’ve edited cron on your Raspberry Pi, you’ll see the following screen:

Press enter to select nano and from there, the process is exactly the same as on the Mac.

Using your cursor keys. scroll to the bottom and add a new line:
*/60 * * * * ntpdate -u pool.ntp.org

This line is adding a task to automatically run the command we discussed above every sixty minutes. You could change the 60 to be any number you want and it will run at those intervals.  For more information about how to configure cron, please see here.

Press control-x, press y and then press enter to save the file.  You’re done.  Your Raspberry Pi will now keep very accurate time with no further intervention.

I’ve tested these with MacOS Sierra (10.12.6) using a 2017 iMac and the latest version of Raspbian Jessie on various different model Raspberry Pis including a Pi Zero, Pi Zero W, Raspberry Pi2 and Raspberry Pi3.

This entry was posted in Amateur radio, FT8, JT65, JT9, Raspberry Pi, WSPR and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *