<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/"/>

<title>Jeff Epler's blog</title>
<modified>2019-06-06T12:19:03Z</modified>
<tagline>Photos, electronics, cnc, and more</tagline>
<author><name>Jeff Epler</name><email>jepler@unpythonic.net</email></author>
<entry>
<title>Yet another Raspberry Pi Stratum 1 NTP server</title>
<issued>2019-06-06T12:19:03Z</issued>
<modified>2019-06-06T12:19:03Z</modified>
<id>https://gamma.unpythonic.net/01559823543</id>
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/01559823543"/>
<content type="text/html" mode="escaped">


There are lots of instructions for setting up a Stratum 1 NTP server
on your Raspberry Pi.  A lot.  After much research, I found a simple
configuration that uses a single ntp reference clock and does not involve
gpsd, but uses both NMEA and PPS for the most accurate timekeeping possible.
NMEA+PPS is a mode of NTP's &amp;quot;&lt;a href=&quot;http://doc.ntp.org/4.2.8p10/drivers/driver20.html&quot;&gt;Generic NMEA GPS Receiver&lt;/a&gt;&amp;quot;.

&lt;p&gt;Here's what worked for me:

&lt;p&gt;Start with a pi3 and raspbian stretch.  Add a GPS with TTL-level PPS and 9600
baud NMEA outputs.

&lt;p&gt;Hook up you GPS: GND and +3.3V or +5V (according to the specifiics of your GPS)
to any matching pin, the GPS's TX output to the Pi's RX input on pin 10
of the header, and the GPS's PPS output to the Pi's pin 12.  (You can hook
up your GPS's RX input to the Pi's TX output on pin 8 if you like, but I don't
think this is necessary)

&lt;p&gt;Using raspi-config, disable the serial console and enable the serial port
hardware.

&lt;p&gt;Manually edit /boot/config.txt to add: &lt;tt&gt;dtoverlay=pps-gpio&lt;/tt&gt; —
If you have to (or prefer to) use a different pin for pps, you can apparently
specify it as &lt;tt&gt;dtoverlay=pps-gpio,gpiopin=##&lt;/tt&gt;, where ## is the
internal numbering of the GPIO pins, not the number on the 40-pin header.

&lt;p&gt;Manually edit /etc/modules and add a line that reads &lt;tt&gt;pps-gpio&lt;/tt&gt; — According to some sources, this step is not necessary.

&lt;p&gt;Install ntpd with apt-get.

&lt;p&gt;Delete all the content in /etc/dhcp/dhclient-exit-hooks.d/ntp, leaving an
empty file.

&lt;p&gt;Remove the file /run/ntp.conf.dhcp if it exists.

&lt;p&gt;Edit /etc/udev/rules.d/99-com.rules.  On each of the two lines that ends
&lt;tt&gt;, SYMLINK+=&quot;serial%c&quot;&lt;/tt&gt;, append &lt;tt&gt;, SYMLINK+=&quot;gps%c&quot;&lt;/tt&gt;

&lt;p&gt;Create /etc/udev/rules.d/99-ppsd.rules with the content &lt;tt&gt;SUBSYSTEM==&quot;pps&quot;, GROUP=&quot;dialout&quot;, MODE=&quot;0660&quot;, SYMLINK+=&quot;gpspps%n&quot;&lt;/tt&gt;

&lt;p&gt;In /etc/ntp.conf, add a stanza to access the GPS:
&lt;pre&gt;
# gps clock via serial /dev/gps0 and /dev/gpspps0
server 127.127.20.0 minpoll 3 maxpoll 3 mode 16 burst iburst prefer
fudge 127.127.20.0 refid GPS time2 +.250 flag1 1
&lt;/pre&gt;
(leave the &amp;quot;pool&amp;quot; line(s) or other ntp server lines; if your pi doesn't
have a battery-backed RTC, you need a way to get the correct time initially,
before a GPS fix may be available!)

&lt;p&gt;&amp;quot;time2&amp;quot; doesn't seem critical with this setup, because the PPS time is
preferred over the serial reception time.  &amp;quot;minpoll&amp;quot;, &amp;quot;maxpoll&amp;quot;, &amp;quot;burst&amp;quot;
and &amp;quot;iburst&amp;quot; may be superstitious and unnecessary.

&lt;p&gt;Reboot now and have a look at `ntpq -c peers`.  You should see something
like this:
&lt;pre&gt;
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
oGPS_NMEA(0)     .GPS.            0 l    1    8  377    0.000   -0.001   0.003
+ntp.u           .GPS.            1 u    2    8  377    1.104    0.013   0.064
&lt;/pre&gt;

&lt;p&gt;&amp;quot;GPS_NMEA&amp;quot; is selected as peer and is using PPS (this is what &amp;quot;o&amp;quot; in the
first column means).  delay, offset, and jitter should all be extremely small
(Here, .001 is 1 microsecond).  &amp;quot;ntp.u&amp;quot; is my other local stratum-1 NTP server.
The &amp;quot;+&amp;quot; indicates it's in good agreement with the local GPS.  If you use pool,
you will see multiple lines here; some may have &amp;quot;+&amp;quot; and some may have &amp;quot;-&amp;quot;.

&lt;p&gt;If something's not working, you will get, &amp;quot; &amp;quot; (blank), &amp;quot;-&amp;quot; or &amp;quot;x&amp;quot; next to
GPS_NMEA.  If it's got &amp;quot;*&amp;quot; then NMEA is working but PPS isn't.  Now you get to do things like debug whether the PPS signal is working properly according to ppstest, whether NMEA messages are actually coming in
at 9600 baud, etc.  Or you can follow one of those other guides.  :wink:

