Username:    Password: Remember me
Follow us:
Build your 40 MHz Frequency meter!
By Ibrahim Kamal
Last update: 9/5/08


This article shows how to build a small, cheap and simple frequency meter, without any fancy, out of reach components. The simple proposed design can measure frequencies up to 40 Mhz with errors below 1%! This degree of precision will be more than enough to debug most of your analog and digital circuits, and will give you the ability to analyze many aspects that you were unable to see before.

The frequency meter is build on a veroboard, using only 3 components and 8 resistors. It is designed to be plugged into any standard bread-board.

1.The hardware

figure 1.a

As
you can see in figure 1.a, the design is built on a ATMEGA16 micro controller. There are no crystal oscillators, as we are using the internal RC oscillator, calibrated to 8 MHz.

The frequency measurement probe is connected to W1 terminal, which is then fed to the pin PB0 and the clock input of the 74191 4-bit counter. The 4-bit counter will be used to divide the measured frequency by 16 before feeding it to the microcontroller. As you can see, all the features of the counter aren't used, only the Q3 output is used, whose frequency will always be equal to the input frequency divided by 16.

The 7 segments display used is composed of 4 cells all integrated into one package. This reduces the number of wires to enable the different cells.

The display we used is common anode type, and had its leads arranged according to the diagram in figure 1.b, and most of the 7 segment cells of this type will follow those standard connections. However, if you use different type of 7 segment display, the software can be very easily adapted to accommodate the changes. The pins E1 to E4 lets you enable one of the 4 cells, E1 enables the right most one.

Since we are using an ATMEGA16 microcontroller that can source up to 40mA of current per I/O pin, we don't need transistors, so the 4 enable signals of the 7 segment display is directly connected to the microcontroller.

The wire connection W2 is to connect to ground, to allow measurement of frequencies on devices that do not share the same power supply as the frequency meter itself. (Remark, for one circuit to be able to measure an signal on another circuit, they must share the same ground voltage, se we connect them together.)

figure 1.b

Finally, J1 is a connection for the ISP programmer (In System Programmer). In need, after you finish the project you will spend 10 minutes doing nothing by calibrating your frequency meter, and adjusting some variables to make the display clearer or to reduce the flickering of the numbers being displayed. That's why we added this ISP connector, because we will need to update the code of the microcontroller often.

The whole is assembled on a veroboard, and, as you can see in figure 1.c, it is designed to be plugged directly into the power supply rails of a standard bread board. You can also notice that there is a protection diode added (though it is not present in the schematic), it prevents any damage to occur in case the frequency meter is plugged in the wrong direction.

figure 1.c

Next figure (1.d) shows the two probes that are connected to W1 and W2 (see the schematic in figure 1.a). The ground probe is only used when the power supply of the frequency meter is different from the power supply of the device being tested, to connect both grounds together. the frequency measurement probe is made of a single pin header, covered with a piece of "Heat shrink tube" (or also called thermo-retractable tube).

figure 1.d


2. Frequency measurement algorithm
We all know - at least most of the visitors that made it to that website! - that "Frequency is a measure of the number of occurrences of a repeating event per unit time". but measuring frequencies with digital tools such as this microcontroller that have its limitations involve some further studies to achieve the required results.

The maximum frequency that can be sampled by one of the counters of the ATMEGA16 cannot exceed the CPU clock divided by 2.5. Let's call this frequency F_max. So, assuming the ATMEGA16's CPU is clocked at 8 MHz, we can directly measure frequencies up to 3.2 Mhz. Frequencies above that limit will be measured as 3.2 MHz or less, since not all the pulses will be sampled. To be able to measure frequencies above F_max, we use a 4 bit counter as a frequency divider, dividing the measured frequency by 16. This way we can also measure frequencies up to 16 times F_max, but due to the limitation of the 74191 counter, the actual maximum measurable frequency wont exceed 40 MHz.

