Hi Frequency & ratio capture

The code suggested by johnwasser in the post Square wave generator and frequency meter - #3 by johnwasser works well only for low frequencies. I would like to have something like that to capture frequencies & duty cicle from 1Hz to 1MHz. Any suggestion ? Thanks

What Arduino are you using, and at what inpuy frequency does the code fail?

I am using the Mega 2560. The precision is getting worse as frequency goes up. It is acceptable only until 100Hz ...

How are you measuring the frequency?

How you measuring it?

Exactly as suggested in Square wave generator and frequency meter - #3 by johnwasser but since the Mega pins are different compared to Uno, I change the Timer1 to Timer5. The reading is done in Mega pin D48 (icp5)

Then you are doing something wrong, as the code includes a test function that gets the correct answer for a 975 Hz signal.

The code will work only on AVR based Arduinos, and only with clean, 5V rectangular input pulses. Given the 16 MHz CPU clock, you probably can't expect it to work at much higher than typical audio frequencies.

Something is clearly wrong with either your code or your input signal. The Input Capture example sketch works fine at 975.56 Hz and should get to at least 100 kHz (1000 times higher than your limit). Getting to 1 MHz will be hard because that only allows 8 instruction cycles between interrupts.

What is the source of your input signals?

Get a Teensy LC (or Teensy 3.x) and use the appropriate PJRC Frequency Library.