Username:    Password: Remember me
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:
uttam
on: 07 Jul 2010
40 MHz frequency meter
['Quote ]
To Mr. IKA,

I want to measure very less frequency, like 3 to 10 HZ, will this frequency meter work, Will there be a problem if ON /OFF pulse width vary, like more pulse width in less frequency and less pulse width in high frequency.-- please reply
regards,
Uttam Dutta
Posted by:
radamta
on: 02 Jun 2010
40 MHz frequency meter
['Quote ]
thanks very much
Posted by:
xsilvergs
on: 25 May 2010
40 MHz frequency meter
['Quote ]
I have the counter working but really need it to work at low frequency but have trouble getting readings below 200Hz.

I'm trying to read frequencies around 20Hz, any ideas why it shouldn't work?

Thanks for any help.
Posted by:
User avatar
ikalogic

on: 20 May 2010
Re: 40 MHz frequency meter
['Quote ]
Thanks a lot xsilvergs for this contribution! :)
Posted by:
xsilvergs
on: 20 May 2010
Re: 40 MHz frequency meter
['Quote ]
OK, it was to do with the programming of the JTAG via the fuses.

I used the calculator from here [link] which gave me results thus -U lfuse:w:0xe4:m -U hfuse:w:0xd9:m .

Then useing cmd.exe I programmed the low fuse thus:
avrdude -c bsd -p ATMEGA16 -U lfuse:w:0xE4:m
and the high fuse thus:
avrdude -c bsd -p ATMEGA16 -U hfuse:w:0xD9:m

as per this [link]

hope that helps somebody.
Posted by:
xsilvergs
on: 19 May 2010
40 MHz frequency meter
['Quote ]
Hi, I've just tried to build this as my first project and find that pins 24 to 27 do not change state. Is it due to the JTAG interface being enabled? If it is, how can I disable it in code?

Thanks
Posted by:
pankajpatil26
on: 25 Mar 2010
40 MHz frequency meter
['Quote ]
i want to c

conect HTf3223 humidity sensor to atmega 32 and display it on lcd to use port c and contoll line rs and en to PA7 & PA6 what will be the code

plz send me
Posted by:
User avatar
ikalogic

on: 11 Mar 2010
Re: 40 MHz frequency meter
['Quote ]

Quoting n_huda2006: hi ika,
Can I ask for an another HEX code for the digital frequency meter, it seems that the output pins from 24 to 27 or the (B,C,G and H/decimal point) pins of the 7 segment displays seemed to be shorted to each other..This is maybe the cause why the output display of the 7 segments are just C's..I have checked the connections with a multitester without the ATMEGA16 chip mounted and all the pins of the 7 segments seemed alright.I am having this doubt that maybe there is something wrong with the programming or the program itself..I have two ATMEGA16 chips and I have programmed them both, and unfortunately, both of them got the same output response..please help with this..
I am hoping for your immediate response..Thank you in advance!


What i can tell you for sure is that it's not a problem is the HEX file. many user (starting by me!) have used the same one without problems. what you should be looking at is the wiring of your 7-seg. i know that some displays have really weired wiring schemes, so work you way out with a resistor and a 5V power supply to figure out where is the common anode/cathode, and which pin light which segment.

Good luck
Posted by:
n_huda2006
on: 10 Mar 2010
40 MHz frequency meter
['Quote ]
hi ika,
Can I ask for an another HEX code for the digital frequency meter, it seems that the output pins from 24 to 27 or the (B,C,G and H/decimal point) pins of the 7 segment displays seemed to be shorted to each other..This is maybe the cause why the output display of the 7 segments are just C's..I have checked the connections with a multitester without the ATMEGA16 chip mounted and all the pins of the 7 segments seemed alright.I am having this doubt that maybe there is something wrong with the programming or the program itself..I have two ATMEGA16 chips and I have programmed them both, and unfortunately, both of them got the same output response..please help with this..
I am hoping for your immediate response..Thank you in advance!
Posted by:
n_huda2006
on: 19 Jan 2010
40 MHz frequency meter
['Quote ]
Hi

just CCCC's onLy and what is my problem!!pls answer asap.[b][/b]

nazmul huda
Bangladesh[b][/b]
Posted by:
diff.thinkr
on: 12 Dec 2009
Re: 40 MHz frequency meter
['Quote ]
What are the values of R1-R8?
Posted by:
User avatar
ikalogic

on: 12 Dec 2009
Re: 40 MHz frequency meter
['Quote ]

Quoting diff.thinkr: Also can I use 74LS191 instead of 74HC191. I am not able to get HC's here.
Thank you


yes
Posted by:
diff.thinkr
on: 12 Dec 2009
40 MHz frequency meter
['Quote ]
Also can I use 74LS191 instead of 74HC191. I am not able to get HC's here.
Thank you
Posted by:
diff.thinkr
on: 11 Dec 2009
40 MHz frequency meter
['Quote ]
What are the values of R1-R8?

Also is it ok to use common anode or common cathode type displays?
How do I distinguish them?
Posted by:
abhay289
on: 22 Nov 2009
40 MHz frequency meter
['Quote ]
can i use the Atmega 8 for doing this project
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.