Sunday 18 August 2013

Simple Arduino Frequency Counter

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

I've been having problems with clock drift on one of the projects I've been working on. It's meant to be 3.25 MHz, but it seems to drift down to about 3MHz on occasion. I have been monitoring it on my trusty oscilloscope, but although it can measure frequency if you move the cursors around, it can't keep track of a changing signal. The cursor needs to be moved whenever it changes to check the new reading.
I had a look around at various meters I have, but none can measure frequency over a few tens of kilohertz. I started to consider buying a dedicated frequency counter, but I noticed that some of the cheap ones on ebay looked to be nothing more than a microcontroller and an LCD display. A quick search turned up a number of projects, one of the simplest was a Frequency Counter kit from Sparkfun. This was based on the ATmega328P running at 16MHz, as used on the Arduino. The LCD was a standard HDD44780 based display (as used in my LCD Clocks). The wiring was as follows:
  • D2 - RS (LCD)
  • D3 - R/W (LCD)
  • D4 - E (LCD)
  • D5 - Frequency input
  • D6 - DB4 (LCD)
  • D7 - DB5 (LCD)
  • D8 - DB6 (LCD)
  • D9 - DB7 (LCD)
  • D10 - Backlight Control (LCD)
Other than the usual backlight contrast pot, no special parts were required, so I hooked up the circuit on an Arduino.
The code provided on the Sparkfun site was simple and to the point, just set one of the internal counters to use an external clock (which is fixed as pin D5, so that is used as the input), wait 1000mS and see what it has counted up to. I modified the display code to format the frequency with commas as thousands separators (as 4000000 Hz and 400000 Hz can look the same at first glance, 4,000,000 Hz vs 400,000 Hz should be clearer.
That was all there was to it, the accuracy is reasonable, certainly good enough for indication of the clock remaining stable or starting to drift again. There is no input stage, other than the built in protection on the ATmesg328P, and building things up with long wires and breadboards isn't fantastic when working with higher frequencies due to all that stray capacitance, but it does seem to work quite well. I will probably build this up into a box as it's turned out quite handy, but I think I would add a FET input stage to reduce the loading on the circuit under test.
My clock issues have also been sorted out, and work on the other project continues. Yes, the label on the EPROM does say ZX81. More on that at a later date.....