Picaxe Simplest Clock

simplest clock picture
(click image for better view)


After creating the simplest nightlight possible (but with very neat functionality) using just a single PICAXE 08M2 chip and an LED, I became intrigued by what else I could do with a minimum of components.  A clock using a single-bit input and single-bit output seemed like the natural progression.  Sure, an accurate clock using a DS1307 as a timebase, and a few pushbuttons for input would be a good build an afternoon, but the limitations of just a bare PICAXE were too tempting to resist.  And here is where I started to sink way too much time into this project.  There is a reason that real-time clock chips exist, and there is also a reason that electronic interfaces almost always have more than a single bit for input and output.  But read-on if you must...

Here it is: a fully functional timepiece, complete with Westminster quarter hour chimes (during the daylight hours only), active calibration, and a reasonably functional time-setting mechanism (considering there is only a single pushbutton and piezo present for I/O), using only a PICAXE, button, and speaker.

In this design, I use the TIME function on the 08M2 to count seconds.  Unfortunately, a 16 bit word can't hold the 86400 seconds in a day, so we have to have an AM/PM flag.   And this chip was never intended to be an accurate time base.  It's temporal accuracy will vary by manufacturing, voltage, temperature, and probably a few other parameters.  In my rudimentary tests, my PICAXE 08M2 driven by 2.6 volts (2 somewhat dead AA batteries) at 70 degrees F loses about 20 minutes/24 hours.  Not very good, as this ends ups losing well over a few hours each week.  A timeskew of approximately +35 seconds/hour corrects the time drift quite nicely, but we can't simply add/subtract some seconds, because the internal resonator of the PICAXE is likely to drift over time, and each implementation will need a custom calibration. That's why his program provides a straightforward method of active calibration to greatly improve accuracy.  

After the clock is set(we'll get to that in a minute), pressing the button at the exact change of hour (as determined by an accurate external clock) calibrates this clock's measured time change with the actual time change that occured.  From this, the timeskew is calculated and appied.

Here's the concept behind setting/calibrating the clock with a single button (and a minimum of pain):
  • If you press the button within 10 seconds of powering up, or within 10 seconds of correcting the time, the clock assumes that you want to increment the hour and does so
  • If it's been more than 10 seconds since you powered up (or last pressed the button), then the clock assumes you are indicating the top of the hour, and that minutes are now 0.  It uses this whole hour button press to reset the time to the nearest whole hour.  (whether it is up to 29 minutes ahead, or 29 minutes behind the current time).
  • If it's been running for more than 1 hour since you corrected the time, and you haven't crossed a 12 hr boundary (noon/midnight) since last calibration, then it will also use this event to calculate and apply a new time skew correction.
  • At any poiint thereafter, you can press the button at the exact hour, and the chip will set the time to the nearest hour.  If you indicate 2 exact hour settings within a single 12 hour period, it will uses these events to recalculate and and adjust the skew.  
  • Time skew is corrected at the last second of the last minute of each hour.
  • The clock outputs the current time across the serial interface every 15 seconds, but you don't need to use this

The UI took some thought to design.  What is the best way to perform input and output using a single bit?  I'm sure that there are much better ways to build a user-interface with a single-bit input and output, so please email me if you have something truly ingenious in mind.  That's the thing about software, it can always be improved with more time and some imagination.

To set the clock:

  • If you are within 25 minutes AFTER the current hour, then do the following:
    • Power-up the clock 
    • Within 10 seconds, press and hold the button, the clock will advance and beep the hour (in 24hr time)
    • Hold the button in until the hour is correct (don't worry about the minutes yet)
    • While looking at an ACCURATE clock (http://www.time.gov/ works well), wait until the next hour is about to come around, and press the button at the exact time that the hour changes.   
  • If you are within 25 minutes BEFORE the next hour, then do the following:
    • Power-up the clock 
    • Within 10 seconds, press and hold the button, the clock will advance and beep the hour (in 24hr time)
    • Hold the button in until the hour is correct (don't worry about the minutes yet)
    • Advance the hour 1 more time so that the clock indicates that it is set to the NEXT hour.
    • While looking at an ACCURATE clock (http://www.time.gov/ works well), wait until the next hour is about to come around, and press the button at the exact time that the hour changes.  
  • Of course, you can also edit the code in the "init" section for the correct time and simply reprogram the PICAXE

Congratulations, your 1-bit clock is now set to the current time.


To calibrate the clock (must be performed within a singe AM or PM period (eg, between midnight-noon, or noon-midnight):

  • While looking at an ACCURATE clock (http://www.time.gov/ works well), wait until the next hour is about to come around, and press the button at the exact time that the hour changes.  
  • Wait 1 or more hours.  And again, while looking at an ACCURATE clock (http://www.time.gov/ works well), wait until the next hour is about to come around, and press the button at the exact time that the hour changes.  


The following schematic/layout drawing was generated from this file using PEBBLE (Picaxe Electronic Bread Board Layout Emulator - V3.1).

simplest clock schematic(click to enlarge)

Parts count:

  • 1 Picaxe-08M microcontroller
  • 1 normally open momentary contact button
  • 1 piezo speaker
  • 2 resistors (10K, and 22K; for in-circuit programming)
  • 1 3-pin programming header
  • misc wire, DIP socket, a battery holder, veroboard

My schematic is above, and here is the code (500 Lines, approximately 850 bytes, and plenty of comments).  My style/documentation is pretty rigorous, but I strongly advocate good coding practice regardless of the machine; all software only becomes more obfuscated over time.


And if you're still reading, and perhaps even thinking of building this clock, I should note that while it is a fun project, my patience to improve the accuracy starting wearing thin after a while.  Perhaps it was the inability to set/correct the time except exactly on the hour, that became tedious.  Or perhaps it was just the realization that clocks are ubiquitous in modern life, and that a clock that is off by more than a few minutes quickly loses its value. 

Regardless, I now have a great appreciation both for mechanical timepieces which can keep time to within a few seconds per day, and for our modern-day lifestyle, in which we have an expectation that anyone, anywhere, can make arrangements to meet someone else with an accuracy right down to minute, if not down to the second.


This code is explicitly released under the GPL. And this page is licensed under a Creative Commons Attribution 2.5 License.  

Write me if you find this project interesting. Link to this page if you find it relevant..

Warning, may cause loss of time. This project is provided without any warranty and probably isn't suitable for anything.

Read or write a comment on the hardware projects

Back to Picaxe Projects