Username:    Password: Remember me


5hz to 500 Khz Frequency-Meter
Home-made, accurate, and simple solution!
By Ibrahim Kamal
Last update: 4/4/08

Overview
Based on the famous AT89C52 microcontroller , this 500 Khz frequency-Meter will be enough to trace and debug most of your circuits, to adjust 555 timers frequency and perform all kind of frequency measurements in Digital circuits. And once you have this tool between your hand, you will get used to it to the point of no-return! and you will say "remember the days we adjusted the frequency by trial and error till we get it..!"

Note: This tutorial assumes you already have some basic notions about microcontrollers and general electronics skills.

To understand the block diagram below, you should recall the simplest definition of frequency: "the number of occurrences within a given time period". What we are trying to do is to count the number of electric pulses during a time of one second. To do this we need a counter, to count the pulses, and a timer so that every 1000 milliseconds, the processor stop counting, calculate the frequency and display it, then start counting again from zero.



This is the principle of operation of a frequency meter, however, to build a reliable frequency meter, we will need to do some more mathematical operations. For example: Some operations will accurately predict the frequency before waiting for a whole second to elapse, which will also increase the refresh rate. Another minor upgrade is to to display the average of the last 5 reading rather than displaying the frequency instantaneously (which can cause lot of display flickering if the frequency being measured is not very stable).


This project will be discussed in two Parts, the Hardware and the Software.

PART 1: HARDWARE
The Hardware Parts
Lets start by this easy part,to make the project look far-or-less like a profession lab equipment, well I simply built it in an old Avo-Meter, one of those you find in stores for less than $5! (god bless china!), and then painted it all in black. (it was originally yellow!)

And by the way, after I hacked that poor Avo-Meter, I also used the same leads for our frequency meter.

As you can notice in the picture, there are 4 seven segments display

Finally you can also notice the 2 connections for the leads (Blue shaded area). those connectors are standard for the test leads of most AVO meters and testing devices.

The Display
Before Getting into the electronics, here is a final trick: To make the display look even neater and Protect it from dust and scratches, simply add a thin plastic film on top of it (it will be firmly held in its place when the frequency meter is re-assembled).


The electronics

I am sure that you understand that to accomplish this project, we will need to perform some mathematical operations, as mentioned before, to increase the performance of the device. Thus we will rely on an AT89C52 microcontroller to perform all the required tasks in this project.


The Display system:

While being very simple (as you can see in this diagram), the display system can seem complicated due to the high number of connections (as you can see in the schematic below).

The main idea for this display system, is to connect all the 7-segment cells together in parallel, but only power one of them in the same time.
for example, here is the sequence to show the number "1984":
1-Send the number "1" though the data lines
2-Energize the first cell while all other cells are off
3-wait for a short time delay (in my code i paused for 0.6 Millisecond)
 
7-Send the number "8" though the data lines
8-Energize the third cell while all other cells are off
9-wait for a short time delay..
 
4-Send the number "9" though the data lines
5-Energize the second cell while all other cells are off
6-wait for a short time delay..
10-Send the number "4" though the data lines
11-Energize the fourth cell while all other cells are off
12-wait for a short time delay..

13-Start over from the step No: 1.
Believe it or not, the human eye wont notice anything wrong, because, with the given delays the display will refresh at the rate of 375 cycles per second (375 Hz!) and the human eye can hardly notice some flickering in a display refreshing at 24 Hz.
Now That you understand the display process, you should be able to easily follow the schematic above.

Here is a brief description:

Q1,Q2,Q3,Q4 2N2222 switching transistors to drive the 7-segment displays.
DD1 to DD4 7-segmets display, common Anode type.
Note that it is called 7 segments, but actually each cell contain 8 leds (7 leds for the digit, and 1 led for the decimal point)
R21 to R24 1Kohm pull up resistors
R35 to R38 100Kohm pull down resistors
U1 The AT89C52 Micro-Controller (actually you can only see the Port 0 and the 4 used pins of PORT 1)

Input leads connections:
Nothing really critical about this part, since we only intend to measure TTL frequencies, the input is directly fed to the TO pin of the AT89C52, which will be used as a counter (more details about this in the software part).

P1 and P2 are simply the 2 connections for the test leads. Note that in order to make any kind of measurements on another circuit, the ground of the Measurement device and of the circuit being tested must be connected together, this is why there are at least 2 leads in any testing device (one for the Ground and one for the measured signal).