&lt;p&gt;Here's how the local time to GPS offset has looked over the last
10 hours or so — I find it awesome that my computer appears to be
synchronized to GPS to within ±5 microseconds almost all of the time:

&lt;p&gt;&lt;img src=&quot;https://media.unpythonic.net/emergent-files/01559823543/ntp-offset.png&quot;&gt;

&lt;p&gt;</content>
</entry>
<entry>
<title>Precision vs Accuracy: A Clock</title>
<issued>2019-06-03T01:17:09Z</issued>
<modified>2019-06-03T01:17:09Z</modified>
<id>https://gamma.unpythonic.net/01559524629</id>
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/01559524629"/>
<content type="text/html" mode="escaped">
&lt;div style=&quot;float:right;clear:right&quot;&gt;&lt;!-- IMG_20190602_205835.jpg--&gt;&lt;div class=albumouter style=width:306px id=&gt;&lt;div class=albumimage style=&quot;width:306px;margin-left:0.0px;&quot;&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/IMG_20190602_205835-medium.jpg&quot; class=&quot;thickbox&quot; rel=&quot;album&quot; title=&quot;The clock, in all its glory&quot;&gt;&lt;img src=&quot;https://media.unpythonic.net/emergent-files/01559524629/IMG_20190602_205835-small.jpg&quot; width=300 height=198&gt;&lt;/a&gt;&lt;div &gt;&lt;div style=&quot;float: right&quot; &gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/IMG_20190602_205835-medium.jpg&quot;&gt;(M)&lt;/a&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/IMG_20190602_205835.jpg&quot;&gt;(L)&lt;/a&gt;&lt;/div&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/IMG_20190602_205835.jpg&quot;&gt;The clock, in all its glory&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;I was inspired by &lt;a href=&quot;https://hop-picker.tumblr.com/post/51065757240&quot;&gt;this watch face design&lt;/a&gt; (I think that's the original version) and by the arrival
of a &amp;quot;OCXO&amp;quot;, a very reliable time keeping circuit, to finally make an
electronic clock.

&lt;p&gt;Accuracy: Between hardware and software tuning, the OCXO keeps time with
an accuracy of possibly better than 100 microseconds per day (loses or gains
well less than a half second per year)
(Yes, I'm deliberately ignoring a lot about crystal aging here!)

&lt;p&gt;Precision: The time displayed is to the nearest minute, and the touchscreen
setting mechanism is (deliberately?) poor, making it hard to set the time
closer than +- 2 minutes or so.  Oh, and it takes a good fraction of a second
to update the screen anytime it changes.  (The best way to set it seems to be
to wait until a few seconds before 6AM/6PM and plug it in, since it boots with
that time showing)

&lt;p&gt;&lt;div style=&quot;float:right;clear:right&quot;&gt;&lt;!-- timelog.png--&gt;&lt;div class=albumouter style=width:306px id=&gt;&lt;div class=albumimage style=&quot;width:306px;margin-left:0.0px;&quot;&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/timelog-medium.jpg&quot; class=&quot;thickbox&quot; rel=&quot;album&quot; title=&quot;Estimated accuracy of the OCXO vs GPS: 60 microseconds per day?&quot;&gt;&lt;img src=&quot;https://media.unpythonic.net/emergent-files/01559524629/timelog-small.jpg&quot; width=300 height=187&gt;&lt;/a&gt;&lt;div &gt;&lt;div style=&quot;float: right&quot; &gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/timelog-medium.jpg&quot;&gt;(M)&lt;/a&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/timelog.png&quot;&gt;(L)&lt;/a&gt;&lt;/div&gt;&lt;a href=&quot;https://media.unpythonic.net/emergent-files/01559524629/timelog.png&quot;&gt;Estimated accuracy of the OCXO vs GPS: 60 microseconds per day?&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
The clock consists of:
&lt;ul&gt;
&lt;li&gt; A &lt;a href=&quot;https://www.adafruit.com/product/2390&quot;&gt;PyBoard&lt;/a&gt; running micropython
&lt;li&gt; A &lt;a href=&quot;https://www.adafruit.com/product/1591&quot;&gt;touchscreen LCD&lt;/a&gt; and controller
&lt;li&gt; A &lt;a href=&quot;https://www.tindie.com/products/deBug67/10mhz-frequency-reference-ocxo-t/&quot;&gt;10MHz OCXO frequency reference&lt;/a&gt;
&lt;/ul&gt;
.. all in a 3d printed enclosure that's not quite the right size.

&lt;p&gt;&lt;div style=&quot;float:right;clear:right&quot;&gt;&lt;!-- clock.gif--&gt;&lt;div class=albumouter style=width:306px id=&gt;&lt;div class=albumimage style=&quot;width:278px;margin-left:14.0px;&quot;&gt;&lt;img src=&quot;https://media.unpythonic.net/emergent-files/01559524629/clock.gif&quot;&gt;&lt;br&gt;&lt;span&gt;The dial as an animation (1 revolution = 12 hours)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
Along the way, I added an even more accurate time source to a Raspberry
PI (GPS with PPS) so that I could even measure the accuracy of the OCXO, and
discovered I even have a GPS module which was negatively affected by the GPS
rollover that occurred in April of this year (the second 1024-week rollover).
This leads to a surprising sequence of clock arithmetic, and finally gpsd
decides the GPS is returning a date sometime back in 1963.
</content>
</entry>
</feed>