The algorithm that we developed measures both the original frequency (let's call it F_o) and divided frequency (F_d). in normal conditions, when the frequency is below F_max, the following relation is true:

F_o = 16*F_d

But as F_o approaches to F_max, more and more pulses wont be sampled, and the the relation above will obviously become:

F_o < 16*F_d

And hence the limit of the microcontroller can be automatically detected.

The frequency meter starts by selecting the original frequency for processing and display, and as soon as it detects that it reaches F_max (using the method described above), the divided frequency is selected instead.

This algorithm can be summarized in the following flow chart (figure 2.a)

figure 1.a


3.The software

The C source code for this frequency meter can be downloaded here.

The source code is detailed with as much comments as I could, but you may need some more explanations to understand the code:

The code is made such that the number being displayed is in KHz. For example, if you see on the 7 segments the number "325.8" that means 325.8 KHz, "3983" would mean 3983 KHz (or 3.983 MHz). If the number is in tenth of megahertz, it is displayed with an "m" at the end like "22.3m" meaning 22.3 MHz.

Timer/Counter 0 is used to count incoming pulses directly

Timer/Counter 1 is used to count incoming pulses divided by 16

Timer/Counter 2 is configured as timer with a 1024 prescaller (counting CPU frequency divided by 1024). It is used to call the "frequency calculation and selection algorithm" every timer period T. T is defined as "1024*256/(F_cpu)".

The constant "factor" defined in the top of the program as "31.78581" have to be calibrated by measuring a known frequency. This factor was initially calculated as the following:

factor = F_cpu / (1024*256) = 8.E6 / (1024*256) = 30.51757

But due to the fact that there is some delay between the time the Timer_2 overflows and the time where the counted frequency is actually processed, this factor need some calibration to reflect more accurately the reality of the frequency being measured.

The Anti-flickering algorithm is complicated but very effective, specially in measuring frequencies that are unstable. It will totally prevent the display from quickly switching between various values, while still showing accurate values, and quickly change if the frequency being measured "really" changes.

One last note, the ATMEGA16 is chipped with a 1 MHz internal oscillator enabled. To set the internal oscillator to 8Mhz, you have to use some programmer (like our ISP programmer) to change the fuse bits of the micro controller to adjust the internal oscillator's frequency. (For 8MHz, the CKSEL3..0 fuses have to be set to '0100').


Download the zip file for the project.
containing the C code and complied HEX file.


I hope this article was useful. Any comments and further questions are welcome in the forum below.

Discussion (Last 15 posts preview...)
Preview of the last 15 messages discussing this page. Messages are sorted from the newest to the oldest.
Posted by:
kapox
on: 02 Oct 2011
40 MHz frequency meter
['Quote ]
Why used atmega16? I think it should work with atmega8 or etc.
Sorry for my bad English
Posted by:
ehsanheidari
on: 30 Aug 2011
40 MHz frequency meter
['Quote ]
what internal ferequency the micro is worked?
thanke you
Posted by:
uttam
on: 18 Jul 2011
Re: 40 MHz frequency meter
['Quote ]

Quoting detmeters: after compiling the code with codevision AVR, it shows some errors. Please correct the code.
Ashish

Ashish,
compile it with AVR GCC, there will be error less compilation, AVR GCC is available free in net, I am not usedup with codevision.
regards,
Uttam
Posted by:
detmeters
on: 17 Jul 2011
40 MHz frequency meter
['Quote ]
after compiling the code with codevision AVR, it shows some errors. Please correct the code.
Ashish
Posted by:
uttam
on: 24 Jun 2011
40 MHz frequency meter
['Quote ]
To Mr. Ika,

I want to clear a doubt,
You mentioned that low fuse bit CKSEl3..0 will be 0100 does it mean the complete word will be 11100010 = E2(HEX)[As per data sheet default word = 11100100 = E4(HEX)]
and what will be the high fuse bit , as per datasheet it is 10011001=99(HEX)
My programmer asked for fuse bit in HEX for both High and Low fuse bit. and I heard that setting of fuse bit is very important , wrong fuse bit may permanently defang. the micro. I am going to build this circuit so please reply.
Regards,
Uttam Dutta.
Posted by:
uttam
on: 26 May 2011
40 MHz frequency meter
['Quote ]
Thank You for reply , Very soon I am going to build it with Atmega32
Regards,
Uttam Dutta
Posted by:
User avatar
ikalogic

on: 26 May 2011
Re: 40 MHz frequency meter
['Quote ]

Quoting uttam: To Mr. Ika,

Will performance vary if I use 74LS191 instead of 74HC191.
regards,
Uttam
Please reply


normaly yes. You just have to check the datasheet , and make sure that it can be clocked at a frequency of 40Mhz.
Posted by:
uttam
on: 24 May 2011
40 MHz frequency meter
['Quote ]
To Mr. Ika,

Will performance vary if I use 74LS191 instead of 74HC191.
regards,
Uttam
Please reply
Posted by:
zeba
on: 29 Jan 2011
Re: 40 MHz frequency meter
['Quote ]
M going to implement it on hardware :wink:
Posted by:
atinjain
on: 08 Jan 2011
40 MHz frequency meter
['Quote ]
how can i build 4 digit display using 1 digit 7 seg display (inter module connection).
Posted by:
faithnic
on: 24 Sep 2010
40 MHz frequency meter
['Quote ]
Hi, thanks for the project. I have the following questions.

1. Did you or anyone tried to show the result on LCD?

2. Where should we compile the C code, AVR GCC?

3. Why 8MHz internal osc? Why not an external Xtal?

Thanks
Posted by:
roybean
on: 11 Sep 2010
Re: 40 MHz frequency meter
['Quote ]
iklogic,
thanks for posting, i appreciate it.

I'm using the 4 digit LCDs that I have to multiplex, that is, update each digit in sequence very fast so it appears to the eye that they are all being updated at once. I don't know any other way to do without using 28 lines for the segments and 4 more lines for the digits.
Anyway, to do this, you have to update about every 2ms, which does not leave a lot of time for the freq counter to work. So i am curious how you are doing this. Maybe counters and an interrupt?

Also, my application is to measure frequencies between 50Hz and about 2kHz. Will your counter work ok at this range? I'd like to be able to update the display about 3 times a second, I don't know if that is possible or not.

Thanks for any comments.
Phil
Posted by:
User avatar
ikalogic

on: 11 Sep 2010
Re: 40 MHz frequency meter
['Quote ]
Hello Phil, Hello Dave,

First sorry for the late response - my time is getting very limited with all the company work... :|

I do not use Arduino - so i can't be of much help at this part. However, i can gladly assist you into porting the code to another atmega.

Best of luck and keep us posted!
Posted by:
dgertis
on: 11 Sep 2010
Re: 40 MHz frequency meter
['Quote ]
I received my Arduino 328 the other day. I looked at the pin differences between the Atmega16 used in the Ikalogic design and the Atmega328. The Atmega16 has 40 pins vs. 28 for the 328. Most of the key pins that we need will map straight across, though they are arranged differently. One problem, however, is in how we display the output -- It looks like the best solution might be to use a two-wire serial output from the arduino. This will simplify the physical hardware needs, but will likely require some code changes for output compared to the Ikalogic version. Looks like the input and probe part of the project can be used as designed without any changes, although since they will map to different pins the code will have to reflect the new pin numbers.

All in all, Ika has already done the most difficult parts, and we should be able to port his work with some reasonable effort.

I am travelling for the next couple of weeks, so won't be able to work on the project, but I will check emails and postings if anyone has more info to share.

Dave
Posted by:
roybean
on: 10 Sep 2010
40 MHz frequency meter
['Quote ]
I am very interested in this project and look forward to you guys getting the code ported to an Arduino (i'm using the 328).

Thanks in advance for your efforts!

Phil
You have to be a member to post replies.
Username: Remember me
Register now! it only takes an instant.
Forgot your password?


Unless mentioned, all content is written and designed by Ibrahim Kamal, copying is prohibited and unethical
ikalogic.com: Electronics and Robotics related projects.
All content on this site is provided as is and without any guarantee of any kind. We cannot be held responsible for any errors, omissions, or damages arising out of use of information available on this web site.
Creative Commons License
IMPORTANT COPYRIGHT NOTE: Electronics and Robotics Articles by Ibrahim KAMAL are licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.