Projects

Super Simple Squares

Frequency Generator and Counter Projects

Mike Hansell

Issue 18, December 2018

This article includes additional downloadable resources.
Please log in to access.

Log in

A super simple squarewave frequency generator and a frequency counter for your workbench.

BUILD TIME: 1.5 hours (each build)
DIFFICULTY RATING: Intermediate

Frequency generators and counters can be prized possessions on a maker’s workbench. However, they can be costly for something you may seldom use.

These super simple versions provide you a large majority of the functionality you’ll find in expensive units, at a fraction of the cost.

THE BROAD OVERVIEW

Squarewave generators are useful for a host of debugging functions. They can be quite useful for testing frequency response and transient handling in audio electronics or other electrical systems.

Squarewaves are found in digital electronics virtually everywhere, often as clock signals to time logic signals against. However, outside of that, you might struggle to understand what you’d use them for.

WHAT IS A SQUARE WAVE?

A 1kHz square wave is made up of a sine wave at the fundamental frequency (1kHz) PLUS odd harmonics (odd multiples of the fundamental, 3kHz, 5kHz etc). The addition of each odd harmonic “widens” the composite waveform to look more and more like a square wave. The perfect square wave would require every odd harmonic so is theoretically impossible to achieve. The fact that a squarewave is, in fact, a complex waveform makes it a good source of test signals as it’s not just a, for example, 1kHz signal, it includes a 3kHz component, a 5kHz component etc.

Inject a square wave into an input and examine the output. Does the output resemble the input? Any changes are in fact distortion. This may show a limited frequency response.

Square waves can be used as part of a system to synthesise musical instruments. They can be used to create triangle or sawtooth waveforms.

Due to the rapid (theoretically instantaneous) rise time (the vertical part of low to high mark) squarewaves are useful to test for transient response in electronic systems. How quickly can a system respond to a change of input signal? We all know that when you put your foot down on the accelerator in your car the engine takes some time to respond. Electrical systems are somewhat the same. Testing with squarewaves can show how fast it can respond.

Squarewaves are used to provide a clock or synchronisation signal in digital circuits. Our beloved Arduino runs on 16MHz squarewaves. The (relatively) precise and predictable nature of the square wave clock signal allows synchronisation of hardware and forms the basis of the delay() function which allows PWM, etc.

From the educational point of view, having a source of squarewaves of a number of frequencies is quite helpful in teaching digital electronics, especially when coupled with an oscilloscope. Students may understand the nature of a waveform, but seeing it and examining how a change of frequency looks is invaluable. They can experiment with logic gates, frequency dividers and multipliers, filters, etc.

During development, we found that a convenient source of a 1 second time reference pulse would be very handy to define a period to count incoming pulses in our Frequency Counter project. While we used a different approach, this device could easily have provided a solution.

ACCURACY AND STABILITY IN HARDWARE

Does a few hertz matter? Well that depends on what you want to achieve with the generated signal. If you are responsible for a TV station, for instance, you will need a very accurate frequency standard to ensure that the transmitters don’t wander off-frequency and cause interference with other devices and that your signal can be reliably found by TV receivers. Various time and frequency standards and devices are in operation around the world.

There’s a saying: “Standards are a wonderful thing. Everybody should have one.”. Well, that may be fair enough but in this modern world “close enough” is not nearly close enough. For instance, without exceptionally high accuracy frequency standards, GPS would not work. There are many levels of accuracy in frequency standards devices. Some devices fill a room, some can fit in your hand.

Before you have finished reading this article, the standard for the kilogram and the second may well have been re-defined. For some time now, frequency and hence time standards have been based on caesium atomic clocks. A newly developed optical atomic clock won’t lose or gain a second in 15 billion years. Much research is going on in this very high resolution and accuracy range of equipment.

What was at the very highest end of frequency standards, say those used to provide a standard for the second, would be something like the NIST-F2 caesium fountain atomic clock time standard. NIST (National Institute of Standards and Technology) is a physical standard laboratory based in the USA. The NIST-2 will not lose a second in at least 300 million years.

The next level down is the hydrogen MASER. You’ve no doubt heard of the LASER (Light Amplification by Stimulated Emission of Radiation), but the LASER (invented in 1960) was preceded (invented in 1953) by the MASER (Microwave Amplification by Stimulated Emission of Radiation).

