Pocket Frequency Meter

Counting for one second

Count the rising edges of a signal for exactly one second, and the count is its frequency in hertz. This is the classic direct-count method, and the Pocket Frequency Meter is a small instrument built around it.

I built it in 2025 as coursework, and it covers every layer of a small embedded instrument: firmware in C51 on an 8051-compatible STC89C52RC microcontroller, a schematic drawn in functional blocks, a two-sided board and an enclosure. It is kept as a finished record under the MIT license in its repository. The part I went furthest into is the constant that sets the measurement gate, and why matching it to the crystal on the board was the better fix.

The chip's two hardware timers divide the work. Timer 0 counts edges arriving on its input pin, entirely in hardware, and every time it overflows an interrupt extends the count in software, so the count never runs out before the second ends. Timer 2 reloads itself automatically and interrupts at a steady tick. When a full second's worth of ticks has passed, its handler closes the gate, reads the count, converts it to a frequency and opens the gate again.

The main loop does nothing but refresh the display. It lights the digits one at a time and blanks the segment lines between them, so one digit's pattern never shows faintly on the next.

One owner for each job

The structure of the instrument fits in one sentence, and I built it so that it would: a timebase, a counter, a readout and a supply, each owned by exactly one piece of hardware and one piece of code. Timer 2 and its interrupt own the definition of a second, and nothing else touches it. Timer 0 and its overflow count own the measurement. The main loop owns the display. A USB-C input behind a slide switch, a reset network and headers that bring every pin within reach of a probe make up the supply and the way in.

This division is what makes it possible to argue about accuracy at all. The gate lives entirely in an interrupt and the display entirely in the main loop, so no amount of work in the readout can lengthen or shorten a measurement. The counter is hardware, so interrupt latency cannot lose an edge. Every error that remains belongs to the method or to a component, never to the code, and that is the state a small instrument should be in before anyone quotes a number from it.

A second that ran long

The firmware's tick came from a reload value that is correct only for the round-numbered crystal common on 8051 boards. The schematic, however, uses a crystal chosen for exact serial baud rates, very likely inherited from a general-purpose board layout. At that clock each tick ran long, so the gate lasted longer than a second and every reading came out high by the same fraction. The error was systematic, far larger than every other error on the instrument combined, and invisible from inside the firmware, because a reading that is too high by several percent looks entirely plausible on the display.

The fix was one line: keep the crystal and change the reload value to match it. This left the design better than the original intent would have. The baud-rate crystal divides into the tick exactly, with no rounding at all, while the round-numbered crystal hit the tick only because its numbers happened to come out even. Swapping the crystal would also have worked, but it costs a part and gives up the board's exact baud rates.

Where the readings hold

With the timebase corrected, the errors that remain come from the method and the readout, and each one can be traced to a cause.

The largest at low frequencies is the one-count uncertainty. The gate and the input are not synchronized, so the count can land on either side of the true value. At high frequencies that single count is a tiny fraction of the reading; at low frequencies it is a large one, and this is what sets the bottom of a direct counter's useful range.

A smaller error sits at the gate boundary. Edges that arrive in the brief moment between stopping the counter and starting it again fall outside the gate, so the reading is slightly low, by an amount that grows with frequency. The crystal's own tolerance stays below the one-count floor across the instrument's range, and it would become the dominant error only in a reciprocal-counting design. The counter itself never runs out of range; the top of the range is simply the largest number the display can show. Finally, the counter input is a digital pin with a fixed threshold, so the instrument is specified for a logic-level square wave, and any other signal has to be shaped before it arrives.

Taken together, these terms make the readings trustworthy for a clean logic-level square wave across the instrument's working range, from the point where the one-count uncertainty has become small up to the limit of the display.

Drawn in blocks

I ordered the work so that each decision was made against something already fixed, not against an intention. The measurement method came first, because it decides everything downstream: direct counting fixes a one-second gate, the gate fixes what the timers must do, and the timers fix which pins are taken.

Only then did I draw the schematic, as separate blocks rather than one large net: power input, reset, crystal, key input, header breakout and the microcontroller. Each block can be checked as a unit, the reset network, the load capacitors on either side of the crystal and the pull-up network on the open-drain port among them, so a question about the assembled board goes to one block rather than the whole drawing. Simulation came next, against the schematic rather than the board, so that a wrong connection was caught while fixing it still cost only a wire.

The schematic on one sheet, divided into six labeled blocks: power input, header interface, reset, crystal oscillator, key input, and the main control circuit around an STC89C52RC in DIP-40
The schematic on one sheet, divided into functional blocks that can each be checked on its own.

Two boards from one drawing

The layout follows the blocks. The microcontroller sits in a socket in the middle of the board with the port headers along its long edges, so every pin can be probed without lifting the board. Power enters at the left, where the USB-C connector, the slide switch and the reset button sit together. The crystal sits right against the oscillator pins on the right, because that is the one net on the board where trace length decides whether the circuit runs at all.

Most signals that have to reach a header run on the top layer, and the supply and the return paths drop to the bottom. That split keeps the header rows straight instead of bent around one another.

The top copper layer of the board, 95.0 by 60.0 millimeters, with the DIP-40 footprint in the middle and header rows above and below it The bottom copper layer of the same board, routed in blue, with the resistor values 10K and 1K called out beside their footprints
The instrument board's top layer (left) and bottom layer (right), with the signals above and the supply below so that the port headers run straight to the edges.
A rendering of the assembled board: blue substrate, USB-C connector, slide switch, three tactile buttons, the DIP-40 socket, a resistor network, a crystal and a power LED
The instrument board as it would be assembled.

The same schematic went to a board twice. The first layout is a general-purpose STC89C52 minimum system, with mounting holes in the corners and spare indicators, meant to carry any 8051 project. The instrument board is the second, slightly larger to make room for an extra switch and a wider header field. Everything else carried over unchanged, the supply corner, the reset and the crystal against its pins, because the circuit underneath is the same. Keeping two layouts of one drawing, rather than two drawings, means a change to the circuit is made once.

A rendering of the general-purpose revision: a blue board with mounting holes at the corners, a USB-C connector, a slide switch, three tactile buttons, the DIP-40 socket, two indicator LEDs and a crystal The general-purpose revision as a board layout, 90.0 by 50.2 millimeters, with mounting holes at the four corners and the port headers along the long edges
The earlier general-purpose board, rendered as assembled (left) and as a layout (right), built from the same schematic to carry any 8051 project.

The enclosure came last. I modeled it after the board outline was final, so the display opening, the wall thickness and the clearance over the tallest part all come from the layout instead of being measured off a built board. With the box, the project stops being a bare circuit board.

A CAD view of the enclosure lid with the display cut-out dimensioned, 30.30 millimeters wide A CAD model of the enclosure: an open rectangular shell with a lip around the opening
The enclosure, modeled around the finished board outline, with the cut-out the display shows through (left) and the shell the board sits in (right).

On the finished board, the signal to be measured comes in through the header breakout to the counter input, and the firmware is what turns the board into an instrument.

Resources