2 Replies Latest reply on Sep 1, 2017 9:09 AM by WaMa_286156

    Accurate Frequency measurement


      I'm trying to read a sensor that outputs frequency (pulses).  I use the method described in this document:




      It can read the output frequency from the sensor, but it seems to be about 10% less than what I'm observing from my scope.  Is there any way to improve the accuracy from this method?

        • 1. Re: Accurate Frequency measurement

          If you program a microcontroller to count the number of pulses in a second, and it's wrong.  It's either counting the number of pulses incorrectly, or it isn't accurately estimating the passing of a second.  I'm willing to bet it's the later. 


          To measure time accurately, your microcontroller's clock source either needs to be accurate (crystal source) or needs to be compensated (how many clock cycles are actually in a second VS rated approximation of microcontroler's internal clock).

          • 2. Re: Accurate Frequency measurement

            If your pulse width is proportional to the frequency, you can use something similar to this:




            The timer starts on rising edge and captures on falling edge.   the Capture interrupt has the results.  You have a 1 microsecond accuracy.


            If you need to capture from rising pulse to rising pulse, then put a flip flop in there that sends Q NOT into D, and the rising edge will send Q high.  Send that into counter enable.  The next rising edge will send Q low, disabling the counter.  You can  capture the count using Q Not into Capture (capture on rising edge),  interrupt on capture, then read the capture register.  I found out through experimentation that capture works on a counter with enable low, which is why Q NOT can give you the counter capture.


            Be sure and put this in, then build, then put any USB onto the schematics after a successful build and run, else you can end up with routing issues, with sporadic capture interrupts. (Based on experience with with 6 or more identical counters in the schematic page with USB placed on the page first.)