Power Supply, clock generator, and reset button:
The Power supply here is not critical either, because the device will be powered from a 9V alkaline battery, so we don't need any filter capacitors, we just need to reduce 9V to 5V, and this is simply the job of the IC 7805 (Usually we add decoupling and filter capacitors when there is a lot of disturbance like in motor controllers, or AC to DC power supplies)

Power supply

This part is standard in any circuit containing an AT89C52.

-The switch SW1 will reset the micro-controller and start the program from the beginning.

-C1 and C2 are 27pF, any nearby value between 20 and 40 pF will work just fine.

-X is a 24 Mhz crystal. it is imperative to use a crystal of this frequency, which is the maximum frequency the AT89C52 can respond to. using a


Clock generation, and reset button
12Mhz crystals will result in maximum measurable frequency of 250 Khz instead of 500Khz.
Don't forget to connect the PIN 31 to the 5V, otherwise the microcontroller wont work.



PART 2: SOFTWARE
NOTE: While I tried to make things as clear as i could, this is not a tutorial to explain to beginners what is a microcontroller and how to use it...

Now you have to bear in mind the simple block diagram of a frequency meter shown at the top of the page. You can download the whole code in one file here, but even a professional programmer would easily get lost in someone's else code, so my advice is to try to understand how it is working, and then write your own version of the code.
The Main loop
Now, here is a simplified flow chart of the main loop. next to each box, you can see the corresponding source code, this can help you to understand how each step is programmed.
setup_interrupts(){
EA = 1;
ET0 = 1; //Enable the Timer/counter 0 interrupt
TR0 = 1; //Enable Timer/counter 0 to count
TMOD = 0X25; //counter 0 in mode 1 (16 bit counter) , timer 1 in mode 2 (auto reload from TH1
TL0 = 0; //empty the counting registers
TH0 = 0; //empty the counting registers
TH1 = 100; //start timer 1 from 0
ET1 = 1; //enable timer 1 interrupt
TR1 = 1; //Enable Timer/counter 1 to count
PT0 = 1; // Lower priority for timer 0
PT1 = 0; // higher priority for timer 1
}
calc_del++;
if (calc_del > (2248/scale)){ // update data
calc_del = 0;
f = (TL0 + (TH0* 256));
sample[4] = sample[3];
sample[3] = sample[2];
sample[2] = sample[1];
sample[1] = sample[0];

sample[0] = f;
TL0 = 0; // reset the two registers of
TH0 = 0; // the 16 bit counter

f = floor(
(sample[0] + sample[1] + sample[2]+ sample[3]+ sample[4]) /5
)*scale;

void int_to_digits(unsigned long number){
float itd_a,itd_b;
itd_a = itd_a = number / 10.0;
dig[3] = floor((modf(itd_a,&itd_b)* 10)+0.5);
itd_a = itd_b / 10.0;
dig[2] = floor((modf(itd_a,&itd_b)* 10)+0.5);
itd_a = itd_b / 10.0;
dig[1] = floor((modf(itd_a,&itd_b)* 10)+0.5);
itd_a = itd_b / 10.0;
dig[0] = floor((modf(itd_a,&itd_b)* 10)+0.5);
}

if (f < 1000){
pt[0] = 128;
pt[1] = 0;
pt[2] = 0;
pt[3] = 0;
}else if ((f > 999) & (f < 10000)){
f = f / 1;
pt[0] = 128;
pt[1] = 0;
pt[2] = 0;
pt[3] = 0;
}else if ((f > 9999) & (f < 100000)){
f = f / 10;
pt[0] = 0;
pt[1] = 128;
pt[2] = 0;
pt[3] = 0;
}else if ((f > 99999) & (f < 1000000)){
f = f / 100;
pt[0] = 0;
pt[1] = 0;
pt[2] = 128;
pt[3] = 0;
}else if ((f > 999999)){
f = f / 1000;
pt[0] = 0;
pt[1] = 0;
pt[2] = 0;
pt[3] = 128;
}


The Counter and the Timer.
the function count_pulses() , which is linked to the "Interrupt 1", will be executed each time the TIMER 0 overflows. Actually this should never happen, because the counting registers are emptied every 1/5 second (if scale = 5, by default), but in case the timer overflows, we should increase the variable: "scale", hence the counting variables will be emptied every 1/6 second, and if the the TIMER 0 still overflows, scale will be increased to 7, and so on, until the system stabilizes at a point where the counting registers are being emptied fast enough, then the calculated frequency is correct. count_pulses() interrupt 1 //counter 0 interrupt
{
if (scale < 200)
scale++;
}
The function calc_and_disp() will be executed every 0.66 millisecond (this is defined by the variable TH1 in the setup_interrrupts() function) Each time this function is executed, one of the 4 7-segments is energized, and a corresponding digit is being sent, then, next time the function is called, the next digit is energized to show the next corresponding number... And the sequence goes on as explained before in the display system
Note: dcnt is the variable used to select on of the 4 display cells.
calc_and_disp() interrupt 3 {
P0 = (bcd[dig[3-dcnt]] - pt[3-dcnt]) ;
P1 = ord[3-dcnt];
dcnt++;
if(dcnt > 3){
dcnt = 0;
}
}

Now you should be able to build your own Frequency meter. To help you a litle more, I'm giving you a zip file containing the full schematic, the PCB design and the source code. [note: i use ExpressPCB(FREEWARE) to design the schematics and the PCB]

Download the zip file for this project

Join the Mailing List
Let us get in touch with you when we upload new interesting content.
Name:
Email:
Subscribe  Unsubscribe 



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:
User avatar
ikalogic

on: 10 May 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quoting ingbaja: Thanks again for your answer.
OK, I canīt make the measuremenet on the wheels or the drive sahft becausse it rotates at a diferent speed that the engiene, and also itīs variable. The kart doesnīt have a shifting transmision, but you can change (prior to racing) the ratio between engine and drive shaft to optimize performance at each track.
Also I donīt have to put a magnetic sensor, the magnet is already there, it is used for the spark timing and since itīs a 1 piston two stroke engine, every pulse the magnet sends to the spark generator represents 1 revolution so i figured I could use that same pulse (since it is low voltage)


Well you convinced me! good luck then!

Let me know how if you need help :)
Posted by:
ingbaja
on: 10 May 2008
500 Khz Frequency meter (using AT89S52)
['Quote ]
Thanks again for your answer.
OK, I canīt make the measuremenet on the wheels or the drive sahft becausse it rotates at a diferent speed that the engiene, and also itīs variable. The kart doesnīt have a shifting transmision, but you can change (prior to racing) the ratio between engine and drive shaft to optimize performance at each track.
Also I donīt have to put a magnetic sensor, the magnet is already there, it is used for the spark timing and since itīs a 1 piston two stroke engine, every pulse the magnet sends to the spark generator represents 1 revolution so i figured I could use that same pulse (since it is low voltage)
Posted by:
User avatar
ikalogic

on: 09 May 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]
I still can't get why attaching white strip would be more difficult that installing a magnetic sensor! Why not add that a white stripe to one of the wheels?
Posted by:
ingbaja
on: 09 May 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]
Thanks for your answer. Actually I read the other article, but i donīt need a contach less feature (I canīt open the engine, well, I can but the shaft is very thin, an also it is very crouded in that part of the kart to put an optical mark and receiver) so i would prefer a "permanent" tachometer if you know what I mean and i think that using the magneto signal (wich is very easy to get) could save me some trouble. I need the tachometer not just for static testing, i need it in order to "take care" of my engine, since the kart has no gears and no rpm regulator and itīs quite common to "blow up" engines in long straights, so i figured out i could mount it on the steering wheel. Ther are some gadgets in the market wich could take care of my problem, but down here (Iīm from Mexico) theīre pretty expensive.
Thanks again
Posted by:
User avatar
ikalogic

