Introduction: Digital Frequency Counter
First of all, if you like this project, please vote for me in the Hurricane Laser Competition and the LED Contest!
Have you ever needed to find out how quickly something is oscillating? For things like hamster wheels and ocean waves, this is easy. You just count how many times the wheel goes around in a certain amount of time, then do some math, and you have the frequency. But what if you're dealing with something a little less tangible like audio waves or electronic signals? That's where digital electronics saves the day.
This Instructable will show you how to build a digital frequency counter capable of measuring events up to 250kHz and will attempt to explain some of the electronics theory behind it. The project costs about $30US, assuming you already have all the tools and equipment required.
This is actually my term project in a Digital Electronics course at the University of Oregon. It's intended as an addition to my term project from the winter term, which was a Function Generator I also posted to this site.
Step 1: Description
A frequency counter is a device that measures a periodic signal and determines its frequency in Hertz (Hz or s-1). But how does it do it? If you were trying to determine the frequency of a wheel manually, you might follow this operation:
1. Mark a spot on the wheel, then let it spin.
2. Start a timer.
3. Count how many times the spot on the wheel reaches the top.
4. Stop the timer.
5. Divide the number of rotations by the amount of time that passed while you were counting.
The result of the division would be the frequency of rotation of the wheel. This build actually follows the same steps, but it does all the operations automatically, repetitively, and it doesn't actually do any math. The math is built into the way the frequency counter counts.
This frequency counter actually follows these steps:
1. Generate a stable, known, reference time period.
2. Count the number of periods of the signal you're measuring.
3. At the end of the reference time period, display the number of counts and reset the count to zero.
These steps can be completed very quickly and repetitively using digital electronics methods. It's a very "dumb" process. The accuracy of the output frequency depends wholly on the accuracy of the reference time period and the speed of the counting chips. Using quartz crystal oscillators, we can generate reference periods with an error of about 20ppm, or 0.002%. Digital electronics can typically handle up to 30MHz signals, so this project is actually quite accurate.
The key to keeping any math out of the device is to make the reference periods base ten (decimal). Digital electronics is all base two (binary) so we need some chips that let us do some decimal conversion. What would be really nice is to just have a 1 second reference period. Then we could just count the number of pulses from the measurement in one second and display that and we'd automatically have Hz. That's actually what's going on.
But there are only three digits for display. How do we keep the display from rolling over? Let's say we have a frequency of 1,234Hz that we want to measure. If we counted for one second, the display would show 234 since it would roll over once. That's no good. But what if we only count for 1/10th of a second? Then we would get a count of 123 since 1234Hz * 0.1s = 123.4 (the .4 gets rounded, digital only deals in integers). If we KNOW we're only counting for a tenth of a second, we just multiply this count by 10 to get 1230Hz. This works for higher frequencies too. If you know the order of magnitude of the reference period, you can just do some easy 10^n multiplication and get a three-significant-digit result.
Step 2: Parts
The schematic is attached below in PDF form and as the original EagleCAD 6.2.0 file.
|Quantity||Description||Mouser Part Number|
|1||7805 +5V Voltage Regulator||863-MC7805CTG|
|1||LM311 high speed comparator||-|
|1||7404 Inverter gate||771-74HC04N|
|1||7402 NOR gate||771-NF02N602|
|5||4518 Dual BCD Counter||595-CD4518BE|
|1||74151 8-to-1 Multiplexer||771-74HC151N|
|1||7474 Dual D-FlipFlop||595-SN74LS74AN|
|4||4511 BCD to 7-Segment Decoder||771-74HC4511N|
|1||100kHz Crystal Oscillator||520-ECS-100-12.5-13X|
|2||100pF Ceramic Capacitor||594-S101K29SL0N6TK5R|
|1||3-Digit 7-Segment Display||696-LDT-C512RI|
|1||1-Digit 7-Segment Display||859-LTS-4301JG|
|2||SPDT Momentary Push Switch||688-SPPH430200|
|1||Protoboard (at least 8x11 cm)||-|
|1||40 Conductor Ribbon Cable w/ connector||-|
Step 3: Description of Parts
Comparators are similar to analog op-amps, but instead of being used as amplifiers (negative feedback), they are used to make a very fast decision about whether one voltage is bigger than another (positive feedback). Comparators usually have either an "open collector" and "open emitter". The LM311 has both. This just means the comparator controls an internal transistor, the voltages to which can be user selected. For this project, we hold the collector at +5 through a pullup resistor, and set the emitter to ground. This generates a TTL signal. The comparison operation is as follows:
|If pin 2 > pin 3, then output = HIGH|
|If pin 2 < pin 3, then output = LOW|
Inverters are digital logic gates that convert a "True" condition to a "False" condition. For TTL, +5V is generally "True" or HIGH, and 0V is "False" or LOW. An inverter simply negates the truth of its input.
7402 NOR Gate
NOR gates are OR gates with an inverted output. An OR gate adds two bits together, A + B = Q. It adds in the logical NON-ALGEBRAIC sense, as in 1 + 1 = 1. If A is true OR B is true, then at least one of them is true.
4518 BCD Counter
Counters do exactly what you think they do. When you input a series of pulses to a counter input, the outputs reflect the number of pulses the input has received. All digital counters count in binary, but they can have different limits for their maximum count. A standard 4bit counter counts from 0 to 15 (0000 to 1111 in binary). A BCD (Binary Coded Decimal) counter is intended for math involving decimal (base ten), so they only count from 0 to 9 (0000 to 1001). This obviously wastes bit capacity, but it makes certain things easier. Inputting a constant frequency into a counter results in the division of the frequency. A one-bit counter will divide the input frequency by 2. A 4 bit counter can divide the input frequency by 16. A BCD counter divides the input frequency by 10.
These are used to select between up to 8 input lines. A thee bit address is fed to the multiplexer, which selects the desired input. Then whatever signal is on the selected input appears at the output. They are the digital equivalent of an 8 position rotary switch.
7474 D-Flip Flop
These are almost infinitely useful in digital electronics. In fact, many components can be decomposed into a combination of D-Flip Flops. They are, in essence, a one bit counter. An input is given on D and the FF does nothing until a clock pulse is given, then the FF places the input value on the output Q. That's it. They are used as a one bit memory for this reason. 1. Input bit to be stored, 2. command storage, 3. stored bit appears on output until overwritten. The two FFs in this project are used as a sort of "shift register", which propagates a single command bit along a line of D-FFs.
4511 BCD to 7-Segment
This chip converts a binary coded decimal address (from the counters) into a pattern that correctly lights up a 7-segment decimal display. It takes a TON of logic gates to do this, so having it all on one chip is really nice. These also come equipped with several neat features such as a BLINK pin and a LATCH pin. Blink shuts off the outputs when the blink pin is held low. This is used to very quickly turn the displays on and off at low duty cycle to conserve power (they draw about .6A at continuous on, and get quite hot). Latch is used to capture the counts of the BCD counters and hold that number until the next latch command. This is so we only see the final count instead of 0,1,2,3,4...final count,0,1,2... etc.
100kHz Crystal Oscillator
This is what enables the frequency counter to get an accurate measurement. We have to compare the thing we're measuring to SOMETHING. Crystal oscillators are highly precise, with typical frequency errors of 20-40 ppm. That means this crystal would lose about 15 minutes after 1 year of operation. They're not perfect, but for this project, they're actually kind of overkill. But they're also cheap, cheap, cheap. They operate on the piezoelectric effect. When they receive a voltage, they physically expand very slightly (on the order of nanometers). Then when they contract due to their own elasticity, they generate a new voltage. Since the expansion oscillation is 90 degrees out of phase with the voltage oscillation, we can use it in combination with some capacitors to get an oscillation that is 180 degrees out of phase with the driving voltage. When this condition is met, positive feedback ensues and we get a stable oscillation. The crystal's shape and mass ensures that it oscillates at exactly 100kHz. Manufacturers make crystals in thousands of different frequencies, 100kHz is just convenient for this project.
Step 4: Play!
Once you get your parts, you should probably play with them. If you don't have a breadboard, you should definitely invest in one. Digital components are robust, it's actually pretty hard to fry them in my experience, especially these simpler chips. The one thing you should NEVER do is connect two outputs together. You don't want one chip trying to hold it's output HIGH while the other is trying to hold it's output LOW. Not only is this bad for the chips, it's logically abhorrent! You can connect all the inputs you want together, but never, ever put two outputs on the same line unless the chips have what's called a Three-State Output. But none of these chips have that, so just don't do it. Anything else is pretty much ok.
Plug your chips into power and see what they do when you give them different inputs. Test the truth table for the inverters and NOR gates. Try measuring the count after manually giving a counter a clock signal. Make sure the multiplexer works as described. You'll learn way more from playing with these components than you ever will by reading about them. If you have access to an oscilloscope and function generator, you will have a MUCH easier time examining your chips.
Off you go.
Step 5: Board Layout
The positioning of your chips and other components is an important detail. Finding a layout that has a good functional flow and also minimizes connection length can be tricky. The layout I used is attached below in PDF form, but feel free to choose your own. I ended up with a protoboard that was 7.5x11cm that perfectly held all of my components.
Step 6: Making Connections
This is easily the most tedious step. One of the nicest things about digital electronics is that almost no calculations are required. Each chip performs a specific task and is designed to work with just about all other types of digital electronics. However, the number of connections is much higher than most of the analog projects you'll encounter.
Use some lower gauge wire (~26AWG) to supply power to all your components first. This tacks your chips in place and allows you to test that all of them are getting good power before continuing (kind of important).
To make all the other connections, I recommend using wire wrapping techniques. Wire wrapping tools and wire are available at Radio Shack. Follow the images to get an idea of what to do. The basic idea is that we'd like to be able to make all these tiny little connections consistently and cleanly, without the use of solder. Soldering has several problems. First, you only want to apply soldering heat to sensitive ICs if it's absolutely necessary. Second, for ICs soldering is pretty much permanent. Have you ever tried to desolder an IC? You don't want to have to do that. When all the connections are made and the thing works, then you get to solder your ICs.
Step 7: Making Even More Connections
Like I said, this step is highly tedious. Just keep at it. Play some music. Eventually you'll have so many wires that the back of the board almost feels furry. Don't forget to install the 2x20 pin block that will allow you to connect the ribbon cable to the mainboard. Make sure you know which pins are which, and number them on the mainboard so they sync up with the 7 segment displays. You don't want to wire everything up and realize you forgot to mirror the numbering on the backside...
Step 8: LED Display
Ok, let's take a break from wiring up the back of the main board. You saved the extra pieces that you cut off of the protoboard, right?
Take the length of 40 connector ribbon cable and find a way to strip all the wires evenly. I did this by scoring both sides with a utility knife, then firmly pulling on this score mark with a flat head screw driver. With enough work, the insulator should begin to come away from the rest of the cable, leaving 40 evenly spaced, exposed wires. You need to feed these wires into a 3 x 7.5cm chunk of protoboard. To do this I had to twist all the little wires by hand, then tin them with solder to make them solid. Since the ribbon cable's wires are spaced by 0.05 inch, and the protoboard is spaced every 0.10 inch, you'll have to systematically place every other wire in one row of holes, with the other wires in the next row of holes. Doing this for 40 wires all at once is a little tricky, but it can be done. Solder them in place as soon as you can. You don't want to lose all that work.
Place the 3-digit and 1-digit 7 segment displays into the little chunk of protoboard. I left 5 holes between the large and small displays. Later I'll be putting a label in this space indicating that the small digit is an order of magnitude multiplier.
Use wire wrap to connect the displays to the ribbon cable. I decided to call the red wire on the ribbon "1", and number upward after that, with each number corresponding to the pin number of the large display. The large display has 28 pins, so the 29th wire connects to pin 1 of the small display. The small display has 10 pins, so the total number of wires required is 38. I chose to ignore the fact that there are redundant pins and unused decimal point pins at this stage. I just wanted the ribbon cable to be systematic and easy to connect.
Step 9: Put It All Together
So, you're done making all your connections? Ha. I doubt it. A nice thing about digital electronics is that it doesn't really matter. Connect it to power!
No sparks? Good. Does it work? That's what I thought. Do you have access to an oscilloscope? No? Hrm. I suggest going through the schematic and using a continuity tester to make absolutely sure you've made all the necessary connections. If you've got an oscilloscope, it's relatively easy to find where signals are being lost.
In all likelihood you will at least have a few numbers displaying zero correctly. Use the working numbers to find and fix your errors in the other numbers.
If you suspect your clock isn't working, connect an led from Pin 6 of IC5 to Ground. It should blink each second. If it does, then all the other clocks must be fine. If it isn't blinking, then try to find bad connections in your cascaded counters.
Step 10: Function Testing
Whew, hopefully that troubleshooting went better for you than it did for me.
Now it's time to hook up the signal input to some sort of function generator. You should have some way of telling if the frequency counter is doing its job well. Ironically, you'll need a frequency counter for that. If you don't have access to a function generator that displays frequency, then you'll need to find some other way to determine the accuracy of your frequency counter. You may be able to use your computer to generate a known frequency, for example.
When I tested the accuracy of my counter with a laboratory function generator, I found that all the measurements were more or less spot on. The frequency counter worked exactly as I expected. Except for the the highest order of magnitude. I found that, when measuring frequencies higher than about 100kHz, the count is too low by 10% or so. The reason for this has to do with the use of a 100kHz crystal as the reference signal. There is a shift register in the frequency counter that performs two operations, one right after the other. This is done to prevent the counters from being erased before the counts can be latched to the 7-segment display encoders. However, it causes a very slight delay on the order of two clock pulses. When measuring high frequencies, this delay becomes significant, while low frequencies are still measured quite accurately.
This can be fixed extremely easily, I was just to lazy to do it. You're welcome to try though. Instead of a 100kHz crystal, use a 1MHz crystal. Then use the extra BCD counter (remember, we only used half of a dual BCD counter earlier) to divide that by 10. This will make the time delay an order of magnitude shorter, resulting in a 1% error at high frequencies rather than a 10% error.
Step 11: Installing the Frequency Counter Into a Function Generator
The whole point of this project for me was to install the frequency counter into the function generator I made, so that I'd have an actual function generator of reasonable quality that I could use for my electronics projects. That meant finding room inside the already packed ATX power supply case I had used. I had to push wires out of the way, make sure nothing would short, and fit the frequency counter board into the cavity above the function generator's board. Then I had to tap into the +12v and ground lines to power the frequency counter's 5v regulator. Then I connected the high impedance signal output of the function generator to the signal input of the frequency counter. When this was tested, it worked just fine.
Then I had to cut into the ATX power supply case with a Dremel tool to make rectangular openings for the LED display. This was pretty tricky, and quite messy. If you're doing the same thing, I highly recommend cutting this hole before painting and installing knobs and buttons into the casing.
I also needed to drill holes for the screws that would hold the display in place, as well as holes for the two momentary buttons that control order of magnitude and latch override.
Once you install the frequency counter into some kind of housing, the project is complete!
Thanks for reading this Instructable, and watch out for my next one, which I'm in the process of building: An Arduino powered, sonic position sensor equipped carpet rover!
Finalist in the
LED Contest with Elemental LED
We have a be nice policy.
Please be positive and constructive.