Page 2 of 3

Re: Freq meter err (FC11,Nano)

Posted: Tue May 19, 2026 9:36 am
by mnfisher
Might be pushing it...

There are alternative techniques. One is a custom interrupt handler (to reduce the ISR overhead) Faster still - move the input to D5 and use it as input for TMR1... Both need some C.

Signal integrity needs to be good too - and not degrade due to capacitance etc

Martin

Re: Freq meter err (FC11,Nano)

Posted: Tue May 19, 2026 11:04 am
by mnfisher
... and using TMR1 with an input as its pulse.

This requires D5 as the signal to be measured and D3 with a 1s pulse - I used a AWG for both signals

The theoretical maximum frequency it can measure is 6.4MHz - at 5MHz I got 4999986Hz :-)

It is pretty much all in C - I used a custom interrupt for the timer1 overflow - to reduce the overhead (overflows is defined in supplementary code) - needed to create a dummy macro to keep FC happy (dummy - is never called)

The 1s (d3) interrupt uses the standard FC setup - but most of the code is in C (to retrieve the current value of TCNT1 and the number of overflows)

Now the pulse counter is hardware based - so the MCU isn't overloaded. This means we can use delays again.

An average value would be good - again I've kept the code as short as possible.

Martin

Re: Freq meter err (FC11,Nano)

Posted: Tue May 19, 2026 11:07 am
by mnfisher
Tested at 6.4MHz and pretty good (6399981Hz) and at 1MHz - 999997. A few 'ticks' are missed whilst handling the 1Hz / overflow interrupts?

Using an external 1Hz pulse means that the results are independent of the Arduino's clock - so should be reliable - depending on the accuracy of the timer instead.

I used a square wave for the 1Hz signal and initially a sine for the frequency to be measured (also tested with square for this)

Re: Freq meter err (FC11,Nano)

Posted: Tue May 19, 2026 11:42 pm
by viktor_aust
Hi Martin
I like the idea of external 1Hz pulse.
Looks like this is the key to a good freq meter with Arduino.
Will test it with Mega shortly.
Thanks.

Re: Freq meter err (FC11,Nano)

Posted: Wed May 20, 2026 6:22 am
by mnfisher
Okay, let us know how you get on...

It looks like there will be a few changes - you'll need to change the pin from d5 to one that is a timer breakout (you might need to change to timer 5 and D47?) and d3 is int5 rather than int1

Will require some changes to the setup and interrupt handler for the 1hz pulse.

Martin

Re: Freq meter err (FC11,Nano)

Posted: Wed May 20, 2026 9:26 pm
by viktor_aust
Sure. Will do.

Re: Freq meter err (FC11,Nano)

Posted: Thu May 21, 2026 2:02 am
by chipfryer27
Hi

In any counter application the problem is in the overheads, i.e. the time the code takes to actual process. Using an external timer as suggested above would free up the processor to focus on capture.

I've no access to hardware just now (still upside down) but I always planned to test the following.

External timer set to a suitable period in seconds. I would check against a scope / analyser and adjust until I got it as close as I possibly could to target time. If say it was one second then no averaging would be needed, but with five seconds then that would be my averaging value to divide by.

Microcontroller has interrupt already running on Freq-In pin set to trigger on rising edge.
Interrupt simply increments a counter when called (e.g. counter = counter +1)

Microcontroller sits waiting on the timer pulse to go High (for example).
Microcontroller clears counter
Microcontroller waits for timer pulse to go Low
Microcontroller assigns counter value (e.g. value = counter)
Divide value by how many seconds timer runs for to give value in Hz.
Back to beginning.

As I mentioned I'd always wanted to test the above to see what I got when connected to a known frequency source, perhaps checked against scope / analyser. May even be possible to slightly adjust parameters to factor in the time it takes to process at a known frequency.

Speed the microcontroller is running at would obviously affect accuracy.

About a year ago Martin and I attempted to measure how fast a uC could measure x-amount of ADC readings vs it using and external ADC. Quite fun.

Regards

Re: Freq meter err (FC11,Nano)

Posted: Thu May 21, 2026 2:30 am
by mnfisher
The version here has 'no' overheads - and can measure up to 6.4MHz(accurately - i think this is the max!) which is remarkable for a 16MHz processor!

The HS signal is acting as the pulse for TMR1 (decreases a 16 bit counter TCNT1) instead of the MCU clock (/some prescale)

This is done in hardware not code - the MCU runs normally and the only code is executed when timer1 overflows (every 65536 pulses) and once every second to calculate the frequency.

The Mega doesn't break out a pin for timer1 hence the need to change to timer5 - though i didn't look at the datasheet for long (original question was for a Nano) and I'd started wondering if i could use an ATTiny :) and if i could measure PWM frequency and duty cycle...

Crikey - was it a year ago! It was fun! Time flies because of it....

Eek it is 2:30 am - really need to sleep before work :-(

Re: Freq meter err (FC11,Nano)

Posted: Thu May 21, 2026 5:21 am
by chipfryer27
Hi

I've no access to FC either at present so can't look at any charts (well not during the day)

Sleep..... Only in my dreams....

Typically waking around 2am and irrespective of when I go to sleep I'm still up at 2am.... That's about 4pm your time the day before. At best snatching a few hours here and there. However being an insomiac is nothing to lose sleep over as I will get plenty when I die <s>

I saw above it was HW orientated, just mentioning what I always wanted to try for the fun of it <s>

Interested to hear how Viktor gets on.

Regards

Re: Freq meter err (FC11,Nano)

Posted: Thu May 21, 2026 6:20 am
by mnfisher
Wish lottery numbers would you like to know? Or if you're ahead? Euro millions would do nicely 💷💶🥳

As the mega has multiple timers it could perhaps go back to generating the bogosecs (nearly 1Hz) interrupt too?