NTP server on Raspberry Pi

About to start following the post, http://www.satsignal.eu/ntp/Raspberry-Pi-NTP.html, by David Taylor. I emailed David (contact is on his page) thanking him for his post and he replied with a few pointers.  I’ll incorporate his suggestions into this blog post and any challenges.

Hardware purchased:

Hardware Assumed:

  • Keyboard and Mouse
  • Monitor with HDMI Support, (Makes setup of WiFi easier even if you plan to run headless)
  • microUSB power cord
  • Soldering Iron
  • Resistor (220 Ohm)

Raspberry Pi Setup for New Users:

http://www.raspberrypi.org/wp-content/uploads/2012/04/quick-start-guide-v2_1.pdf – Various videos on YouTube show you how to boot up your pi if you are using a NOOB card. The USB WiFi dongle states that the latest versions of Wheezy Raspbian distribution support this module out-of-the-box. Default username is pi and the password is raspberry. When booting your pi, make sure to enable ssh services. In addition, you may want to expand the root partition to fill the entire SD card.

If you are loading your own version, you can follow the steps below:

  1. Download an OS image for the SD card – 2012-09-18-wheezy-raspbian.zip
  2. Unzip the contents of the Zip archive to a .IMG file
  3. Download the SD card writer program: Win32DiskImager
  4. Use the Disk Imager to write the OS image to the SD card

Setting up for Wireless Communication:

Although my end goal is to use my raspberry pi as a direct access point, it’s easier to become familiar with the pi by connecting to it through my home router. My first step was to verify the Internet worked by directly connecting to my router by Ethernet. I switched into the raspbian GUI by typing startx on the command line. To test the Internet connection, I opened the the NetSurf Web browser. Following adafruit’s network setup tutorial, I used the WiFi Config application to connect to my wireless network. If you don’t see any network connections during a network scan, you may need to remove the WiFi dongle from your raspberry pi and connect it back. I should add that I struggled getting the WiFi connection to work and I switched to trying to a command line approach. I admit I’m not certain what sets were necessary in allowing me to finally connect to my network through the WiFi dongle. When I have both the Ethernet and WiFi dongle plugged in, I lost access to the WiFi. I then followed a forum post regarding the ifplugd that caused your pi to only use Ethernet when it was plugged in. To remove ifplugd enter sudo apt-get remove ifplugd on the command line, unplug your pi from the power source to reboot, and then WiFi will work when Ethernet is plugged in, but I wasn’t able to get it to work with both network interfaces. In retrospect, removing ifplugd was a dumb idea, apparently you can modify it’s configuration file to enable both. Go to “/etc/default/ifplugd” and change the file to

ARGS="-q -f -u0 -d10 -w -I"

SSH and Headless Mode:

Since I don’t own a monitor with HDMI, I’ve been using a pico projector with an eye-straining 480×640 screen. SSH allows me to access my pi through my laptop’s command line. If you didn’t enable ssh when you first started your OS, you can enable ssh by typing sudo raspi-config. To get your pi’s IP address, type either ifconfig or access your router’s IP lookup table. Last, from the command line of another computer (my favorite is mobaxterm), type ssh pi@IPADDRESS with the password raspberry.

Raspberry Pi P1 Header and GPIO pins: 

Raspberry Pi P1 Header
Raspberry Pi P1 Header Described by Mosaic Industries

This is the 26 pin I/O P1 Header on the pi. Pin number 1 is closest to the SD card. You can locate the pin 1 copper square by looking at the mirror image on the underside of the raspberry pi.

My next step was to see if I could read an input and an output. Using my jumper cables, I connected Pin 11 to 12 with a 220 Ohm resistor between. (It may be safe, but I just wanted to be careful.)

I then wrote the following code using the command line, creating test.py using the following command:  nano test.py

import RPi.GPIO as GPIO
import time

def toggle(pin1, pin2, input):
val = GPIO.input(pin2)
print “%r” % (val)


outPin = 11
inPin = 12
toggle(outPin,inPin, True)
toggle(outPin,inPin, False)


I then saved the file by typing CTRL-O, then CTRL-X to quit.

From the command line, execute the program by typing sudo python test.py

Adafruit Ultimate GPS Breakout - 66 channel w/10 Hz updates

GPS Board:

