This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Measure High Time of a Varying Frequency

Hello everyone,

I would like to ask to who has more experience with this.

I have a custom sensor that outputs data by a varying frequency. Now i don't just need to measure the frequency. What i need is actually the High time, but of course, if the frequency changes, the high time also does.

The duty percentage should stay the same, it should be linear. Still i would like to measure the High time directly.

What i have currently is a rising edge interrupt on a digital pin, start a timer, wait for a falling edge interrupt, register the timer value.
Okay, but now i would like to use edge-time capture mode of the timer. The issue i am seeing is that i would need to have the timer work for both edges. Interrupt on rising edge, register value ,wait for falling edge, register value. I see issues because of overflows since the frequency is not fixed.

I could use a digital interrupt on rising edge to start the timer and a edge-time interrupt on falling edge in which then i reset/stop the timer, assuming i use a Load value high enough. This would be more precise than the first method. Still i would like it to be more precise, but it doesn't seem viable with just 1 timer, am i correct?


  • There do exist "tricks" which hapless users of far earlier MCUs (pre ARM) were forced to use due to limitation of Timer numbers.  But ARM MCUs "overflow" w/Timers.  You do not explain why you wish to, "swim upstream!"  (i.e. use only a single timer)  Is not your task far simplified/speeded via the use of several timers?

    Cannot your timer overflow be prevented (or at least greatly reduced) through the use of a wide timer?  Unless your frequency of interest is extremely low - this appears most viable.

    Lastly - you give no clue as to the expected range of frequencies to be measured.  That's helpful - don't you agree?

    Might you identify your sensor - armed w/that (and a data link) someone may "rise" to your single timer bait...

  • I was considering the option of using multiple of this sensors so i was trying to see if my thoughts were correct in the fact that to increase the accuracy i would need more than 1 timer.  I needed something like 5 sensors working at the same time


    The frequency range depends on how i configure the sensor.
    It uses a 555 to convert current from a photodiode to frequency.
    When i still used programming like Arduino i had this in a configuration that would have a High time that could be as low as  20mS or more! This would mean a terribly slow data rate.
    Now of course i know more so i intend to increase the frequency of the 555 by changing the discharge resistor while keeping the photodiode working linearly.

    Still the lowest frequency can be considerably lower than the highest frequency, but i am hoping to get the lowest, practical, value of high time to be around 200uS. Of course i will adjust the speed to the MCU capabilities 

  • Luis Afonso said:
    ...using multiple of these sensors ... 

    Luis - this is a key point - and one which was absent your initial writing.  And - serves to (almost) justify why readers should, "want to assist you!" 

    Sales are critically important - even here!  So many "help me" posts arrive - and there's so little time/energy - which posts will "draw in" best qualified to assist?  That's Sales - is it not?  And that's why "justification/explanation - SELLING of your need" is critical.  (and too rarely employed)  (awareness of these tactics took us, "public")

    555 is (unlikely) to reside w/in modern, commercial sensor.  Have you checked the sensitivity of such to temperature, voltage supply, and simple aging?  Commercial "light sensors" exist - several from this vendor - and should provide much superior performance.

    And lastly - I doubt you'd require "all sensors to report/be interrogated - at exactly the same moment."  This points to your potential Timer-Saving/Reduction via simple "multiplexing" of the sensor signals - one or two at a time - into the proper sensor - for processing by a reduced number of MCU Timers.  Might that work?

  • By "multiple" i meant that the full sensor would be composed by multiple of those photodiode+555.

    As for sales, it will be something really small. I never seen a sensor this precise and simple in any of the competitions i seen here in Portugal.

    I know 555 is well... not the best, i intend to get a better IC for the purpose but until i find it, i need the sensor working with the 555.

    I only need to get 5 sensor data in a maximum time of 1ms, doesn't matter if at the same time but i do have to think of every possibility.

    Thanks, i really just need a bit of insight on the use of one or multiple timers.

  • As always - wish you well.  I'd bet that your review of several (similar) commercial sensors will prove useful.

    And by "Sales" (in past post) I meant the "selling" of your post here - to make it more interesting & attractive thus valuable - to potential, experienced responders.  Too often posters begin with, "I wanna..." - unexplained is their need, the advantage they seek - or (any) attempt to "generalize" their request - so that their post becomes more engaging & appealing.  That's selling - is it not? 

    When small firms (such as ours) go against "giants" we have to employ, "every trick in the book" to gain notice - "pull potential clients to our brand" - and "compel" them to "take action!" 

    The subject/title of this thread may be strengthened with, "Conserving Timer usage so that multiple, "timed sensors" may be operated - even beyond the MCU's Timer capacity..."  That title's got a hook - does it not?  Posters may be better drawn in by it - as they anticipate a clear personal benefit - not so evident in the original thread subject/title. Title I've just "on the fly" crafted is too long - but does convey my idea...

    Self interest is a very powerful force - writing so that your request becomes appealing to and beneficial to MANY (not just to you) can be proven to generate, "faster & richer" responses...