Trustworthy Timepieces

Terry Pratchet's Discworld series presents a ruler-figure of sorts named Lord Vetinari (Disclaimer: I've never read any Discworld book). He has a clock in his waiting room which stands as inspiration to anyone with sufficient appreciation for the ridiculous (and I'm most certainly not the first). The regular tick of a clock is annoying enough to many, but this one has an irregular, random tick. That is the extent to which I know anything about the original inspiration for this project. To make a short story shorter, I want just such a clock and I saw it as an interesting challenge and one that might also make a nice beginner project for friends and students trying to get started in the field.

Getting Started

So first, get a clock with a “seconds” hand. Dollarama in Canada has them. Some are decent, some are crap. It's hard to tell which are which, but the investment is small in any case. I think the Lebron James ones are particularly classy machines. Unfortunately our own Dollar Tree doesn't stock clocks, which is a serious human rights violation. I don't have any of my clocks with me as I write this, so I will simply hope to add better pictures later.

However your clock is held together, take it apart and get that little black box with the gears and the battery out. Take it apart and you'll find a coil inside with some fine wire on it. When you apply some voltage to the two leads of the coil, a magnetic field is created proportional to the current. At first, this is driven by some IC usually covered in a blob of glue. To deal with this, we could intuitively just cut the wires and attach them to our own circuit. That sucks though because it's really easy to melt those wires and it's nice to have them supported by the PCB. Just cut the traces on the PCB and solder some small wires (I used 30AWG wrapping wire so I could easily attach the other end) near where the coil wires are attached. Put the clock back together with those wires hanging out and you're practically a millionaire. You can just apply pulses to those wires and the clock will tick.

To drive it, an easy solution was to grab one of those $4.30 MSP430 development boards (Launchpad?). They come with debug circuitry and a 32.768kHz crystal to make sure you keep accurate time. The coil require alternating pulses, meaning that you need two GPIO pins (or possibly just a series capacitor -- I didn't bother playing with that). The firmware I've written is hosted here and works for the 4.0 branch of MSPGCC. It could be ported to other hardware quite easily if you know the hardware well enough.

Random Ticking

The process of generating random ticks in software is really simple. First, random numbers are needed. To get a pseudo-random seed, I used two oscillators: the imprecise 16MHz internal RC oscillator which is very sensitive to voltage variations and the 32kHz crystal which is a lot more stable. Counting a second on the 32kHz crystal, I read the value mod 65536 (16-bit timer) counted by the RC oscillator. For each successive random number from the seed, I use Marsaglia's multiply-with-carry method because it's cheap and easy.

To drive the random ticking, I took the easy way out. Eight times per second, the CPU wakes up and decides, with 1 in 8 probability, whether or not to tick. Errors are tracked, forcing a tick if the clock gets behind by more than 30 seconds and similarly inhibiting ticks if it is ahead by more than 30 seconds. After each tick, a second timer is started in order to clear the output between ticks. The optimal timing for this is dependent on the clock. The other option is the DC blocking capactitor mentioned earlier, but it seems easier to deal with it in software...

For some clocks, I've found that you can make the ticks faster, supporting up to 16 ticks per second. You can adjust it pretty easily in the code I've provided.