The Raspberry Pi outputs 3.3V on pin 1 and 5 V on pin 2 of the P1 Header, while the VIN of the GPS breakout board accepts between 3.3-5 V. In his tutorial, David Taylor connected his GPS board to pin 2, because he wanted to reduce the load on the raspberry pi 3.3 V power regulator.

We’ll be using pins 2, 10, 18, 20 on the raspberry pi to connect to the GPS breakout board.( I’m going to reference pin locations, because I’ve found different naming conventions for GPIOs.) In addition, to make the setup easier, I soldered the break away headers that came with the GPS module into the vias.

Red 2 +5V 8 VIN
Black 20 Gnd 7 GND
Blue 10 UART RxD 5 TXD
Yellow 24 GPIO 8 9 PPS

I ran into issues trying to install the GPS Daemon, so I updated apt-get with sudo apt-get update && sudo apt-get upgrade

To setup the GPS I followed a tutorial from adafruit. I’ve copied their steps here, because web pages on the Internet are often ephemeral.

To install gpsd, run: sudo apt-get install gpsd gpsd-clients python-gps

Then enter sudo nano /boot/cmdline.txt


dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait


dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Then: sudo nano /etc/inittab


#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100


#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Reboot your Pi: sudo reboot

Point the gps daemon to HW UART: sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

Test the output with: cgps -s

You may have to wait several minutes to lock onto a GPS signal.


David Taylor’s blog gives more of the insight into NTP setup, but I’ve summarized the key points here:

$ mkdir ntp
$ cd ntp
$ sudo apt-get install libcap-dev # once-off, required to prevent later file not found error
$ wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-dev/ntp-dev-4.2.7p368.tar.gz # development
$ tar xvfz ntp-dev-4.2.7p368.tar.gz
$ cd ntp-dev-4.2.7p368
$ ./configure –enable-linuxcaps # takes 11-15 minutes
$ make
$ sudo make install
$ sudo /etc/init.d/ntp stop
$ sudo cp /usr/local/bin/ntp* /usr/sbin/
$ sudo /etc/init.d/ntp start
$ ntpq -crv -pn # optional step to check for version and basic function
# To configure gpsd to auto-start, enter: $ sudo dpkg-reconfigure gpsd

Three windows will appear, answer with the following:
(1) Device the GPS is attached to: /dev/ttyAMA0
(2) Options to gpsd: -n
(3) Gpsd control socket path: /var/run/gpsd.sock


David Taylor provides methods for both kernel-mode PPS and user-model PPS. In an email, he told me kernel-mode is only needed for synchronization below 3 microsecond average jitter. I’m providing his steps for user-model PPS.

$ wget http://vanheusden.com/time/rpi_gpio_ntp/rpi_gpio_ntp-0.3.tgz
$ tar xvfz rpi_gpio_ntp-0.3.tgz
$ cd rpi_gpio_ntp-0.3
$ sudo make install

#To test that you are receiving a PPS on pin 24, exit with CTRL-C
$ sudo rpi_gpio_ntp -g 8 -d

rpi_gpio_ntp v0.3, (C) 2013 by folkert@vanheusden.com

NTP unit: 0
GPIO pin: 8
Fudge : 0.000000000
“Fork into the background” disabled because of debug mode.
1384662324.539383140] poll() GPIO 8 interrupt occurred
1384662325.016712554] poll() GPIO 8 interrupt occurred

# To set NTP to read the PPS from pin 24 (GPIO 8)
$ sudo rpi_gpio_ntp -N 1 -g 8
# Modify the ntp.conf file to the shared memory for GPSD
sudo nano /etc/ntp.conf

Replace these lines below:
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst


server minpoll 4 prefer
fudge refid UPPS
pool us.pool.ntp.org iburst # For US ntp pool

CTRL-o to save then CTRL-x

Reboot NTP:
$ sudo /etc/init.d/ntp stop
$ sudo /etc/init.d/ntp start

Check that UPPS now shows values in microseconds:
$ntpq -np
* .UPPS. 0 l 9 16 77 0.000 4.412 8.684

$ntpq -c rv
$sudo nano /etc/rc.local

The line before “exit 0” enter: /usr/local/bin/rpi_gpio_ntp -N 1 -g 8

For the curious, the section under peers in documentation about ntpq explains what the fields are when sending the command ntpq -np

WiFi Direct Access Point:

As I explained earlier, my current setup requires the pi to communicate through a home router. However, for testing it would be beneficial to have the smallest network delay I can manage.

– Richard Latimer (LinkedIn)