Reciprocal Frequency Counter with Raspberry Pi Pico - Page 1
Forum section: electronics / projects

author: ingbird64
author_rank: Newbie
timestamp: March 18, 2022, 08:24:45 pm
content: Hello!I would like to share my current project with you. It's a frequency counter using the Pi Pico and a few additional components.You will find the documentation and the source code at github:https://github.com/ingbird64/Pi-Pico_FrequencyCounter.The resolution is 7 digits at one second gate time and 8 digits at 10s gate time. I use two counter blocks, so measurements are without a gap. Output is a 2x16 LCD-display with I2C-Bus, the measurement values are also available at the serial output over USB. The gate time can be changed by a push button (0.25s, 1s and 10s).Another push button starts the calibration. To do this, a precise 1Hz-signal must be present at the input.I build the circuit on a breadbord and tested the resolution and accuracy with a 1pps-signal from a gps-receiver. With a TCXO from IQD (CFPT-126) the error is ±1 count in the least significant digit.My ideas for improvement:- using a nextion display with touchscreen- adding some statistics (mean, pk-pk, standard deviation)- increasing resolution by adding a TDC7200 for precision measuring of the gate time

author: Benta
author_rank: Super Contributor
timestamp: March 18, 2022, 09:10:57 pm
content: Nice little project.A comments:for test and measurement, it pays off using the fastest front-end logic possible. I recommend using 74AC- types instead.

author: dmendesf
author_rank: Frequent Contributor
timestamp: March 19, 2022, 02:34:54 pm
content: Very nice project indeed. Keep it up!

author: ingbird64
author_rank: Newbie
timestamp: March 19, 2022, 04:25:45 pm
content: Hello Benta,you are right. The maximal external clock for the pwm-slices of the Pi Pico is half of the Pi Pico clock, so it should be possible to measure up to 65 MHz with this circuit. For that, the 74HC-types are too slow (only up to 15 MHZ). I used this type, because I had it in stock.I have changed the schematic, so the circuit will get the full performance.Thanks for the hint.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 19, 2022, 05:30:04 pm
content: How is the extra flip-flop helping the reciprocal counting ?Many µCs can do reciprocal counting with no extra hardware, just the µC timer hardware. An external divider may be used to extend the frequency range.It is a good idea to have some kind of schmidt trigger of similar at the input. Not all flip-flops (e.g. most of the 74HC sereis) are happy with a slowly changing input voltage.

author: ingbird64
author_rank: Newbie
timestamp: March 19, 2022, 06:27:08 pm
content: The extra flip-flops are used to synchronise the gates for the counter with the leading edges of the counting-clocks (see attached timing-diagram). I chose this structure because the circuit should be easy to understand.There is no inputstage yet, I am testing the circuit with digital signals.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 19, 2022, 07:48:50 pm
content: I don't see proper synchronization: there is still a chance to have the first flip flop change just at the wrong time to violate the set-up time of the 2nd flip-flop and get a poorly defined response for the 2nd flip-flop. It is not likely but it can happen.Usually the µC inputs already include synchronization (usually 2 stage and thus less suceptible to borderline states) and most µC timers can do the time measurement with the internal clock with no external HW. AFAIK the µC has a clock faster than 10 MHz and could thus provide more resolution.Things get a lot easier without an extra external clock domain. The µC internal counters can usually do more than just count pulses - they are actually not even very good in counting external pulses.

author: Benta
author_rank: Super Contributor
timestamp: March 19, 2022, 08:52:32 pm
content: Have to agree with Kleinstein.And gating clock and FF outputs is bad practice anyway.

author: Renate
author_rank: Super Contributor
timestamp: March 20, 2022, 11:37:59 am
content: I don't like unrelated clocks. They make my head hurt.Most? Some? µC timers restrict external clocks to a fraction of the internal clock frequency when they synchronize them coming in, I think? Also, peripheral clocks are usually much lower than processor clocks. I've got a Teensy 4.0 and that can only measure 6 MHz or so.I was thinking how to do this in an overkill sort of way. I have a CMOD A7 FPGA that I haven't done much with (and that like everything else in 2022 is out of stock everywhere.)Here's my idea: 32 bit counter running flat out on external clock. 32 bit binary to gray. 32 bit latch running off GPS PPS. Optional gray/binary conversion. VGA/OLED/LCD/7seg/UART/I²C/SPI/whatever as output.(I've already got VGA running on the CMOD.)The good part is that binary to gray is fast, gray to binary is slow(er), but can still be done in log2(width).Undoubtedly this is an old idea.Ok, this isn't reciprocal. But it could be.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 20, 2022, 12:53:48 pm
content: I had a quick look at the RP2040 datasheet: it looks like the timers are rather basic and don't have HW support for direct time measurements. In this case the way with an external FF and gates may actually be needed. However the RP2040 also just seems a rather poor choice to build a recirpcal counter.Many other other µCs (e.g. most AVRs, PCI16, PIC18, STM32L..., STM32F...) have a input capture or similar called timer function to do time measurements: From an external signal the current counter value is copied to a special register and can this may be used to measure the time of external events with a resolution of up to the system clock. With this HW support a reciprocal counter is relatively easy to build. External hardware would be more like a prescaler to reduce the frequency if needed, so that the µC is fast enough to handle all the events.

author: rmozel
author_rank: Newbie
timestamp: March 21, 2022, 11:47:46 am
content: It seems that Programmable IO functionality of RP2040 might be beneficial for this application.

author: jdev99
author_rank: Regular Contributor
timestamp: June 30, 2025, 11:50:04 am
content: You must try this method (using ASM code and PIO) to see if it makes it even better:https://forums.raspberrypi.com/viewtopic.php?t=306250

author: PCB.Wiz
author_rank: Super Contributor
timestamp: June 30, 2025, 08:23:40 pm
content: Quote from: jdev99 on June 30, 2025, 11:50:04 amSince this thread started and since that forum post was done, the RP2350 has been released.The RP2350 has more MHz and more PIO resource, so can improve the specs.A great reciprocal counter resource is here:http://mino-elektronik.de/fmeter/fm_software.htmOne example uses time-to-digital expansion, those TDC parts are dropping in price and getting easier to use, tho not yet 'both features at the same time'?

author: grx
author_rank: Newbie
timestamp: October 03, 2025, 08:11:02 am
content: I was able to make a reciprocal counter with a pico with no additional hardware except signal conditioning. It has 8 counting channels (one per hardawre pwm line).Using just software one can count pulses on each line without dead times, and deduce frequencies relative to one input count.