These are suitable for applications like national timekeeping and GPS satellite monitoring. These work by “exciting” hydrogen atoms which emit radiation with a frequency of 1,420,405,751.7667Hz ±0.0009Hz. This frequency is very precise and extremely stable.

An example of this technology is the MHM 2010 device which weighs in at 216kgs without its backup batteries. It is specified to have a stability of between 2 × 1015 and 2 × 1016 per day. That’s very roughly 1 part in 1 million billion. This device is claimed to be 100 times more stable than the next level down of frequency standard, the caesium atomic clock. Cost? We couldn’t find a figure.

The next level down is the caesium atomic clock. These can be accurate to 1 second in 1.4 million years. A rough calculation makes that about 1 part in 3.8 × 1014. The size of these devices is around the size of a modern computer or if you understand rack technology, 3RU. We couldn’t find a cost on these but a good guess would be $100,000. Certainly not for the hobbyist.

Moving on from this is the rubidium clock. These are accurate to 1 second in 4000 years. You can hold one of these devices in your hand. They cost in the order of $5000.

A crystal oscillator as used in consumer electronics is “fairly” accurate and stable. If it is rated at 16MHz as in the case of an Arduino UNO, then it’s going to generate a frequency close to 16,000,000Hz, but may well have a tolerance of +-10 to +-50ppm. At 16MHz, the frequency may be out by +-160Hz to +-800Hz. The frequency generated by a crystal may suffer from jitter and drift. Jitter refers to a periodic change of frequency. Drift refers to change in accuracy of the output signal in relation to ambient temperature, humidity or pressure on the package. The frequency shift in relation to temperature may be in the order of +/-10 to 100ppm. The crystal’s frequency may simply drift over long periods of time, by say +/- 2ppm per year. You can see that while a crystal is considered accurate and stable, some inaccuracy is inherent.

Higher accuracy crystals may be packaged with integrated electronics to monitor and compensate for temperature induced drift. At the higher accuracy (and cost) end of the spectrum, the so-called OCXO (oven controlled crystal oscillator) crystal actually houses the crystal in a temperature controlled oven.

So, does a few Hertz matter? In the context of this project, yes and no. All of the frequencies that we generate are derived from a 16MHz crystal. A few hertz in 16,000,000 is not a lot for our purposes. During the development of this project, we used our oscilloscope to inspect the waveforms and measure the frequencies. The timing of the oscilloscope is most certainly derived from a crystal too. What if it’s crystal frequency was a little high and ours was a little low? Then that would compound the issue. Certainly, to calibrate any time or frequency source, a device of higher accuracy is required. Do you have a spare $5000 burning a hole in your pocket?

HANDLING CMOS

CMOS or Complementary Metal Oxide Semiconductor uses both N-channel and P-channel (hence complimentary) MOSFETs internally. These are EXTREMELY high impedance and a tiny static charge can damage them. Have you ever walked over carpet then touched something metal and gotten a “zap”? That is static electricity. This static charge may be transferred from your hands or tools. When working with CMOS it is imperative to eliminate this. If you touch a metal object that is connected to mains earth, for example, your computers metal case, that will at least temporarily drain the static off your hands. A better solution is to use an anti-static wrist strap (Jaycar TH1780, Altronics T4002, Core Electronics, seems to be MIA). The strap has a lead with an alligator clip that can be securely connected to something grounded.

Very early MOSFETs had a shield pin and all 4 pins were physically joined together by a metal ring. When the metal ring came off you had to be very careful or else!

TEST LEADS

We have opted for RCA sockets for the signal inputs and outputs. These are cheap and effective, but depending on your preferences and budget you could substitute a BNC connector or use a BNC to RCA adapter. This would allow using a high quality cable as used with oscilloscopes and professional gear. These typically have an alligator clip for the ground connection and a hook probe that can be attached to component legs. In the case of connecting directly to an IC pin, this is not a good choice as the test clips may create a short circuit to an adjacent pin. One advantage of using this type of lead is that the end of the probe with the hook can be easily removed revealing a metal probe that can be used to connect to IC pins etc.

As an alternative, an alligator clip for ground and a meter probe (such as Jaycar PP0425) may be ideal to get to those hard to reach places. Finally, we’ve specified IC test clips to allow attaching the test leads to your project. There’s no reason that you couldn’t have multiple sets of leads on hand to suit the current task. As we have separate CMOS and TTL level outputs, we have two separate signal interfaces to accommodate the different levels.

PROJECT FEATURES AND LIMITATIONS

