I have an arduino thing, and the hardware I plan to connect to it (a triac zero-crossing based dimmer) provides a synchronization signal from the mains power phase it connects to.

Main power does of course vary some from the nominal 60Hz, however the exact frequency gets adjusted over the course of the day to ensure there are almost exactly 5184000 cycles per day and pretty much exactly 36288000 cycles per week, and is used by mains-powered wall clocks to keep time.

Since my application will need to keep accurate time-of-day across weeks or months without adjustment, it would be advantageous to be able to use this time source.

I can of course write my own code to increment a counter every time the arduino receives a pulse, but if a library that can handle this already exists, I would prefer to use that, in particular since then I won't have to write code to track days and months, handle DST, or any of that stuff.

What libraries exist (if any) to handle timekeeping based on a fixed-pulse-rate input source?

  • 1
    I don't think these exist, but you could use a general library to covert a seconds counter to time an date. Then use a different library for counting the crossings. Though this is so basic, that you can problem write it with just a few lines of code. Where do you live, because not every country tries to make the average frequency 60/50 Hz.
    – Gerben
    Jan 23, 2017 at 18:36
  • You could use the Arduino Time library. You will have to write your own time provider function based on pulse counting, but then you benefit from all the time-handling routines of the library. Jan 23, 2017 at 19:05
  • @Gerben I'm in the USA, in a region that does regulate the mains power frequency. Jan 23, 2017 at 19:19
  • @EdgarBonet That is probably what I will do, there just doesn't seem like a 'natural' way to integrate the pulse counting as a time provider except by keeping a separate redundant count of the time. Jan 23, 2017 at 19:19

1 Answer 1


Counting the pulses is child's play and doesn't need a library (hint: use an interrupt input and increment a counter). From that count you can use TimeLib.h to convert seconds since 01/01/1970 to an actual time. Or just do simple division to convert seconds (count/50) into minutes, hours, whatever division you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.