Long gap since my last report because I've been experimenting with various platforms and software techniques in hope of finding a cheap way of improving the accuracy and precision of my time measurements. My pendulum works a treat, but I got into trouble proving how accurate it is. Or not!
- In the short-term, how much does the period of each swing vary? I can easily measure swings to within about 10µS, but an Arduino can't resolve below 4µS with the technique I was using. And although the same method gets down to about ±2µS on a faster Nucleo microcontroller, it can't resolve below 1µS either. Not good enough – I'm after nanoseconds. A RaspberryPi3B is promising because it's much faster processor and Linux has nanosecond resolution, but not as it turns out nanosecond accuracy. At less than 0.000100s my Raspberry 3B+wobbles more than Arduino and Nucleo, probably because Linux is multitasking, and although I was able to reduce the problem it couldn't be eliminated.
- How well does my pendulum keep time in the long run, over days, weeks, months, years? For that purpose Raspberry is fine. As standard Linux synchronises to UTC over the internet an ordinary installation will always be within 0.1s of actual time. And if the Raspberry is disciplined with a GPS seconds pulse, which is quite easy to do, Raspberry time will be correct within a few tens of microseconds. Shame about the untrustworthy nanoseconds!
So throwing faster computers at the problem didn't work.
John Haine gets credit for pointing me at a better solution. John's used picPet timers for accurate timing. Delightfully cheap and off-the-shelf. The picPet uses a counter/timer as described below, getting good results from a processor distinctly less powerful than a Nano, let alone a Nucleo. Unfortunately, picPet doesn't do the other things I need.
Is the picPet a special timing device? I thought not, and got stuck into programming an Arduino Nano to pull the same trick. Hard at the beginning because I couldn't find any close examples on the web and was led astray by a few that aren't quite right. However, after sweating over the Datasheet, it's straightforward enough. Another 'easy when you know how' jobbie.
The trick is to use the microcontrollers counter/timers to generate timestamps. Counter/Timers are hardware that runs at full clock speed independently (mostly) of the processor and software. So whereas an Arduino CPU and a program can't see below 4µs, its counter/timers clock at about 16MHz, and can count ticks down to 62.5nS, a 40x improvement. Pin 8 is 'special' for this.
Another related feature. Pin 5 can feed the counter/timers with an external clock, such as a TXCO, rather than the system clock. Annoyingly the external clock is speed limited, max 6.4MHz on an Arduino – less resolution.
The code:

Link to source on Dropbox if anyone wants to try it on an Arduino. Tested on a Nano so should be OK on a Uno.
Ought to mention I'm not too happy with the maths. It generates huge integers which are multiplied by a tiny float making rounding errors and overflow likely. Special thanks to anyone suggesting improvements.
A plain Arduino isn't quite fit for purpose because the built-in oscillator is low-quality, either a grotty crystal or a nasty ceramic resonator. I intend to hack a board by fitting a TXCO instead.
Next job is to see if I can code a Nucluo in the same way because it's about 3x faster than an Arduino and the extra resolution would be nice. Another fight with a Datasheet!
Then upgrade my existing clock code to use counter/timer measurement, and back to the statistics. What I really want is a week's worth of high-accuracy measurements, taken from an undisturbed clock.
Slowly slowly catchee monkey1
Dave