Page 1 of 2
Freq meter err (FC11,Nano)
Posted: Fri May 15, 2026 12:41 am
by viktor_aust
Hi
I am traying to figure out what is the best MC/board for the freq. meter project.
Internet does not suggest to use the Ard (Nano) because of the ceramic oscillator and possible problems.
And I did not use the counting of pulses in one second method. Some say, it is not accurate.
And they say - not to use the >100 kHz project with Nano freq. meter.
Short about the (FC11) the Ard Nano freq meter project:
PWM (component-Timer0) produces the pulses.
The pwm pulses go to the D3.
The triggered by D3 interrupt starts /stops the Timer component (Timer1) and Nano measures the time duration (uS) between the raising edge1 and raising edge2.
The Timer component works, but the measured frequency is wrong.
Is it the software timing problems, ceramic oscillator problem or something else?
Example
PWM (component setup) = 1068 Hz
Scope shows the pwm freq. as 3906 Hz
Ard measures = 2994 Hz
The net suggests to correct the frequency by applying a precise signal (e.g., 1 kHz or 1 MHz) and calculate:
factor = actual freq / measured freq.
I am thinking about the STM32F103 series to use or to use the Mega2560 (I think it has the crystal oscillator).
There are some freq. meters (modules) on Ali.
Is it better to buy one and forget Arduino?
Any suggestions?
Thanks.
Viktor
Re: Freq meter err (FC11,Nano)
Posted: Fri May 15, 2026 5:23 am
by mnfisher
You can get reasonable accuracy - but it depends what you want. You'll need an accurate clock and there are limits to how fast you can measure.
You can use the capture compare module of the MCU - there is a long discussion here
viewtopic.php?p=18024#p18024 for a PIC that looks at the workings
I'm sure i posted a version for AVR - see
viewtopic.php?t=2989&hilit=Frequency+measure
What code are you using? The interrupt handler needs to be as quick as possible - timing pulses over a fixed interval should work too.
Martin
Re: Freq meter err (FC11,Nano)
Posted: Fri May 15, 2026 7:42 am
by viktor_aust
Thanks Martin.
Looks like I have to use c-code.
I thought I can use the Timer component.
It does not work, does it?
Re: Freq meter err (FC11,Nano)
Posted: Fri May 15, 2026 7:53 am
by mnfisher
The capture compare would use C. What code do you have - you can use a timed interval and isr?
On phone at present - but
Isr (pin change rising)
Count = count + 1
Every second (this could be an interrupt - i think you can get a timer interrupt 0.96s (i call them bogosecs

)
Frequency= count (adjust for bogosecs if using)
Count= 0
Loop
Display frequency if it's changed - note at high frequency disable interrupts whilst doing this - or it will be a crawl
Martin
Re: Freq meter err (FC11,Nano)
Posted: Fri May 15, 2026 11:27 pm
by mnfisher
I tried the above idea on hardware (although I used a Uno rather than a Nano) - and it performs pretty well...
With a signal at 160kHz - it gives a frequency count of ~155900. The error might be caused by several issues - pin change interrupts occurring whilst the timer interrupt (or output) are missed. The clock (or my calculation) might be slightly out (*). The signal generator might be slightly off (though less likely

) (*) - I think that my maths for the interrupt occurring at 0.954Hz is right - but it's late. It would be easier to use 1s interrupt
One issue is that you can't use delays (or at least they are not accurate) - I counter this by using a timer interrupt (a timer component with a 1s pulse might be better - or (and I've used this in the past) at square wave from a RTC at 1Hz?) - and counting (here 5 x 1.02s intervals). The MCU gets overwhelmed - if the timer is for 5000ms - and it is getting interrupted 5 x 160000times it will take longer than 5s.
160kHz is about it's limit - a faster MCU (ARM, esp32 etc) could do better - interrupt latency etc also play a roll - but it depends on what frequencies you need to measure...
Martin
Re: Freq meter err (FC11,Nano)
Posted: Sat May 16, 2026 8:51 am
by viktor_aust
Hi Martin
I tried the c-code project today.
The time duration is steady, however, I failed to get the correct frequency calculations.
Some recommend to use Toff-Ton, etc.
Not sure...
The one second method.
Correct me if I am wrong.
-----------------------------
Timer1 counts the one second interval (0.954s to be presize) constantly.
Timer0 waits for the change on pin D2.
The macro (pinChange) counts the number of the 'up' pulse edges.
After one second is gone:
Related to Timer1 interrupt (macro) does the calculation:
freq = count * 954 / 1000
count = 0
time = time + 1
---------
When time = 5 seconds, user can see the printed result.
--------
Question
If you update the freq result every five seconds,
would be a good idea to create an average result of the sum of the calculated every second freq values?
Example
If time = 1 (n)
freq_sum = freq_sum + freq
If time = 5
freq_new = freq_sum / 5
Will try your method tomorrow.
Thanks
Re: Freq meter err (FC11,Nano)
Posted: Sat May 16, 2026 1:54 pm
by mnfisher
Hi Viktor,
Timer0 isn't used - just TMR1 for the time interval and a pin change (I used d2). Yes - it would be better to use an average - i used the average component but removed to just leave the barebones. I also tried adding a count and just increasing count after 100 rising edges but it didn't give much benefit - it meant i could use a 16 bit count instead of 32...
Using timer on/off is also possible - start, count pulses (1000??) stop timer.
It depends a bit on what frequency you are trying to measure - and how accurate you need and possibly how often the frequency changes.
I think I'd probably go with maintaining a 'frequency' variable in the timing interrupt and let the code utilise it as needed.
Martin
Re: Freq meter err (FC11,Nano)
Posted: Sun May 17, 2026 1:32 am
by viktor_aust
Hi Martin
Thank you for the correction.
Timer0 is not used. The interrupt waits for the change on pin D2.
Your code works.
In my case:
The Nano PWM generates (on D6) 31.25kHz
The average of 5 readings = 31355 Hz
Appreciate your help Martin
Thanks
Re: Freq meter err (FC11,Nano)
Posted: Mon May 18, 2026 4:26 pm
by mnfisher
Hi Viktor,
That sounds reasonable - glad you got it working...
As mentioned, using the 1s pulse from a ds3231 (or other RTC) makes the clock speed irrelevant - if you need more accuracy?
Martin
Re: Freq meter err (FC11,Nano)
Posted: Mon May 18, 2026 10:33 pm
by viktor_aust
I need to measure the freq fr 40kHz to 200kHz.
Thanks for suggesting to use the RTC 1s reference signal.
Will try it.
Thanks again.