These projects really aren’t comparable to retail-ready equipment in a shiny box. They are highly functional and also quite accurate, but they’re largely for reference and experimentation more than they are professional workbench equipment. However, you will also gain a powerful understanding of how a frequency generator or counter works, which is invaluable.

Frequency Generator:

At the heart of your favourite Arduino is probably an Atmel 328P microcontroller clocked by a 16MHz crystal. One of the things that seperates a microcontroller from a microprocessor is the extra hardware packed into an MCU. This project uses the crystal oscillator and internal counters/timers of the MCU to provide a fairly accurate and stable squarewave generator.

HOW IT WORKS

Using the internal hardware of the 328P we can programmatically generate squarewaves. The highest frequency we can obtain with this technique is 8MHz which is quite adequate for most hobbyist purposes. We use a rotary encoder to choose 1 of 22 frequencies and set the counter as necessary. Frequencies that we can generate are: 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 200kHZ, 100kHz, 50kHz, 20kHz, 10kHz, 5kHz, 2kHz, 1kHz, 500Hz, 200Hz, 100Hz, 50Hz, 20Hz, 10Hz, 5Hz, 2Hz and 1Hz.

The 328P also has a prescaler that can divide further, but its division ratios don’t suit our purposes and have not been used.

Oddly, generating stable waveforms is easier the closer we are to the speed of the crystal. To generate frequencies below 1kHz while retaining stability, we use our some additional ICs to do some division. We can then feed it a higher frequency which is more stable, however, obtain a lower frequency that’s still as stable.

To generate stable frequencies below 1kHz, we use three divide by 10 counter IC’s to give a total frequency division of 1000 times. To produce 500Hz, we provide 500kHz to the dividers which then gives us a stable 500Hz. To generate 100Hz and down we feed appropriate lower frequencies to the divide by 1000 circuit. We use a few logic gates to choose whether the output signal is derived directly from the 328p's internal counter or from the divide by 1000 circuit.

The frequency of the generated signal is displayed on a 4 × 20 line LCD. You could easily reconfigure things for a 2 × 16 LCD if you wanted to reduce the footprint, but we love these displays.

HOW THE SQUAREWAVES ARE GENERATED

As we all know, modern computers are digital. That is their signals are either on or off. In addition to that they may be tri-state, which means they are literally in a “disconnected” state. A squarewave generally has equal on (mark) and off (space) parts. If these are not equal then the signal is a pulse stream or rectangular wave. You may be aware of the PWM signals that an Arduino can generate.

There are cases when a squarewave of a particular frequency is required for a project. An Arduino (or Atmel 328P as we have used) can be programmed to produce squarewaves, but the software overhead limits the maximum frequency obtainable. Consider that it takes a few microseconds for a GPIO pin to change levels from a low to a high or a high level to a low. With no other overhead that’s a limit of about 166kHz. Our oscilloscope measured the actual frequency at an unstable 111kHz. The test code follows:

#define outputPin 10
void setup() 
{
  pinMode(outputPin, OUTPUT);
  while(1)
    {
    digitalWrite(outputPin, HIGH);
    digitalWrite(outputPin, LOW);  
    }
} // End of setup()
// loop() has nothing to do.
void loop() 
{
 ; 
  } // End of loop()

If you don’t quite follow what we’re doing here, don’t fret - we’ll cover it more during the build.

The Fundamental Build:

Frequency Generator Prototype

Parts Required:JaycarAltronicsCore Electronics
1 × Arduino UNO or EquivalentXC4410Z6240CE05629
3 × 4026 Decade Counter ICs XC4026 Z4026-
1 × 4050 CMOS Buffer ICZC4050Z4050-
1 × 74HC00 Quad 2 Input NAND Gate IC XC4800 Z8500014-9591745
1 × 3mm Red LEDZD0100Z0730POLOLU-1070
20 × 4 Line LCD Display QP5522 Z7032SS104990002
1 × Rotary Encoder SR1230S3350ADA377
1 × 220µF/25V Electrolytic CapacitorRE6158R5147CE05149
3 × 100nF Ceramic Capacitors RC5360 R2865CE05188
2 × 100kΩ ¼W Resistors RR0548 R7606-
2 × 10kΩ ¼W Resistors RR0596 R7582PRT-14491
1 × 330Ω 1/4W Resistor RR0560R7546PRT-14490

Parts Required:

We built the prototype on a large breadboard during development, which allows the theory to be tested easily.

