Frequency counter.

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Frequency counter.

Post by mnf »

Inspired by the topic 'How fast an interrupt can capture external pulses' (viewtopic.php?f=63&t=20962)- and having just built an xr2206 signal generator (a cheap Chinese kit)

So - an Arduino frequency counter. I hope to try this with an ARM chip (at 64MHz) - to see how well it copes - but in the first instance I got an Arduino (in my case a Nano clone) doing the honours. It manages to measure up to ~250kHz and you can see how the interrupts take their toll - the display is redrawn slowly (as multiple interrupts will occur).

The hardware is simple - I used a DS3231 RTC (to generate an accurate 1s interrupt)
An oLED i2c display SSD1306 (although I also tested outputting frequency to UART)
The signal - in this case is generated by an XR2206 (https://www.banggood.com/XR2206-Functio ... rehouse=CN) - which is a very cheap kit, although the xr2206 chips may be fake and lose focus at above 12v.
frequency.jpg
frequency.jpg (58.25 KiB) Viewed 7572 times
I tested the resulting frequency count using a SmartScope (https://www.amazon.co.uk/LabNation-A17- ... B00NS2AB60)

The setup is straightforward - RTC and oLED are connected to power, gnd and i2c, no need for pullups (built in to RTC)
SQW from RTC connected to d2 (INT0) and output from signal generator connected to GND and D3 (INT1) I'd tweaked the output from the signal generator to be ~5v (using the amplitude adjustment) - worryingly it still seemed able to power the Arduino and display! (Still no harm done?)
The Arduino Micro was just on the board and doesn't do anything here. The program simply counts the interrupts received on INT1 per 1s (using the interrupt on INT0 from the RTC)
It displays the frequency if it has changed..

Note that I used the -o3 compiler optimisations to minimize the interrupt handler time. (viewtopic.php?f=76&t=20638&hilit=interrupt+o3&start=25) With a little tweaking the Arduino should be able to go faster still! I haven't tested with the standard optimisations to see how much it impairs performance.
frequency.fcfx
(14.23 KiB) Downloaded 413 times
Note that there are probably many enhancements possible - for example measuring the frequency for a fixed period (say 10s) and the disabling the interrupt whilst it is displayed - which would even out some of the fluctuations in frequency.. Simply disabling the interrupt during the display would stop the 'display crawl' at high frequencies.
Have fun..

Martin

As an aside - a slight bug in v8 - Setup interrupt, 'ok and edit macro' creates the macro but returns to editing the current macro rather than the interrupt routine.
The glcd print number is limited to (signed) 16 bit (rather than 32 bit). I sidestepped it here by converting the number to a string.

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: Frequency counter.

Post by jgu1 »

Hi Martin!

Thank’s Martin. Look forward to test your project. I am on holyday at the moment. :D

Br Jorgen.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Frequency counter.

Post by Benj »

I would be interested to see how a 16-bit PIC compares. i.e. one of the 140MHz EP devices.

I might give it a go on the ECIO40P16 if I get chance.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Frequency counter.

Post by mnf »

Would be interested to hear how you get on...

Unfortunately my Windows computer has gone wrong (more phut than bang - just the windows whirly or starting automatic repair) - so haven't had much chance to work on things. Away for a few days (Stornoway :D ) - will fix (fingers crossed) on return.

Martin ( Linux won't let you down :) )

Post Reply