on: 09 May 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quoting ingbaja: Hello.
I want to use the frequency meter as a tachometer for a go kart (using the signal sent from the magnet). The kart engine reaches 19 000 rpm (about 300 Hz). I guess it should work in Hz (to work acurattely) but I would like the frequency displayed in rpms. Do you think it would work fine? or am I trying to kill a mosquito with a cannon?
Thank you


Maybe you meant to build this? [link]

No it's not building a cannon to kill a mosquito.
Posted by:
ingbaja
on: 09 May 2008
500 Khz Frequency meter (using AT89S52)
['Quote ]
Hello.
I want to use the frequency meter as a tachometer for a go kart (using the signal sent from the magnet). The kart engine reaches 19 000 rpm (about 300 Hz). I guess it should work in Hz (to work acurattely) but I would like the frequency displayed in rpms. Do you think it would work fine? or am I trying to kill a mosquito with a cannon?
Thank you
Posted by:
User avatar
ikalogic

on: 13 Apr 2008
Re: 500 Khz Frequency meter (using ATMega32)
['Quote ]

Quoting shakir khan: hi ibrahim;
i m a new learner of micro controller.how can i use atmega32 to measure frequency up to 10Khz.
pls let me know.'ll be greatful.
bye



An article about an ATMEGA based frequency meter is coming soon!
Posted by:
shakir khan
on: 12 Apr 2008
500 Khz Frequency meter (using ATMega32)
['Quote ]
hi ibrahim;
i m a new learner of micro controller.how can i use atmega32 to measure frequency up to 10Khz.
pls let me know.'ll be greatful.
bye
Posted by:
User avatar
ikalogic

on: 28 Feb 2008
Project folder
['Quote ]
Project folder for the frequency meter projects, containing the PCB layout, HEX files, and schematic.
Posted by:
User avatar
ikalogic

on: 28 Feb 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quote: for meansure alternative signal ,what ADC can i use?can you give me a hint?

For measuring Ac sugnals.. I am not sure, but i think you'll have to pass the AC signal through a half-wave rectivier (just one diode), then through a resistor-Zener diode configuration, to output approxilately 5V / 0V pulses whose frequency correspond to the frequency of the orriginal AC signals.

Note from MVS SARMA:
Quote: when the meter is called frequency counter, input is Ac . In this case it is square wave. if you want to use sinewave input , you may have to use interface. 1. if the input is high enough, you have to attenuate by resistive divider,
the signal can then be level shifted by a common emitter amplifier with a NPN transistor like 2N2222 or 2N2369 or even BC547 . the biasing can be done such that with a collector load of 470 ohms and a resistor between collector and base( not Vcc to base) of say 47K in such a way that the voltage at the collector is at 2.5V.
then the collector can be connected to the 89S51 or S52 counter input pin with a series resistor of 470 ohms again. the input to the base can be thro a 0.47uF capacitor. the input signal can be fed to the cap witha 100 ohms series resistor . the juction of 100 ohms and 0.47 can be protected with 2 nos of in4148 diodes connected anti parallel and other side connected to ground.


with this arrangement you can convert AC sinewave signals to a suitable level for input to controller.
All the best

Posted by:
User avatar
ikalogic

on: 28 Feb 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quote: Hi Ibrahim , Iam looking to measure frequency up to 30 Mhz , how do that by the same or a similar project, Since I have a Desktop Frequency Counter but Iam looking to have a handi one ...
Thanks


The solution to this problem is in one word: "The prescaler". some micro-controllers like the ATMEGA family have included hardware prescallers, or you may build your own easily. i should be adding a schematic for a prescaller on the site soon. Time while, you can google for "prescaler+frequqncy meter" and you should find what you need.
Posted by:
User avatar
ikalogic

on: 28 Feb 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quote: Asslam-o-alikum Mr Ibrahim,
I have a question: can i burn the hex file on At89S52 controller,or i have to do changes.

Also max values in 4 digits we can have are 9999. neraly 10k u r saying 500k. :?:


the hex file can be burnt on an AT89S52.

The reading of this tachometer is in KILO hertz, not in hertz. for example:
0.300 = 300 Hz
45.23 = 45.23 Khz
The 500K limit is not due to the number of seven segment cells, it's due to limitations in the internal circuitry of the miro-controller.
Posted by:
User avatar
ikalogic

on: 28 Feb 2008
Re: 500 Khz Frequency meter (using AT89S52)
['Quote ]

Quote: About the programming of the IC. Do you have any constraction of the programmer. What software can I use to programme the IC.

Im newbie thats all this questions.

I understand how the algorithm work but I don't how to make the programming.

Thank you.
About the programming of the IC. Do you have any constraction of the programmer. What software can I use to programme the IC.

Im newbie thats all this questions.

I understand how the algorithm work but I don't how to make the programming.

Thank you.
Posted by:
User avatar
ikalogic

on: 28 Feb 2008
500 Khz Frequency meter (using AT89S52)
['Quote ]

Quote: I want to make a frequency meter for 0Hz - 9999Hz. Can I make this meter using and changing your project?It easy to make the changes?

Thank you.


hello nick, actualy this design is suitable with the frequency range you'r talking about. This design measures up to 500 Khz and you want to measure up to 10 Khz. So, it will work for sure. Now if you want it to be dedicated to the small range of 0 to 9999, then you can make some slight software modifications. but i guess it would be useless.

good luck.
You have to be a member to post replies.
Username: Remember me
Register now! it only takes an instant.
Forgot your password?
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.