It’s also a project for schools and educators who might like to explore squarewaves without expensive hardware.

BUILDING THE PROTOTYPE

Follow the fritzing diagram provided. The schematic below should assist with LCD pin assignments.

You’ll need a 5V power source, which can be taken from the UNO. The overall build is not terribly power intensive, so you won’t overload the onboard regulator.

The LCD is connected using its own breadboard, purely to make connectivity more robust. The pin headers on the LCD (you may have to solder them on yourself depending on where you get the LCD from) will securely connect to a small breadboard, so you can make the required connections to the UNO and the large breadboard.

There are a few points to take care with, most notably the ICs. We’ve tried to keep it fairly simple, with all ICs facing the same way. Most of the heavy lifting is done inside the chips themselves, so outside of power to each one, we only use a few links on each to get our desired connectivity. However, pin assignment is critical, so take care to follow the diagrams carefully.

Your rotary encoder will generally fit rather neatly into the breadboard too, however, some will have odd pin spacing or pins which are too large to fit. If this is the case, still wire it up as shown, just use some jumper wires between the breadboard and rotary encoder itself.

THE CODE

In the digital resources, you’ll find “frequency_generator.ino”. Load it onto your Arduino and let it boot. The sketch is quite straightforward and under 200 lines with about half of that devoted to updating the display. The code is based around controlling the internal hardware counters of the 328P. It is annotated and fairly easy to follow, take a look through the file to learn how it works.

The Main Build:

Frequency Generator

Parts Required:JaycarAltronicsCore Electronics
Atmel 328p ICZZ8727Z5125000-ATMEGA328+UNO+CAPS
16MHz crystalincluded with aboveV1289Aincluded with above
2 × 22pF Ceramic CapacitorsRC5316R2814included with above
3 × 4026 Decade Counter ICsXC4026Z4026-
1 × 4050 CMOS Buffer ICZC4050Z4050-
1 × 74HC00 Quad 2 Input NAND Gate ICXC4800Z8500014-9591745
1 × 28 Pin IC SocketPI6466P0541PRT-07942
3 × 16 Pin IC SocketsPI6502P0565PRT-07938
1 × 14 Pin IC SocketPI6501P0560PRT-07939
1 × 3mm Red LEDZD0100Z0730POLOLU-1070
20 × 4 Line LCD DisplayQP5522Z7032SS104990002
1 × Rotary EncoderSR1230S3350ADA377
1 × 220µF/25V Electrolytic CapacitorRE6158R5147CE05149
3 × 100nF Ceramic CapacitorsRC5360R2865CE05188
2 × 100kΩ ¼W ResistorsRR0548R7606-
2 × 10kΩ ¼W ResistorsRR0596R7582PRT-14491
1 × 330Ω ¼W ResistorRR0560R7546PRT-14490
1 × SPST Toggle SwitchST0335S1310POLOLU-1407
1 × RCA Panel Mount Socket – WhitePS0261P0151-
1 × RCA Panel Mount Socket – RedPS0259P0152-
1 × RCA Plug – WhitePP0244P0202A-
1 × RCA Plug – RedPP0240P0200A-
2 × Red IC Test ClipHM3036P9180AP9180
2 × Black IC Test ClipHM3037P9181AP9181
1 × 9VDC 1.7A Power SupplyMP3484M8904CEO4425
1 × DC Input JackPS0519P0620PRT-10811

Parts Required:

You may like to jump straight into the PCB build if you want this for your workbench, however, the prototype stage will help you understand a lot of what’s going on too! The PCB build will naturally have improved performance though, and reduce intermittent errors that can be present within a breadboard when using sensitive circuits.

The hardware required for this build is much the same as the prototype, however, there are additional components for interfacing, as well as the PCB and 3D printed case. There are also additional components for circuit protection. We have listed them again in entirety for clarity.

BUILDING THE PCB

The PCB is reasonably simple, however, is a double-sided design to keep routing as neat as possible. There are only three tracks on the top layer, however it solves a few track- routing challenges.

We milled the prototype using our Bantam Tools Desktop PCB Milling Machine, which we created as a single-sided board and manually created the three top-side links. If you’re having your PCB commercially produced, you won’t have to do this (and you’ll benefit from solder masking and overlay).

Check your PCB for broken tracks, corrosion, or other manufacturing defects. Give it a good clean with some isopropyl alcohol or circuit board cleaner, to help remove any grease or residue that may exist. This will help reduce the incidence of dry joints and other problems.

