Main Site Documentation

How to measure frequency on Digital In?


Lets say I feed digital pin with low voltage under 5V but want to measure frequency on that wire. Is it possible to code something for during that?


It depends on what frequency and how accurate your measurement must be. To start with lets define the relationship between frequency and the time of one period:

T = 1/f or conversely f = 1/T

So if you know the time of one period of the wave form, that is the time from one low->high transition to the next low->high transition (high->low to hi->low it does not matter) then you know the frequency. You could feed the signal to an interrupt pin set to trigger on hi->low edges and then calculate the time between pairs of interrupts.

You could also use a frequency to voltage converter chip and measure the voltage with the analog input.


Look at the PinCapture class in the latest SDK.

Note, the signal must be digital. Analog signals will not work even they are less than 5 volts.


I need to measure the frequency a car alternator generates depending on engine’s RPMs. Final goal is to math out RPMs to Freq. ratio.

I think the max frequency measured in my application will be around 5000 Hz. And the signal is analog.

So, if it is not implementable then probably AD650 chip will work?


Dependant upon the voltage range of the analog signal, you might be able to use a Schmitt trigger to convert to digital.



For high frequency, I would use another method. You might not get reliable results. Have to try.

If it’s not working, you might try the following:
Configure an input pin as an hardware counter.
See an example here
You might find other examples in this forum using the Register class.
Then make a thread that every second (or more) will read the counter value minus the previous value read which will give you your actual frequency (divide by the number of seconds).

You might also want to use a photocoupler to isolate your board from your sensor, to avoid extra surge that will kill it !


Thanks a lot guys!