Use the lowest temperature possible for the solder you’re using, to help avoid overheating the PCB and components, which can be easily damaged with excessive heat.

As with all PCB builds, we follow a hardware, passives, semiconductors type construction pattern. This means you should start with the pin headers and IC sockets. Then move to the resistors and capacitors; take care with the orientation of the electrolytics. Next, install the diode and the voltage regulator. Install the crystal flush with the PCB.

Now is a great time to run some tests to check for any major issues before we install the ICs and microcontroller.

TESTING

Before connecting your LCD or any Integrated Circuits, we recommend performing the following tests.

Apply power to the DC input, 9-12V is suitable (9V recommended). Grab your multimeter and perform the following tests:

TEST 1: Pin 7 and 8 of the ATmega328P should show approximately +5 on your meter with ground on pins 20 and 22 respectively.

TEST 2: Check pin 16 and 8 for +5V and ground on one of the HCF4026B sockets as shown (you can check all three, but in all likelihood if one is correct the others will be too).

TEST 3: Check pin 7 and 14. of the 74HC00 as shown.

TEST 4: Check pin 1 and 8 for +5V and ground. of the HEF4050B as shown.

You should have 5V (or very close to it) in each of the examples. If you don’t, disconnect power and go over everything, looking for bridged connections or components installed incorrectly.

Now you’re ready to install the ICs. Pay close attention to the orientation to avoid errors, as you can damage them electrically without even noticing.

Close attention should be paid to static with these ICs also. Not everyone will have an antistatic workspace (antistatic mat, wrist-strap etc). However, you can take some simple steps to help protect static-sensitive hardware from damage when you’re handling them.

Almost everyone will have something on their workbench that is grounded. This could be a soldering iron, a computer tower (metal one), monitor, or some other earthed device. By simply touching your hands to the metal case, you should discharge any static from your hands to ground. Naturally, you need to do this sensibly. Don’t touch the hot part of your soldering iron, or you’ll have to take a trip to the emergency room instead. However, this process of manually grounding yourself before handling sensitive ICs is good practice to get used to. If you’re doing it regularly, however, an antistatic workmat and wrist-strap is a worthwhile investment.

THE CODE

The code is precisely the same as in the prototype. Indeed, if you have performed the prototype build successfully, you can simply use the ATmega328P that’s running in the UNO. If you haven’t, having a spare UNO as a programmer for an ATmega328P is a great way to program it. Naturally, you can use your favourite ATmega programmer to do it too.

Once the code is loaded onto the ATmega328P, you can remove it from the programmer / UNO and insert into your PCB observing correct orientation of the chip.

THE 3D PRINTED CASE AND FINISHING

We have created a case to provide handy desktop mounting for the finished unit (the frequency counter will have a matching case too).

The large LCD takes pride of place, as well as the rotary encoder and output RCA connectors. The DC connector is panel-mount, so could be moved to the front-panel if you like, but we decided to keep it on the back. We also added a small power switch which could be located front or back depending on your preference.

The case file is provided in the digital resources, frequency_generator.stl.

We printed the case in coloured PLA, but choose whatever colour and material you like. The print is quite straightforward. We have designed the case in such a way that it’s easy to mount all hardware, while maintaining a print design that’s easy to execute.

You’ll need a few M3 screws to fasten the PCB and screen to the case. Then mount up the rotary encoder, RCA sockets, and DC socket as appropriate. Take care with the polarity of the RCA terminals. The ground isn’t always critical, but good practice regardless. You may need to enlarge the holes for the RCA sockets and other hardware depending on the supplier.

Once all the hardware is mounted, you can proceed to wire up the screen. Follow the diagram shown here for wiring:

Next, wire up your Rotary Encoder. There are provisions to mount it directly to the PCB if you’re making your own case, however, for flexibility, we have panel-mounted ours and wired with hookup cable.

P4 and P3 are the squarewave outputs. Wire P4 to your TTL RCA output, and P3 to your CMOS output.

Connect your DC socket to P1 for power, taking care with polarity also. If you’ve decided to use a toggle switch for power, simply route the positive wire between P1 and the DC socket through the switch to use it.

Give everything a once-over and if you’re happy with your connections, apply power and you should instantly see your screen illuminate and present screen information.

USING THE FREQUENCY GENERATOR

At startup, a 10kHz signal is generated and that frequency is shown on the LCD display. Rotating the rotary encoder clockwise increases the frequency 1 step at a time to the maximum of 8MHz. Rotating the rotary encoder counter-clockwise reduces the frequency 1 step at a time to the minimum of 1Hz.

Note that the frequency generated is in a 1, 2, 5 pattern as in 1kHz, 2kHz, 5kHz. This is to provide the most useful frequencies and to keep within the bounds of what is possible with the frequency generation facility that we have. Odd frequencies of say 43.7kHz are not possible with this project.

Frequency Counter:

This project uses the crystal oscillator and internal counters/timers of the MCU to provide a fairly accurate and stable frequncy counter.

HOW IT WORKS

During development, we found that we could measure up to 4MHz without issue. Our Squarewave Generator produces various frequencies up to 8MHz. The counter couldn’t handle 8MHz and reported it as 6-7MHz. We’ve implemented a simple frequency divider that halves the incoming frequency so a frequency up to 8MHz can be handled. We haven’t attempted to provide higher counting facilities.

Like our companion project the Squarewave Generator, our Frequency Counter is locked to a crystal and is fairly accurate and stable. Although not a laboratory standard, nor is cost anywhere near laboratory standard either.

The frequency and period of the input signal is displayed on an LCD.

This project can handle both CMOS and TTL level input. Please note that the expected input levels are for 5 volt systems. While we have tested with 3.3 volt inputs, using various level converter solutions we found none that were suitable at the higher (MHz) frequency range. Stray capacitance acts like a filter and distorts the squarewave signal into something unusable. That’s not to say that frequencies in the 100’s of kHz may well be suitable for you to use.

An early prototype of the frequency generator.

HOW SQUAREWAVE FREQUENCIES ARE MEASURED AND DISPLAYED

This frequency counter is capable of measuring the frequency of squarewaves as well as pulse streams, rectangular waves or PWM signals.

Note: It works fine with rectangular/PWM waveforms and as a pulse stream is basically the same it should be fine also. There is no Schmitt trigger on input so sine waves even of large amplitude aren’t suitable.

The counter is auto-ranging in that it automatically optimises the display to show the frequency as Hz, kHz or MHz as necessary. It displays the period in a similar fashion with the period shown as ms, μs or ns as required. While you can easily work out these values, it’s little additional work to have them included on the display automatically.

Simply connect the input cable to your circuit under test. Use the appropriate type of connector to suit, either an Ezy-hook style device for IC pins or maybe a meter probe. Just be careful not to slip and potentially short circuit anything. The counter will show the frequency and period of the applied signal.

The Fundamental Build:

Frequency Counter Prototype

Parts Required: JaycarAltronicsCore Electronics
1 × Arduino UNO or Equivalent XC4410Z6240CE05629
1 × 74HC74 Dual D-Type Flip Flop ICXC4830Z8500014-9591745
1 × 4050 Buffer ICZC4050Z4050-
1 × 7805 5V RegulatorZV1505Z0505ADA2164
1 × 1N4004 DiodeZR1004Z0109COM-14884
1 × 3mm Red LEDZD0100Z0730POLOLU-1070
20 × 4 Line LCD DisplayQP5522Z7032SS104990002
1 × 220µF/25V Electrolytic CapacitorRE6158R5147CE05149
1 × 10µf/10V Electrolytic CapacitorRE6066R4767CE05274
1 × 100nF Ceramic CapacitorsRC5360R2865CE05188
2 × 330Ω 1/4W ResistorsRR0560R7546PRT-14490
1 × RCA Panel Mount Socket – RedPS0259P0152-
1 × RCA Plug – RedPP0240P0200A-
1 × Red IC Test ClipHM3036P9180AP9180
1 × Black IC Test ClipHM3037P9181AP9181

Parts Required:

We built the prototype on a large breadboard during development, which allows the theory to be tested easily. It’s also a great project for schools and educators who might like to explore squarewaves without expensive hardware.

BUILDING THE PROTOTYPE

Follow the fritzing diagram provided. The accompanying LCD schematic opposite should assist with any pin assignments.

You’ll need a 5V power source, which can be taken from the UNO. The overall build is not terribly power intensive, so you won’t overload the onboard regulator.

Unlike the frequency generator, this circuit is simpler so you can fit the LCD and components all on the one large breadboard. The pin headers on the LCD (you may have to solder them on yourself depending on where you get the LCD from) will securely connect to a small breadboard, so you can make the required connections to the UNO and the large breadboard.

There are a few points to take care with, most notably the ICs. We’ve tried to keep it fairly simple, with all ICs facing the same way. Most of the heavy lifting is done inside the chips themselves, so outside of power to each one, we only use a few links on each to get our desired connectivity. However pin assignment is critical, so take care to follow the diagrams carefully.

The frequency generator is auto-ranging to optimise the display, so there’s no additional interface required. If you had the desire to add manual range selection, you could do this with relative ease using a rotary encoder or switch. There’s plenty of spare GPIO on the UNO to accommodate it if you wish.

THE CODE

In the digital resources, you’ll find “frequency_counter.ino”. Load it onto your Arduino and let it boot. The sketch is quite straightforward and under 200 lines with about half of that devoted to updating the display. Working with this LCD is straightforward too, provided you don’t try to write more than 20 characters on 1 line. If this happens the extra characters appear at the start of another line, but not the next line. Note that the top line is line 0. The first position on any line is position 0.

The frequency counting side of the sketch is based around a library that controls the internal hardware counters of the 328P. As stated above, this library provides reliable operation to somewhere between 4MHz and 8MHz. We divide the incoming signal by 2 then multiply the result by 2 to allow counting to a suggested maximum of 8MHz.

The period of the incoming signal is also computed and shown on the LCD display. The frequency display is automatically scaled to be shown as Hz, kHz or MHz and the period as ms, μs or ns dependent on the input.

The Main Build:

Frequency Counter

Parts Required: JaycarAltronicsCore Electronics
Atmel 328p ICZZ8727Z5125000-ATMEGA328+UNO+CAPS
16MHz Crystalincluded with aboveV1289Aincluded with above
2 × 22pf Ceramic CapacitorsRC5316R2814included with above
1 × 74HC74 Dual D-Type Flip Flop ICXC4830Z8500014-9591745
1 × 4050 Buffer ICZC4050Z4050-
1 × 7805 5V RegulatorZV1505Z0505ADA2164
1 × 1N4004 DiodeZR1004Z0109COM-14884
1 × 28 Pin IC SocketPI6466P0541PRT-07942
1 × 16 Pin IC SocketPI6456P0534PRT-07938
1 × 14 Pin IC SocketPI6501P0560PRT-07939
1 × 3mm Red LEDZD0100Z0730POLOLU-1070
20 × 4 Line LCD DisplayQP5522Z7032SS104990002
1 × 220uF/25V Electrolytic CapacitorRE6158R5147CE05149
1 × 10uf/10 Volt Electrolytic CapacitorRE6066R4767CE05274
1 × 100nF Ceramic CapacitorsRC5360R2865CE05188
2 × 330Ω 1/4W ResistorsRR0560R7546PRT-14490
1 × RCA Panel Mount Socket – RedPS0259P0152-
1 × RCA Plug – RedPP0240P0200A-
1 × Red IC Test ClipHM3036P9180AP9180
1 × Black IC Test ClipHM3037P9181AP9181
1 × DC Input JackPS0522P0622ADA610
1 × SPST Toggle SwitchST0336S1010COM-09276
1 × 9VDC 1.7A PSUMP3484M8904CEO4425
Header StripsHM3212P5430POLOLU-965
Custom PCB (Files in Digital Resources)---

Parts Required:

The hardware required for this build is much the same as the prototype, however, there are additional components for interfacing, as well as the PCB and 3D printed case. There are also additional components for circuit protection, and items such as IC sockets which were not required on the prototype.

BUILDING THE PCB

The PCB is reasonably simple overall and only requires a single-sided PCB.

We milled the prototype using our Bantam Tools Desktop PCB Milling Machine, which did a great job. If you’re having your PCB commercially produced, you won’t have to do this (and you’ll benefit from solder masking and overlay).

Check your PCB for broken tracks, corrosion, or other manufacturing defects. Give it a good clean with some isopropyl alcohol or circuit board cleaner, to help remove any grease or residue that may exist. This will help reduce the incidence of dry joints and other problems.

Use the lowest temperature possible for the solder you’re using, to help avoid overheating the PCB and components, which can be easily damaged with excessive heat.

As with all PCB builds, we follow a hardware, passives, semiconductors type construction pattern. This means you should start with the pin headers and IC sockets. Then move to the resistors and capacitors; take care with the orientation of the electrolytics. Next install the LED, diode and the voltage regulator. Install the crystal flush with the PCB.

Now is a great time to run some tests to check for any major issues before we install the ICs or microcontroller.

TESTING

Before connecting your LCD or any Integrated Circuits, we recommend performing the following tests.

Apply power to the DC input, 9-12V is suitable (9V recommended). Grab your multimeter and perform the following tests:

TEST 1: Pin 7 and 8 plus 20 and 22 of the ATmega328P socket as shown, you should see approximately 5V on your meter.

TEST 2: Check pin 14 and 7 of the SN74HC74 as shown.

You should have 5V (or very close to it) in each of the above examples. If you don’t, disconnect power and go over everything, looking for bridged connections or components installed incorrectly.

Now you’re ready to install the ICs. Pay close attention to the orientation to avoid errors, as you can damage them electrically without even noticing.

Close attention should be paid to static with these ICs also. Not everyone will have an antistatic workspace (antistatic mat, wrist-strap etc). However, you can take some simple steps to help protect static-sensitive hardware from damage when you’re handling it.

Almost everyone will have something on their workbench that is grounded. This could be a soldering iron, a computer tower (metal one), monitor, or some other earthed device. By simply touching your hands to the metal case, you should discharge any static from your hands to ground. Naturally, you need to do this sensibly.

Don’t touch the hot part of your soldering iron, or you’ll have to take a trip to the emergency room instead. However, this process of manually grounding yourself before handling sensitive ICs is good practice to get used to. If you’re doing it regularly, however, an antistatic workmat and wrist-strap is a worthwhile investment too.

THE CODE

The code is precisely the same as in the prototype. Indeed, if you have performed the prototype build successfully, you can simply use the ATmega328P that’s running in the UNO. If you haven’t, having a spare UNO as a programmer for an ATmega328P is a great way to program it. Naturally, you can use your favourite ATmega programmer to do it too.

Once the code is loaded onto the ATmega328P, you can remove it from the programmer / UNO and insert into your PCB.

THE 3D PRINTED CASE AND FINISHING

We have created a case to provide handy desktop mounting for the finished unit (the Squarewave Generator will have a matching case too).

The large LCD takes pride of place, as well as the output RCA connectors. The DC connector is panel-mount, so could be moved to the front-panel if you like, but we decided to keep it on the back. We also added a small power switch which you could be located front or back depending on your preference.

The case file is provided in the digital resources, frequency_counter.stl.

We printed the case in coloured PLA, but choose whatever colour and material you like. The print is quite straightforward. We have designed the case in such a way that it’s easy to mount all hardware, while maintaining a print design that’s easy to execute.

You’ll need a few M3 screws to fasten the PCB and screen to the case. Then mount up the RCA socket, and DC socket as appropriate. Take care with the polarity of the RCA terminals. You may need to enlarge the holes for the RCA and DC socket depending on the supplier.

Once all the hardware is mounted, you can proceed to wire up the screen. Follow the diagram on page 81 for LCD wiring.

P3 is your signal input header, which should be wired to your RCA socket (or terminals if you prefer).

Connect your DC socket to P1 for power, taking care with polarity also. If you’ve decided to use a toggle switch for power, simply route the positive wire between P1 and the DC socket through the switch to use it. If you have a double-pole switch you can route the ground wire also, but it’s not essential.

Give everything a once-over and if you’re happy with your connections, apply power and you should instantly see your screen illuminate and present information.

USING THE FREQUENCY COUNTER

Our Frequency Counter project is quite straightforward to use. If no input signal is provided the LCD display will after a few seconds, display 'No input signal'. With, say a 1010Hz signal provided, it will determine that the signal is in the kHz range and display it as 1.010kHz.

If the applied signals frequencies wobbles a little (say 999Hz then 1000Hz then 1001HZ) near a boundary to Hz to kHz and kHz to MHz, the displayed frequency will alternate similarly between for example 999Hz, 1.0000kHz and 1.0001kHz.

During testing we found that various input sources had different levels of stability of their frequencies. The period is computed from the input frequency and will follow a similar pattern as the frequency display. That is it may alternate between ms to μs or μs to ns.

WHERE TO FROM HERE?

The Frequency Counter project could be expanded to handle higher input frequencies by changing the input frequency divider to a decade counter or higher, however, you’ll need a method to calibrate or confirm operations. This project is built around squarewaves / digital signals and therefore is not designed to handle sine or other waveforms.

Perhaps an adaption would allow greater versatility of waveforms and further increase functionality and versatility!